# MATLAB FFT of square pulse in MATLAB problem

#### fysiikka111

I am trying to compute the Fourier transform of a square pulse with MATLAB's FFT.
Code:

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

Related Math Software Workshop News on Phys.org

"FFT of square pulse in MATLAB problem"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving