# Direct calculation of the fft of a rectangular window

For a rectangular window defined as,

the frequency spectrum equation and magnitude (or pseudo-magnitude) plot are,

However, when I apply Scilab's fft() function or the definition of DFT definition to w[n] I do not get anything like the image shown.

Should not I be able to calculate the frequency spectrum of a rectangular window directly?

• Yes, that is what you should be getting. If you don't it's either a problem with your code or your interpretation of the results. We can't possibly tell what it is unless you add these details Nov 11, 2021 at 19:50

To get a good approximation (approximate because of the use of a finite length FFT) to the Sinc function, you have to zero-pad your rectangle to a large multiple in size (try 16*N, or more), use an FFT of that longer length, and circularly center your rectangle at x[0] (the first element of your FFT input array).

• Thanks to both hotpaw2 and @peter-k. The zero-padding made the difference. I got this from Dr. S.C Dutta Roy's YouTube DSP course. He did not go into much detail. So, why is the zero-padding necessary? It makes the difference for both Scilab's fft() function and when running the much slower coded definition of DFT. Nov 12, 2021 at 18:07
• The FT is an infinite length transform, which is a fairly long length. Zero padding data for an FFT can make the FFT fairly long relative to the length of the data, thus a closer approximation to an FT, much better than, say, an FFT shorter than the data. Nov 12, 2021 at 18:42
• Dr. Dutta Roy did talk about padding a time domain sequences which did not have a some the lengths before convolution. I found that both for fft() and my own DFT code that padding a single time domain sequence would provide a finer gradation in frequency for the spectrum. Nov 12, 2021 at 21:33

To see this, it's best to look at the discrete-time Fourier transform of the window.

\begin{align} W[e^{j\omega}] &= \sum_{n=0}^{N-1} w[n] e^{-j\omega n} \\ &= \sum_{n=0}^{N-1} e^{-j\omega n} \\ &= \left( \frac{1 - e^{-j \omega N}}{1 - e^{-j\omega}} \right)\\ &= \frac{e^{-j\omega N/2}}{e^{-j\omega/2}} \left( \frac{e^{j\omega N/2} - e^{-j \omega N/2}}{e^{j\omega/2} - e^{-j\omega/2}} \right)\\ &= e^{-j \omega(N-1)/2} \frac{\sin(\omega N/2)}{\sin(\omega/2)} \tag*{\blacksquare} \end{align}

Note that if you just use an FFT of the same length as the signal, all the frequency points (apart from the first one) will occur at zeroes of the function (as indicated in your Figure 19).

To see this, run this matlab code:

N = 128;
x = ones(1,N);
X = fft(x,N*10);
omega = [0:10*N-1]/(10*N)*2*pi*(N-1)/2;
subplot(211)
plot(real(X.*exp(1i*omega)))
subplot(212)
plot(imag(X.*exp(1i*omega)))


which produces the figure below.