Basic FFT Question: What range to sample the function in.
Show older comments
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)),"*")
Accepted Answer
More Answers (1)
Mitchell Thurston
on 29 Mar 2024
Edited: Mitchell Thurston
on 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])

Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



