Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fft (Fast Fourier Transform)

Status
Not open for further replies.

CusterJess

Computer
Apr 26, 2010
9
Hi.

In the example on how to use fft, once they have the signal, they do this:

NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;

Meaning that they get the next power of 2 from length of y (the signal), and then they calculate the NFFT-points fft, to then divide it by L (length of y).
¿Can you tell me why they do that?

What I'm trying to do is capture a sound signal and then with the fft get the frequency of that signal.

Thank you!
 
Replies continue below

Recommended for you

It is called zero padding. It is fast and has the advantage that usually small differences in the length of the signal don't affect the resolution.

Personally I don't like it and don't do it.

Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376
 
The power of 2 FFT is more efficient. That would be the big driver. Some FFT algorithms like the one in excel won't except any input lenght that is not a power of 2.

The zero padding as Greg says doesn't hurt, in fact gives finer "frequency resolution"... sort of.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Thank you very much for both of your answers!

The thing is that I capture the sound of a guitar, let's say an 'A', and after doing the fft and plot the result, I can see a peak where the main frequency should be, but it shows a number that is not supposed to be the one for 'A' (let's say I get a peak on 29, and the frequency's supposed to be 110), so I was looking for the answer on that business with the n-point fft...

¿You reckon what am I doing wrong?

Thanks again!
 
To be more specific:

in the example, after calculatin the fft with Y=fft(y,NFFT)/L , to show the graph (and get the frequency from there), they do:

f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1);

so maybe I should do something like that (not exactly that though, I guess, cos it gives me that wrong frequency I told you about), but I'm not really sure why they do the 2*abs(Y(1:NFFT/2+1). The only thing I kinda get is that they represent half of the signal only cos you have all the info you need there.

 
The first bin is DC. The second bin is 1/T, where T is the length of your (possibly padded) signal. Other bins are multiples of this. Learning a formula for the frequency axis is (in my view) a waste of time. Derive it from first principles each time instead.

- Steve
 
and as ever, posting your data files and scripts as an attachment is far more likely to get less cryptic replies.

Cheers

Greg Locock


New here? Try reading these, they might help FAQ731-376
 
OP,
do a search. I recall vividly posting an example FFT script.

[peace]
Fe
 
Thanks a lot everyone.

I'll do a search around the forum to look for those fft examples, and if I'm still confused about it, I'll post the code I wrote so you can check it. :)
 
Ok, everything looks correct now :) Thanksalot everyone, you've been tremendly helpful.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor