# FFT of square wave: maths and actual numbers do not coincide

this fact is driving me insane... Theory states that the Fourier transform of a square wave is a sinc function, but if I compute the fft of a synthetic (perfect) square wave of amplitude 1 and frequency 1 and I check the numbers, I see them drift from a sinc function. Phases seem to change linearly thru the bins, instead of being all constant as expected, and real values (the odd ones of course) are all strangely equal to 2/framesize instead of being zero as I would expect. In fact, if I synthesize the same square wave in frequency domain by simply using a sinc function and I do the IFFT and I check the output, I notice that the square wave so obtained is almost perfect if it were not for some tiny ringings at the edges (Jibbs ?) How is it ? What is then the "correct" function representing it in frequency domain if not a complex sinc as it should be ??? Or perhaps what I noticed is simply due to the fact that, since we are dealing with discrete quantities (fft =dft) things work slightly different than in the abstract case (fourier transform in pure math sense), and therefore some little modifications (scaling ?)are required ? Thanks in advance

• By Fourier transform, do you mean Continuous-Time Fourier transform ? – Fat32 Sep 26 '19 at 19:41
• It's not clear if the "perfect" square wave you refer to is continuous- or discrete-time. Can you clarify? – MBaz Sep 26 '19 at 21:37
• The Fourier transform (the math) assumes a signal of infinite length so it's not surprising that your results don't coincide exactly with the theory. In order to get closer to what you expect, increase your FFT length, thus making your frequency bins narrower (better frequency resolution). – dsp_user Sep 27 '19 at 7:51

The continuous-time Fourier transform of a single rectangular pulse $$p(t)$$ of duration $$[-d,d]$$ is :

$$P(\Omega) = \frac{ 2 \sin(\Omega d) }{\Omega } = 2 d \cdot \text{sinc}( \frac{\Omega d}{\pi} ) \tag{1}$$

where $$\Omega$$ is the frequency in radians per second.

You can not represent $$p(t)$$ or $$P(\Omega)$$ using a sampled-data computer system because $$p(t)$$ is not bandlimited. However, there is an analogous definition of a rectangular pulse in discrete-time, defined as a sequence $$x[n]$$ of duration $$[-d,d]$$, with $$d$$ being integer, whose discrete-time Fourier transform is :

$$X(e^{j\omega}) = \frac{ \sin( \omega (2d+1) /2 ) }{ \sin( \omega /2 )} \tag{2}$$

which is called a periodic-sinc or a Dirichlet kernel. Note that this is not a simulation of the continuous case.

Using the computer with N-point DFT/FFT, implementation of discrete-time case will be

$$X[k] = X(e^{j\omega})|_{w_k = \frac{2\pi}{N}k} = \frac{ \sin( \frac{\pi k}{N} (2d+1)) }{ \sin( \frac{\pi k}{N} )} \tag{3}$$

A Matlab/Octave implementation is as follows

d = 3;       % rectangular pulse in [-d,d]
N = 32;      % DFT/FFT length

k = 0:N-1;   % DFT index

X = sin( (pi/N)*(2*d+1)*k)./sin( (pi/N)*k));
X(1) = 2*d+1;

x = real(ifft(X,N));   % compute inverse DFT on Xk

figure,stem(k,xp);
title('rect-pulse from inverse DFT of periodic-sinc');


Note that the circularity of DFT implies that negative indices of the pulse $$x[n]$$ are at the right edge.

• Aha, I needed an aliased sinc or Dirichlet kernel ! More or less as I expected then.. and it does the job perfectly ! Thanks 😀 To answer the other comments, I was referring to the fft/ifft of a frame of finite length of course. With "perfect" square wave I meant a digitally precise one i.e on a frame of 1024 samples (513 bins) the first 512 samples are 1 and the others -1. I am not a professional mathematician, the underlying math is quite complex to me but I am trying to slowly make my way into it... – elena Sep 27 '19 at 20:19
• yes that's basically ok. – Fat32 Sep 27 '19 at 21:21
• Sir, would you please have a look at this question: dsp.stackexchange.com/q/61064/26549 – Soumee Oct 4 '19 at 18:00