Basic FFT Question: What range to sample the function in.

조회 수: 2 (최근 30일)
Manuel Santana
Manuel Santana 2024년 3월 29일
편집: Mitchell Thurston 2024년 3월 29일
Sorry about this basic question, and what may just be a bug.
I have been playing with the fft, and as a sanity check I thought I would the case of cosine, which we know only has two nonzero terms in the fft, both equal to 1/2. How ever when I sample cosine in the range it gives me the coefficients equal to -1/2.
test_func = @(t) cos(t);
M = 10;
ms = (-M/2:(M/2-1));
dx = 2 * pi / M;
xn = ms * dx;
fs = test_func(xn);
cms = (1/M) * fft(fs);
plot(real(fftshift(cms)),"*")
However when I change my range to everything works as expected. However I am under the impression that the fft should not be sensitive to this change. Can you tell me where I am going wrong?
test_func = @(t) cos(t);
M = 10;
ms = (0:M-1);
dx = 2 * pi / M;
xn = ms * dx;
fs = test_func(xn);
cms = (1/M) * fft(fs);
plot(real(fftshift(cms)),"*")

채택된 답변

Bruno Luong
Bruno Luong 2024년 3월 29일
" However I am under the impression that the fft should not be sensitive to this change. "
Your expectation in incorrect When you change the range you equivalently shift the signal in time domain; resulting multiplying the FFT by the linear phase term; which happens to be -1 at the non zero frequency.
  댓글 수: 1
Paul
Paul 2024년 3월 29일
To put this another way, fft always assumes the first point in the input corresponds to the independent variable being zero. So, when creating the function like this
test_func = @(t) cos(t);
M = 10;
ms = (-M/2:(M/2-1));
dx = 2 * pi / M;
xn = ms * dx;
fs = test_func(xn);
it is a cosine over the interval from -pi to pi
figure
plot(xn,fs,'-o')
But to fft, the signal is actually this
figure
plot((0:M-1)*dx,fs,'-o')
which is a negative cosine.
Because of the symmetry in ms used to define fs, the correct result can be obtained by using ifftshift on input to fft to get the values of the periodic extension of fs in the interval 0:M-1
cms = (1/M) * fft(ifftshift(fs));
figure
stem(ms,real(fftshift(cms)),"*")

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mitchell Thurston
Mitchell Thurston 2024년 3월 29일
편집: Mitchell Thurston 2024년 3월 29일
When you perform an FFT, it is assumed that the original signal is on a timespan from [0 to 2*pi]. When you pass these signals as arguments using fft, the program is seeing these signals as:
The negative signs you're seeing essentially mean "same magnitude of the frequencies, but 180° out of phase"
You can recreate any signal from fftshift output with:
A = fftshift(cms);
mag = abs(A); % frequency magnitude
phase = angle(A); % phase shift for each frequency
w = floor((1-M)/2):floor((M-1)/2); % angular rates
S = @(t) sum(real( mag.*(cos(w.*t+phase)) ));
figure
plot(xn, fs); hold on
fplot(@(t) S(t), [0 2*pi])

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by