MATLAB FFT of a square pulse in MATLAB problem

Click For Summary
The discussion centers on computing the Fourier transform of a square pulse using MATLAB's FFT function. The user encounters an issue where the expected magnitude at the first frequency component is 10, but the result is only 2. This discrepancy is attributed to the Nyquist frequency limitation, a common challenge for beginners using FFT. A suggestion is made to review the algorithm and consider the properties of the square pulse. Additionally, an example code snippet is provided to demonstrate the FFT of a non-periodic square pulse, which yields the correct amplitude and includes plots for the magnitude and phase spectrum. The conversation highlights the importance of understanding sampling rates and the implications of non-periodicity in signal processing.
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 jedishrfu
Yes, we were doing spring cleaning on some old threads trying to provide meaningful content so your post is welcome.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 16 ·
Replies
16
Views
14K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K