How to determine and adjust the x axis after taking the 1D FFT?

I am trying to take an FFT, but have trouble figuring out the x axis. The time vector is of length N = 100000. The vector has uniformly spaced time points, i.e. dt = t(2)-t(1). How do I find out what the values of f should be? And how can I change the spacing df?

 Risposta accettata

I usually do something like this —
t = linspace(0, 100, 10000); % Time Vector
s = sum(sin([1 5 10 15 20 25 30 35 40 45].'*2*pi*t)); % Signal (Here A Vector)
figure
plot(t, s)
grid
xlabel('Time')
ylabel('Amplitude')
L = numel(t); % Length Of The Signal Vector
dt = t(2)-t(1); % Sampling Time Increment
Fs = 1/dt; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
NFFT = 2^nextpow2(L); % For Efficiency
FTs = fft(s(:).*hamming(L),NFFT)/L; % Fourier Transform (Windowing Not Required, Shown For Information)
Fv = linspace(0, 1, NFFT/2+1)*Fn; % Frequency Vector For One-Sided Fourier Transform Plot
Iv = 1:numel(Fv); % Index Vector (For Convenience)
figure
plot(Fv, abs(FTs(Iv))*2) % Multiply By 2 To Correct For Energy Division Between Positive And Negative Frequencies
grid
xlabel('Frequency (Units)')
ylabel('Magnitude (Units)')
I am not certain what you are doing, however something like this should work.
.

4 Commenti

Thank you so much. Do you mind explaining how it would change if it's a two-sided FFT? And what the Hamming window does?
For a two-sided fft, created using the fftshift function, the frequency vector changes to:
Fv = linspace(-Fn, Fn, length(s));
as a general rule, however depending on whether the length of the fft result is odd or even, that would need to be adjusted so that the zero frequency is exactly at zero in the shifted fft result. I usually use:
Fv = linspace(-Fn, Fn-Fs/length(s), length(s)); % EVEN 'length(s)' (Asymmetric)
Fv = linspace(-Fn, Fn, length(s)); % ODD 'length(s)' (Symmetric)
because that corresponds to the documentation for fft, and generally works. (I like to use linspace for this and similar problems, however it is acceptable to use the colon, : function to do it. See the fft documentation for examples and other details.)
The window functions (other than a rectangular window that is assumed to be applied by default in calculating the fft numerically rather than integrating it symbolically, assuming that is possible) correct for the fft result having a finite length, rather than extending from -Inf to +Inf, as the Fourier transform is defined. (You can experiment with this by taking the fft of a square pulse with and without a window function, such as hamming, hanning (hann), kaiser or others. The result should be a sinc function, and approximates it with a window, however less accurately without one.)
.
As always, my pleasure!

Accedi per commentare.

Più risposte (1)

Hi L'O.G.
Let Y be the output of fft (with or without zero padding)
Let NFFT = numel(Y)
Let Ts = dt = t(2) - t(1) and Fs = 1/Ts.
Then the frequency vector that corresponds to the values in Y is given by
f = (0: (NFFT-1))/NFFT*C
where the proportionality constant C is often one of:
C = 1 % cycle/sample
C = 2*pi % rad/sample
C = Fs % cycle/sec, i.e. Hz
C = 2*pi/Ts % = 2*pi*Fs rad/sec.
If Y is the output of fftshift, then f is
f = (-(NFFT-1)/2 : (NFFT-1)/2)/NFFT*C % N odd
f = ((-NFFT/2) : (NFFT/2-1))/NFFT*C % N even

Categorie

Scopri di più su Fourier Analysis and Filtering in Centro assistenza e File Exchange

Prodotti

Release

R2021b

Tag

Richiesto:

il 19 Dic 2022

Risposto:

il 19 Dic 2022

Community Treasure Hunt

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

Start Hunting!

Translated by