# What is difference between utilization directly N = 1000 with N=100 but calculate FFT of rect with number of points = 1000?

2 views (last 30 days)
Le Dung on 31 Jan 2018
Commented: Le Dung on 3 Feb 2018
|Hi everyone! I'm trying to plot a spectrum of a rectangular window function. But, i have a problem. that is, when i use N=100 (first code) and calculate FFT of rect use : fft(rect,N*10) that results in my plot (spectrum)is same with that use "wvtool". (thank you to Greg Dionne who showed me how to do). But, when i use N = 1000 (second code), that mean i will calculate FFT of rect use: fft(rect,N) result in my plot (spectrum) is not same that use "wvtool". So, why? although, number of points of FFT is the same? (number of points = 1000). By the way, Am I right or wrong when divided FFT of rect by N (sixth line in code)? Thank you so much! You can see my problem in link: https://www.mathworks.com/matlabcentral/answers/379530-why-i-can-t-plot-a-spectrum-of-a-rectangular-window-function?s_tid=mlc_ans_email_view#comment_530073
% FIRST COE:
N=100;
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*10))/N; % calculate spectrum of the rectangular window
figure;
plot(20*log10(abs(fftrect))) % Plot rectangular windows in frequency domain.
wvtool(rect) % Plot rectangular windows in frequency domain use "wvtool" to compare
% SECOND CODE:
N=1000;
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.
wvtool(rect) % Plot rectangular windows in frequency domain use "wvtool" to compare

David Goodmanson on 31 Jan 2018
Hello Le Dung
A window is of course a function that is large in one area and small in another. Let's take the SECOND CODE first. The supposed window function is equal to 1 all the way across, as in the first plot. As far as the fft is concerned, it just sees a constant function. When you transform a constant function you get a single nonzero point in the frequency domain, zeros everywhere else. You can see that if you change the second plot command to
plot(taxis,(real(fftrect)),'o').
So in this case you are getting exactly what you should be getting. No window.
What about the FIRST CODE? Because of the 10*N option you are doing the tranform on ten times as many points as you supplied. The fft creates those by zerofilling, so the function y(x) you are transforming is now
y = 1 for 1 <= x <= 100
y = 0 for 101 <= x <= 1000
which is a very different thing. It's a window, so you get an appropriate fft.
What about wvtool? It seems to have a way of knowing that it needs to construct a window, possibly by zerofilling. I don't know, though. I don't use it because I don't know how it works.
Le Dung on 3 Feb 2018
Yes. Thank you so much. Dear David. Could you explain to me my problem on link: https://www.mathworks.com/matlabcentral/answers/380363-why-peak-of-main-lobe-in-spectrum-of-hanning-window-is-not-equal-to-zero