Matlab FFT and home brewed FFT

半世苍凉 提交于 2019-12-02 04:25:37

In matlab try using fftshift(fft(...)). Matlab doesn't automatically shift the spectrum after the FFT is called which is why they implemented the fftshift() function.

It is simply a matlab formatting thing. Basically, matlab arrange Fourier transform in following order

DC, (DC-1), .... (Nyquist-1), -Nyquist, -Nyquist+1, ..., DC-1

Let's say you have a 8 point sequence: [1 2 3 1 4 5 1 3]

In your signal processing class, your professor probably draws the Fourier spectrum based on a Cartesian system ( negative -> positive for x axis); So your DC should be located at 0 (the 4th position in your fft sequence, assuming position index here is 0-based) on your x axis.

In matlab, the DC is the very first element in the fft sequence, so you need to to fftshit() to swap the first half and second half of the fft sequence such that DC will be located at 4th position (position is 0-based indexed)

I am attaching a graph here so you may have a visual:

where a is the original 8-point sequence; FT(a) is the Fourier transform of a.

The matlab code is here:

a = [1 2 3 1 4 5 1 3];
A = fft(a);

N = length(a);
x = -N/2:N/2-1;

figure
subplot(3,1,1), stem(x, a,'o'); title('a'); xlabel('time')
subplot(3,1,2), stem(x, fftshift(abs(A),2),'o'); title('FT(a) in signal processing'); xlabel('frequency')
subplot(3,1,3), stem(x, abs(A),'o'); title('FT(a) in matlab'); xlabel('frequency')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!