Differences in FFT of boxcar function

47 次查看(过去 30 天)
Hello,
I am working on plotting FFTs in MATLAB. I generated a boxcar that is 1 from -0.5 to 0.5 and 0 everywhere else. When I take the FFT of this signal, it has internal 'waviness' rather than being a smooth sinc function. However, if I take the fftshift of the original function first, then do the FFT, it is a smooth signal. I'm not sure if this is really how it is supposed to be or if I made an error somewhere.
My code:
%Part A
t=linspace(-10,10,20480);
x=heaviside(t+0.5)-heaviside(t-0.5); %Generate original signal
figure
plot(t,x)
title('x(t)')
xlabel('t')
%Part B; take FFT and plot amplitude/phase
Fs=1024;
NFFT=length(t);
r=[10141:10341];
fVals=Fs*(-NFFT/2:NFFT/2-1)/NFFT;
X=fft(x);
X=fftshift(X);
figure
plot(fVals(r),real(X(r)))
title('Magnitude FFT of x(t) from -5 to 5 Hz')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
figure
plot(fVals(r),imag(X(r)))
title('Phase FFT of x(t) from -5 to 5 Hz')
xlabel('Frequency (Hz)')
ylabel('Phase')
%Part C; shift original signal
x_shift=fftshift(x);
figure
plot(t,x_shift)
title('fftshift of x(t)')
xlabel('t')
%Part D; take FFT of shifted x and plot phase/magnitude
X_s=fft(x_shift);
X_s=fftshift(X_s);
figure
plot(fVals(r),real(X_s(r)))
title('Magnitude FFT of shifted x(t)')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
figure
plot(fVals(r),imag(X_s(r)))
title('Phase FFT of shifted x(t)')
xlabel('Frequency (Hz)')
ylabel('Phase')
Thanks for your help!

回答(1 个)

Yash
Yash 2024-1-12
Hi Heather,
Based on your description, I understand that you are facing an issue with the plot obtained from the Fourier Transform of the boxcar function. Upon reviewing your code, I can identify that the reason for the unexpected plot is that you are getting is the real part of "X(r)". To visualize the sinc wave's magnitude, you should plot the magnitude (absolute value) of "X(r)" instead.
Here's the corrected code segment:
t=linspace(-10,10,20480);
x=heaviside(t+0.5)-heaviside(t-0.5);
Fs=1024;
NFFT=length(t);
r=[10141:10341];
fVals=Fs*(-NFFT/2:NFFT/2-1)/NFFT;
X=fft(x);
X=fftshift(X);
figure
plot(fVals(r),abs(X(r)))
title('Magnitude FFT of x(t) from -5 to 5 Hz')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
By plotting the absolute value of "X(r)", you will see the magnitude of the sinc function. Note that this reflects the magnitude of the Fourier Transform, which same as the sinc signal's magnitude. To interpret information about the signal's negative and positive amplitude, you have to examine the phase component.
Hope this helps!

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by