FFT of a square pulse in MATLAB problem

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 8K views
fysiikka111
Messages
41
Reaction score
0
I am trying to compute the Fourier transform of a square pulse with MATLAB's FFT.
Code:
Matlab:
Fs=1000; %Sampling rate (Hz)
T=1/Fs; %Sampling time interval
P=10; %Period of pulse
t=0:1/Fs:P/2; %Time axis
N=length(t);
x=rectpuls(t,P); %Pulse amplitude
n=pow2(nextpow2(N)); %Number of frequency components
Y=fft(x,n);
freq=Fs/2*linspace(0,1,n/2+1);
subplot(1,2,1)
plot(t,x)
subplot(1,2,2)
plot(freq, 2/N *abs((Y(1 : n/2+1))));
xlim([0 2])
Magnitude at the first frequency component should be 10, but is giving a result of 2. Why is this occurring?
Thanks
 
Last edited by a moderator:
Physics news on Phys.org
It looks like the original question is several years old, but there is a recent reply.

Here is some Matlab code to demonstrate the FFT of a non-periodic square pulse. This will give you the correct amplitude. It will also plot the mag and phase spectrum.[CODE lang="matlab" title="FFT of Square Pulse"]ts=50;
t=-ts:.1:ts;
x=zeros(size(t));
x(450:550)=ones(1,101);
subplot(4,1,1)
plot(t,x)
axis([-ts,ts,-.1,1.1])
title('CONTINUOUS-TIME NON-PERIODIC PULSE SIGNAL x(t) [duration 10 sec]')
xlabel('Time t (sec)')
Tw=10;
fs=2;
f=[-fs:.005:-.005,1,.005:.005:fs];
X=(1./(pi*f)).*sin(pi*f*Tw);
X((length(f)-1)/2+1)=Tw;
f((length(f)-1)/2+1)=0;
subplot(4,1,2)
plot(f,X)
axis([-fs,fs,-3,12])
ylabel('Fourier Transform X(f)')
xlabel('Frequency f (Hz)')
subplot(4,1,3)
plot(f,abs(X))
axis([-fs,fs,-3,12])
ylabel('Magnitude Spectrum |X(f)|')
xlabel('Frequency f (Hz)')
subplot(4,1,4)
plot(f,angle(X))
axis([-fs,fs,-4,4])
ylabel('Phase Spectrum arg{X(f)} (rads)')
xlabel('Frequency f (Hz)')[/CODE]
 
  • Like
Likes   Reactions: jedishrfu