Why i can't plot a spectrum of a rectangular window function?

Hi everyone! I have a problem, so could you explain to me? I knew that, to plot a spectrum of any windows function we can use "wvtool" command in Matlab or you can find a code to do this on google. But, why i can do this with my code? My plot (use my code) is different with that uses "wvtool". P/s:i understood that spectrum of any window (rect, hanning....) function is FFT of the windows. Is that right or wrong?. And my code:
t=0:100;
N=length(t)-1;
rect=rectwin(N+1); % create a rectangular windows function
taxis=-N/2:N/2; % time axis
plot(taxis,rect); % Plot rectangular windows in time domain.
fftrect=fft(rect); % calculate spectrum of the rectangular window
figure;
plot(taxis,20*log10(abs(fftrect))) % Plot rectangular windows in frequency domain.
wvtool(rect) % Plot rectangular windows in frequency domain use "wvtool" to compare

 Accepted Answer

If you line up the frequency points, you'll see that your FFT is showing the zeros only. If you crank up the number of points in the FFT you are computing you'll see a similar picture:
plot(20*log10(abs(fft(rect,N*10))))

3 Comments

Thank you so much. But, why do you calculate FFT of rect with N*10? I don't understand.
I used 10 so you could see the lobes that are non-null. You could use any positive whole number.
Yes. thank you so much. And, of course, i can use any positive number, e.x. 20,30,..and so on. But, i also have some problems: (for example, we use N*10)
  • 1. Is my problem (in my code above) value of N is too small?. If right, i will use N=1000 in this time, i.e, i will fft(rect,N) instead of fft(rect,N*10) as you used. (see my new code). But, my plot is not same that use wvtool.
  • 2. So, What is difference between utilization directly N = 1000 with N=100 but calculate FFT of rect with number of points = 1000? (i.e. fft(rect,N*10))My new code for N=1000:
N=1000; % I use N=1000 instead of N = 100 as the first.
rect=rectwin(N); % create a rectangular windows function
taxis=-N/2:N/2-1; % time axis
plot(taxis,rect); % Plot rectangular windows in time domain.
fftrect=fftshift(fft(rect,N))/N; % calculate spectrum of the rectangular window
figure;
plot(20*log10(abs(fftrect))) % Plot rectangular windows in frequency domain.

Sign in to comment.

More Answers (1)

There is a mistake in your plotting command. Because the spectrum is in frequency domain, you need to have a frequency axis instead of time axis.
As to the curve it self, your concept is correct. However, the one in wvtool is computed with 1024 points, you can reproduce it using the following code snippet.
nfft = 1024;
fftrect=fft(rect,nfft); % calculate spectrum of the rectangular window
faxis = 0:2/nfft:2-2/nfft;
figure;
plot(faxis(1:nfft/2),20*log10(abs(fftrect(1:nfft/2)))) % Plot rectangular windows in frequency domain.
HTH

3 Comments

Yes, Thank you so much. But i don't understand why does frequency axis is: faxis = 0:2/nfft:2-2/nfft?
That's how the frequency axis is defined in normalized frequency domain. Essentially it divides 0 to 2 to nfft equal bins.
HTH

Sign in to comment.

Asked:

on 29 Jan 2018

Edited:

on 31 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!