PDA

View Full Version : WDL_fft questions


ArdeII
09-19-2009, 01:28 PM
Hi,

Another questions from me ;)

I'm trying to get the fft result frequencies drawn to the screen but I have few problems.
It seems that my freqs aren't in correct order in the response array.

My code:

fftbuffersize = 512;

WDL_fft_init();
WDL_fft(fftbuffer, fftbuffersize,false);
WDL_fft_permute(fftbuffersize, 0);
...
magnitudebin[bin] = sqrt((fftbuffer[bin].re*fftbuffer[bin].re + fftbuffer[bin].im*fftbuffer[bin].im) / (float)fftbuffersize);
...

then I draw those magnitudes to screen and they seem to be in wrong order.

Can you spot any mistakes from my code?
What is the max and min values of the magnitude supposed to be? I need to know these in order to scale them correct.

thanks again ;),
- Arto

schwa
09-19-2009, 01:37 PM
for (i = 0; i < fftsize; ++i)
{
j = WDL_FFT_permute(fftsize, i);
sortedbuffer[i] = fftbuffer[j];
}

ArdeII
09-19-2009, 02:05 PM
thanks,

The frequencies seems to be now in right order but the drawing looks like there is only low freqs and very high freqs present.

Are the values in the magnitude array now ordered from low freq to high freq or do I have to get the freqs out some other way?

I can post the picture out of the situation if ou don't understand what I mean.
My code:

// sort
for (int i2 = 0; i2 < fftbuffersize; i2++)
{
int j = WDL_fft_permute(fftbuffersize, i2);
ffttempbuffer[i2].re = fftbuffer[j].re;
ffttempbuffer[i2].im = fftbuffer[j].im;
}

and the old question from witch you didn't give answer to:

What is the max and min values of the magnitude supposed to be? I need to know these in order to scale them correct.

- Arto

ArdeII
09-20-2009, 03:12 AM
More info about my problem.
I have analyzed the output with sine sweep and other analyzer and I have come to this conclusion:

- The frequencies are mirrored in the buffer after permuting i.e 256 first values are the same ones from end of the buffer to middle of the buffer.

This means if I use fftbuffer size of 512 the result is divided in to 256 frequencies on result buffer??

- The result buffer is not linear after permutation. When drawing the result to the screen when analyzing the sine sweep it seems that there is more high frequency information in the buffer than the low freq information.
There is no or little movement in the graph until the frequncy goes to 800 Hz or so.

Please help me understand the FFT and how it works because I want to learn and at the moment I'm very lost ;)

- Arto

schwa
09-20-2009, 05:26 AM
If you do an FFT on n samples, you get n/2 frequency bins, which divide the frequency spectrum in n/2 equal Hz bins from 0 Hz to N Hz where N = Nyquist threshold = samplerate/2 Hz.

The bins are linear in Hz. You probably getting poor low frequency information because your FFT size is too small, and your FFT is not windowed.

You should do an internet search for general information about FFT, you should be able to find some good explainers.

ArdeII
09-20-2009, 11:58 AM
Thanks Schwa for your answers. You are very helpful.There should be more like you in this world.

If you do an FFT on n samples, you get n/2 frequency bins, which divide the frequency spectrum in n/2 equal Hz bins from 0 Hz to N Hz where N = Nyquist threshold = samplerate/2 Hz.

Yes I know now. I just read the a good article about fft ;) This isn't the case in all the code examples I have read or I didn't notice it.

The bins are linear in Hz. You probably getting poor low frequency information because your FFT size is too small, and your FFT is not windowed.
I figured out why low frequencies weren't showing 'properly':

My sample rate is 48000 and my fftbuffer size is 1024,
I get 512 amplitude values between 0 hz and 24000hz.
This means when the sorted magnitude values are drawn to the screen one pixel length represents 46,875hz. 10 pixels are 468,75hz so viewing low freq values accurately is not possible.

I did feel myself stupid when I realized this. You have to forgive me my stupidity because I have studied this stuff only for few days ;)

You should do an internet search for general information about FFT, you should be able to find some good explainers.
Yes I sure will. Am I nerd or what but this stuff fascinates me ;)

MezmerizeR
09-20-2014, 05:34 PM
Hi,

sorry to re-open this topic.

I'm not a total noob at FFT but kind of ;). I don't understand the WDL_fft fully. What exactly gives me the fft_permute back? If I'm printing the output there are values above half of the FFT size I think.
Why do I get values above (FFT size)/2? I thought I'd get (FFT size)/2 bins.

How can I plot the result on a logarithmic scale because I don't think it's logarithmic at the moment? Besides the spectrum is mirrored what would not be bad, but while sweeping through the frequencies at a certain frequency the spectrum jumps to the beginning again. Here's my Draw() method:

bool Graph::Draw(IGraphics *pGraphics)
{
double mag = 0;
int bins = FFT_BUFFERSIZE/2;
double x =0;
for (int i=0; i<bins; i++)
{
if (FFTmag[i] > GUI_HEIGHT/2)
{
mag = GUI_HEIGHT/2;
}
else
{
mag = FFTmag[i];
}

pGraphics->DrawLine(&COLOR_BLACK,
x, //x1
kGraph_Y+kGraph_H-FFTmag[i], //y1
x + double(GUI_WIDTH)/double(bins) , //x2
kGraph_Y+kGraph_H-FFTmag[i+1]); //y2

x= x + double(GUI_WIDTH)/double(bins);

}
return true;

}

And is there an easy way (a function) to get something like pixels in dB for drawing?

Thank you!
MezmerizeR