fft/ifft: Sampling Frequency and Length of Signal
Asked Answered
A

1

5

This is partly taken from the Matlab fft-documentation:

Fs = 30;                    % Sampling frequency
T = 1/Fs;                   % Sample time
L = 130;                    % Length of signal
t = (0:L-1)*T;              % Time vector

x = sin(2*pi*1*t);          % 1 Hz sinus

plot(real(ifft(abs(fft(x))))); % fft then ifft

% Fs = 30, L = 60 / 90 / 120 ... : ok
% Fs = 20, L = 60 / 80 / 100 ... : ok
% Fs = 30, L = 50 / 70 / 80 ... : not ok

It seems to me that whenever the length of the signal is a multiple of the sampling frequency, the sinusoid is reconstructed correctly (apart from some shift), e.g. here Fs = 30, L = 60:

enter image description here

However, if for example Fs = 30, L = 80 (not a multiple), the result looks odd:

enter image description here

Is this behaviour correct? Why is this happening and how can I avoid this? Just throw away some part of the signal such that the length "fits" the sampling frequency?

Angora answered 11/6, 2012 at 15:20 Comment(0)
L
7

When you use the abs(fft()) in ifft, you are using only the amplitude of the signal and dropping the phase information, which is needed.

Use the whole signal (removed abs):

plot(real(ifft(fft(x)))); % fft then ifft
Leman answered 11/6, 2012 at 15:22 Comment(2)
Thank you, that solves it. Still, I don't understand why the phase information is needed. Could you elaborate on this a little bit more? Why does it work correctly (apart from the shift) in some cases?Angora
Check fft (or dft) definition. The frequencies in the result of fft(X) are (Fs/2)*(0...(N/2)+1)/(N/2+1). To get to your 1Hz sinus you need to find k such that (Fs/2)*k/(N/2+1) = 1. when k is an integer (round number), no phase (delay) is needed to represent 1Hz, and that happens when N/Fs (or L/Fs in your case) is an integer. When it isn't , there is no 1Hz in your FFT output, and the algorithm "represents" 1Hz in a value k that is close to 1Hz plus a phase value that delays it to 1Hz. I hope my explanation is some what clear. :)Leman

© 2022 - 2024 — McMap. All rights reserved.