Fourier transform of a rectangular pulse

101 views (last 30 days)
Luke Peterson
Luke Peterson on 23 May 2022
Edited: Matt J on 24 May 2022
I'm trying to plot the FFT of a rect pulse but the outcome is not so "pretty". I generate a rectangular pulse inside this range (-29.5, 29.5) and the A = 7. When I try to plot the FFT the outcome it's just an angle and I have to zoom in to see the sinc. You got any idea how to beautify it?
My code is this:
and the outcome is this:
Does this problem have to do with my initial rectangular pulse? Did I make it right?
Paul on 23 May 2022
Why would the output of fft() be plotted against t?

Sign in to comment.

Answers (2)

Paul on 23 May 2022
Hi Luke,
Your original code was o.k. except for plotting against t. It is typically plotted against an array of frequencies. Also, you can use fftshift() to get the "beautified" plot, you'll have to zoom in ...
Here is some code that works a similar problem. Maybe you can modify it as necessary.
Define a continuous time rectangular pulse with unit amiplitude and width 11 and its Fourier transform.
syms t w real
y(t) = rectangularPulse(-5.5,5.5,t);
Y(w) = simplify(fourier(y(t),t,w))
Y(w) = 
Its Fourier transform is a real sinc.
Sample the continuous signal. We could sample y(t) directly, but here we use rectpuls()
Ts = 0.01; % sampling period
tval = -10:Ts:10; % time samples
yval = rectpuls(tval,11); % signal samples
We ant to maintain symmetry with the continous signal, so we need to modify the samples at the leading/trailing edges of y(t)
yval(abs(tval) == 5.5) = 1/2;
Verify that only two values of yval were modified
tval(yval == 1/2)
ans = 1×2
-5.5000 5.5000
Now take the fft of the samples
Ydft = fft(yval);
The number of samples is odd
N = numel(Ydft)
N = 2001
The discrete frequency vector that corresponds to the fft outpus is
wn = (0:N-1)/N*2*pi;
The fft() assumes that the first sample corresponds to n = 0. But in reality our first sample started at n*Ts = -10. So we have adjust the phase of the output of fft()
nshift = tval(1)/Ts;
Ydft = Ydft.*exp(-1j*wn*nshift);
Verify that the imaginary part is (approximately) zero, as expected based on Y(w)
ans = 2.2723e-10
Apply fftshift() to put the zero frequency in the center
Ydft = fftshift(Ydft);
Scale by the sampling period
Ydft = Ydft*Ts;
Compare Ydft to Y(w)
hold on
wval = (-(N-1)/2:(N-1)/2)/N*2*pi/Ts; % frequency vector for odd N
xlim([-5 5])
As expected, the fft() outputs very closely approximate samples of the Fourier transform of the continuous pulse.

Matt J
Matt J on 23 May 2022
Edited: Matt J on 24 May 2022
It's always good to decide on both your time and frequency sampling axes first, before doing any FFT processing. For a pulse width of 59, you don't really need such a high time sampling rate, though it doesn't do any harm. Also, be mindful that rectpuls does not generate the pulse in ifftshifted position.
dt=1; %desired time sampling interval
dfmax=0.001; %desired frequency sampling interval (upper bound)
df=1/N/dt; %actual frequency sampling interval (df<=dfmax)
taxis=Naxis*dt; %time axis
faxis=Naxis*df; %frequency axis
X=fftshift( real(fft(ifftshift(x))));
plot(faxis, X); xlabel 'Frequency (Hz)'

Community Treasure Hunt

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

Start Hunting!