# Fast Fourier Transform with discrete data

조회 수: 4(최근 30일)
HyunDong Lee 20 May 2020
댓글: Star Strider 22 May 2020
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 1000x2 double]
y=data;
x=fft(y);
X=abs(x);
plot(X);

### 채택된 답변

Star Strider 20 May 2020
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).

#### 댓글 수: 2

HyunDong Lee 22 May 2020
im so happy that your code works
but i have questions
1.
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
2.
plot(Fv, abs(FTs(Iv))*2)
why *2 on abs() value? what does it means?
Star Strider 22 May 2020
1. The ‘1’ is added because the vector begins at 0 and adding the ‘1’ will then include the Nyquist frequency, with the correct number of elements.
2. The fft function returns a vector equal to the size of the input vector, with half of the symmetric Fourier transform vector being the complex conjugate of the other half. (This can be seen most easily after using the fftshift function.) The result is that the signal energy is divided equally between the ‘positive’ and ‘negative’ frequencies. Multiplying by 2 approximately restores the original amplitudes of the signal peaks in a plot of only the ‘positive’ half frequencies in a one-sided Fourier transform, as is done here.