From: Luis on
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!
From: Wayne King on
Luis <luisthearcade(a)gmail.com> wrote in message <1695692628.24503.1272294384294.JavaMail.root(a)gallium.mathforum.org>...
> 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!
Hi Luis,
That L is a scaling factor to produce a power spectral density estimate. To be technically correct, it should also include (1/Fs) where Fs is the sampling frequency. From the description of your use case, you seem to just be attempting to locate a peak in your spectrum identifying a sinusoidal component. In that case, the scaling factor should not matter and you can just plot the absolute value or absolute value squared of the DFT. If you have the Signal Processing Toolbox, there are plenty of spectral estimators available to you that will automatically produce a correct frequency axis and properly scaled PSD estimate, so that you can identify the frequency of interest. I'm including an example of a PSD estimate using the Signal Processing Toolbox functionality and the "brute force" way using fft(). You can see how the use of the scaling factor produces agreement between the Signal
Processing Toolbox functions and fft().

dt=1/10000; %Fs=10000
t=0:dt:.1024-dt; %time vector of length 1024
y=cos(2*pi*1000*t)+randn(size(t)); %1 kHz sinewave in noise
h=spectrum.periodogram; %spectrum object (periodogram)
NFFT = 2048;
Hpsd = psd(h,y,'Fs',1e4,'NFFT',2048,'SpectrumType','onesided');
plot(Hpsd);

%now let&#8217;s do it by brute force and check agreement
psd_y=(dt/length(y))*abs(fft(y,2048)).^2;
psd_y=psd_y(1:NFFT/2+1);
psd_y(2:end)=2*psd_y(2:end);
%plot the comparison between PSD estimates
figure;
plot(Hpsd.Frequencies./1000,10*log10(psd_y))
xlabel('kHz'); ylabel('Power/Hz (dB/Hz)'); grid on;

Hope that helps,
Wayne
From: Greg Heath on
On Apr 26, 11:05 am, Luis <luisthearc...(a)gmail.com> wrote:
> 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!

1. You don't have to do either.

a. If L is huge and/or you have to process many cases,
you can reduce running time by a factor of log(NFFT)/L .
If this doesn't concern you, just use

Y = fft(y)/L.

b. The normalizing factor L will yield an amplitude of 1
for exp(i*2*f0*t) at f = f0.

Hope this helps.

Greg
From: Steve Amphlett on
Greg Heath <heath(a)alumni.brown.edu> wrote in message <c17b6883-f695-4934-877c-838b7ff5f82d(a)n5g2000yqh.googlegroups.com>...
> On Apr 26, 11:05 am, Luis <luisthearc...(a)gmail.com> wrote:
> > 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!
>
> 1. You don't have to do either.
>
> a. If L is huge and/or you have to process many cases,
> you can reduce running time by a factor of log(NFFT)/L .
> If this doesn't concern you, just use
>
> Y = fft(y)/L.
>
> b. The normalizing factor L will yield an amplitude of 1
> for exp(i*2*f0*t) at f = f0.
>
> Hope this helps.
>
> Greg

.... except at DC and Nyquist, where you need 2/L.
From: Greg Heath on
On Apr 27, 2:50 am, "Steve Amphlett" <Firstname.Lastn...(a)Where-I-
Work.com> wrote:
> Greg Heath <he...(a)alumni.brown.edu> wrote in message <c17b6883-f695-4934-877c-838b7ff5f...(a)n5g2000yqh.googlegroups.com>...
> > On Apr 26, 11:05 am, Luis <luisthearc...(a)gmail.com> wrote:
> > > 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!
>
> > 1. You don't have to do either.
>
> > a. If L is huge and/or you have to process many cases,
> > you can reduce running time by a factor of log(NFFT)/L .
> > If this doesn't concern you, just use
>
> > Y = fft(y)/L.
>
> > b. The normalizing factor L will yield an amplitude of 1
> > for exp(i*2*f0*t) at f = f0.
>
> > Hope this helps.
>
> > Greg
>
> ... except at DC and Nyquist, where you need 2/L

No.

>> Fs = 1, N = 6, dt = 1/Fs
t = dt*(0:N-1);
x = 1 + cos(2*pi*(Fs/2)*t);
X = fft(x)/N

Fs = 1
N = 6
dt = 1

X = 1 0 0 1 0 0

Hope this helps.

Greg