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

1 view (last 30 days)
|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

Accepted Answer

David Goodmanson
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.
  3 Comments
David Goodmanson
David Goodmanson on 31 Jan 2018
Edited: David Goodmanson on 31 Jan 2018
If you are ffting something that is seen as repeating in time, i.e. you can put side-by-side many copies of the function, then you do divide by N. For signal processing, in most cases you divide by N.
As far as a rectangular window you could do e.g.
w = zeros(1,1000);
w(1:100) = 1;
plot(w)
If you make the function yourself, you have control of the situation.
By the way I went and looked up rectwin. I imagine you know this anyway, but it's one of those dumb little "convenience" functions, and all it is is ones(1,n)
Le Dung
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!