MATLAB: Fast Fourier Transform with discrete data

fft

i measured vibration with oscilloscope and get 1000 points of (time, voltage)
i have seen many example with input function x is known like below 2 sentence
x=sin(2*pi*40*t)+sin(2*pi*80*t);
X=fft(x);
but in case of me, i have discrete data and cannot find example
how can i build the code?
i have tried like below 4 sentence but i cannot make it
[data 1000×2 double]
y=data;
x=fft(y);
X=abs(x);
plot(X);

Best Answer

I suspect the first column of ‘data’ is a time vector and the second column is the signal vector.
If that is correct, try this:
t = data(:,1); % Time Vector
s = data(:,2); % Signal Vector
Ts = mean(diff(t)); % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
L = numel(t); % Signal Length (Number Of Samples)
FTs = fft(s - mean(s))/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTs(Iv))*2)
grid
xlabel('Frequency')
ylabel('Amplitude')
This calculates the Fourier transform, and displays a one-sided Fourier transform (0 Hz to the Nyquist frequency).
This code subtracts the mean of ‘s’ from ‘s’ to remove a constant (d-c) offset (that could obscure other peaks), before taking the Fourier transform.
The sampling interval must be constant, or this code will not give correct results. You can determine that by taking the standard deviation of the time intervals:
Ts_sd = std(mean(diff(t)));
It should be close to 0 (usually less than 1E-12).
Related Question