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

2 visualizzazioni (ultimi 30 giorni)
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)),"*")

Risposta accettata

Bruno Luong
Bruno Luong il 29 Mar 2024
" 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 Commento
Paul
Paul il 29 Mar 2024
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)),"*")

Accedi per commentare.

Più risposte (1)

Mitchell Thurston
Mitchell Thurston il 29 Mar 2024
Modificato: Mitchell Thurston il 29 Mar 2024
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])

Categorie

Scopri di più su Fourier Analysis and Filtering in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by