MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Lisa Justin
on 15 Feb 2012

why is the magnitude higher in the plot below without using the flattop window than when it is used. I thought the flattop window should compensate for the window effect

fs=1000; T=1/fs; N=512; n=0:1:512-1;

t=(0:1:N-1)*T; xt=sin(2*pi*101.6*t); xn=sin(2*pi*101.6/fs*n); t=(0:1:N-1)*T; f=fs*(0:length(xn)-1)/length(xn) wn = flattopwin(N); % xn=xn(:); % xn=xn.*(wn); Xk=fft(xn,length(xn)); % Xk=(Xk)'; G=abs(Xk); plot(wn)

*No products are associated with this question.*

Answer by Wayne King
on 16 Feb 2012

Accepted answer

Lisa, you are correcting above for the window, which is what I said initially. You're not working with power in the above example, which is why you are only using sum(w).

The factor of two gives you the correction for a single-sided spectral estimate for all frequencies except 0 and the Nyquist.

You should not multiply 0 and the Nyquist by two.

The mistake you have is that you are plotting the whole spectrum [-Nyquist,Nyquist). You only want to apply the factor of two if you are plotting just a single side, so:

fs=1000; T=1/fs; N=512; n=0:1:512-1; t=(0:1:N-1)*T; xn=2*sin(2*pi*100/fs*n); t=(0:1:N-1)*T; w=flattopwin(N); xn=xn(:); xn1=xn.*w; ws=sum(w); Xk = fft(xn1); Xk = Xk(1:length(Xk)/2+1); Xk = Xk.*(1/ws); Xk(2:end-1) = Xk(2:end-1)*2; plot(abs(Xk));

Lisa Justin
on 16 Feb 2012

if i run your code i get the amplitude at 2. but if i take sca=1/ws as you said in my code i get the amplitude at 0.5 which i think is the correct amplitude for the fourier transform of the sinusoid. so my question is, what is the job of sca=1/ws or the job of sum(W) in both programme. Thanks

Wayne King
on 16 Feb 2012

Lisa, I used 2 as the amplitude in the above example (not 1). Change it to 1 and you see that you now get approximately 1 as the amplitude estimate. Or change it to 4 and see what happens. I have to tried to tell you many times in this thread what the job of sum(w) is. You have to divide out the effect of the window, that is what 1/sum(w) is doing.

Lisa Justin
on 16 Feb 2012

Thanks a million, now i have a better understanding of fft and its scaling!

Answer by Wayne King
on 15 Feb 2012

Hi Lisa, If you use the msspectrum method for spectrum.periodogram with the flat top window, you see that it does an awfully good job.

Fs = 1e3; t = linspace(0,1,1000); x = cos(2*pi*100*t); Hft = msspectrum(spectrum.periodogram('Flat Top'),x,'Fs',1e3,'NFFT',length(x)); sqrt(2*Hft.Data(101)) Hnw = msspectrum(spectrum.periodogram,x,'Fs',1e3,'NFFT',length(x)); sqrt(2*Hnw.Data(101)) % flat top actually does a better job than no window

Lisa Justin
on 15 Feb 2012

how do i convert Hft to the type n*m double because i need to use is subsequently

Answer by Wayne King
on 15 Feb 2012

Lisa, you have to compensate for the L2 norm of the window, regardless of which window you use. Keep in mind that you are multiplying the signal by the window, which means that you are convolving the signal's spectrum with the spectrum of the window.

Show 1 older comment

Lisa Justin
on 15 Feb 2012

how do i convert Hft to the type n*m double because i need to use is subsequently

Lisa Justin
on 15 Feb 2012

One last question please , why is the magnitude higher with no window than with a flattop window in my code?

Answer by Wayne King
on 15 Feb 2012

The L2 norm is the energy of the window, not the length.

You can use Hft.Data that has the doubles that you need.

Hft.Frequencies has the corresponding frequencies.

Lisa Justin
on 16 Feb 2012

Thanks for your support. But i need the result from the fft in type double not just the frequencies. Let say Yk=fft(x,NFFT); So i need Yk to be in the type double because it is Yk i need to work with.

Wayne King
on 16 Feb 2012

Lisa, I have told you. Hft.Data is what you want and that is type double.

Lisa Justin
on 16 Feb 2012

Hi Wayne If i do this i get the frequency at 1 but i do not know what the scale 2/Ws, I really like to know to understand more.

fs=1000;

T=1/fs;

N=512;

n=0:1:512-1;

t=(0:1:N-1)*T;

xt=sin(2*pi*100*t);

xn=sin(2*pi*100/fs*n);

t=(0:1:N-1)*T;

f=fs*(0:length(xn)-1)/length(xn)

w=flattopwin(N);

xn=xn(:);

xn1=xn.*w;

ws=sum(w);

sca=2/(Ws);

Xk=(fft(xn1,length(xn1))).*sca;

plot(abs(Xk))

What does sca=2/Ws do in this code?

Answer by Elige Grant
on 16 Feb 2012

Nooooo... The FFT should be scaled by the time increment (dt = 1/fs). You apply this correction to ALL the amplitudes in the frequency domain, not just the ones that aren't at 0 Hz or the Nyquist.

See my reasons here:

http://www.mathworks.com/matlabcentral/answers/15770-scaling-the-fft-and-the-ifft

It has to do with the conservation of energy between the time domain and its representation in the frequency domain.

## 0 Comments