Single cycle fft

Hello guys, I’m playing with FFT, using this code to get phases and magnitudes from one shape cycle. I get phases and magnitudes using inverse transformation and restore the signal. Everything looked good before I started editing the phases. I saw that every time I get a phase with a shift. -1.57233 looks like half PI. I’m not an expert in FFT, maybe it’s impossible to get phases from one cycle? or what am I doing wrong? To check, I made several WAV files, 2048 each.
And everyone shows me this shift :slight_smile:

test filtes: sines.zip - Google Drive
first sine with start phase 0 give me shift -1.57233

fftOrder = 11;
fftSize = 1 << 11;
tableLen = 2048;

void timeDomainToPhasesAndMagnitudes(float *table, int tableLen, int fftOrder, int fftSize, float *magnitudes, float *phases)
{
	float *fftData = new float[fftSize * 2];
	for (int i = 0; i < fftSize * 2; i++)
	{
		fftData[i] = 0;
	}
	for (int i = 0; i < tableLen; i++)
	{
		fftData[i] = table[i];
	}

	fowardFFT->performRealOnlyForwardTransform(fftData, false);

	fftData[0] = 0;
	fftData[fftSize * 2 - 1] = 0;
	for (int i = 0; i < fftSize * 2; i++)
	{
		fftData[i] /= fftSize * 2;
	}
	auto *complexData = reinterpret_cast<std::complex<float> *>(fftData);
	for (int i = 1; i < 512; i++)
	{
		const float re = complexData[i].real();
		const float im = complexData[i].imag();
		float magnitude = std::abs(complexData[i]);
	

		float phase = 0;
		// if (magnitude >= 0.000001) // -120 db
		// 	phase = std::atan(im / re);
		if (magnitude >= 0.000001) // -120 db
			phase = std::arg(complexData[i]);

		magnitudes[i] = magnitude;
		phases[i] = phase;
	}
	delete[] fftData;
}

When you say sine do you mean the sin() function, or cos() function, or either? Try the FFT of cos() and see what you get :wink:

it looks like

yes I used sinf to create test files

If your wave files have 2048 samples, what is the FFT order / size you’re using? I see the number 512 in the code, which leads me to believe the fftSize is 512 as well?

In that case, the for (int i = 0; i < tableLen; i++) might be writing outside the bounds of your fftData array.