FFT from CSV data file

27 ビュー (過去 30 日間)
buumms
buumms 2016 年 10 月 20 日
コメント済み: Niki AdiNegoro 2020 年 1 月 20 日
Hello,
I have a large CSV file with 1 collum of data. This data contains ~1100 entries.
Now i want to use the FFT on this data. I know T (296s) and f (3.378e-3). I have imported the data with double click on the csv file. Now i can plot my data
But how can I now use this information for the FFT? I tried it with the help of this https://de.mathworks.com/help/matlab/ref/fft.html but what is my S and what is my X?
Fs = 0.00378; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1040; % Length of signal
t = (0:L-1)*T; % Time vector
How can i now refer those informations to the FFT and my data.csv?
I would be delighted to any help.
  4 件のコメント
Prerak Chapagain
Prerak Chapagain 2018 年 6 月 13 日
Can you please post the data.csv file that you used? Can't find it. I am trying to learn and was trying to use this as an example.
Niki AdiNegoro
Niki AdiNegoro 2020 年 1 月 20 日
Can you please attach the data.csv you used? thank you

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2016 年 10 月 23 日
‘what is my S and what is my X?’
Your ‘S’ is your original signal, and your ‘X’ is your noise-corrupted signal. So the code you posted to read your data creates the correct assignment.
To calculate and plot the Fourier transform of your signal ‘X’ and to recover ‘S’ from it, using your csvread call to be certain I’m using the correct data, requires a bit of coding of some relatively straightforward ideas.
The Code:
filename = 'buumms data.csv';
X = csvread(filename,0,2,[0,2,1039,2]);
Fs = 0.00378; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1040; % Length of signal
t = (0:L-1)*T; % Time vector
Fn = Fs/2; % Nyquist Frequency
FX = fft(X)/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
figure(1)
plot(Fv, abs(FX(Iv))*2)
grid
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
FXdcoc = fft(X-mean(X))/L; % Fourier Transform (D-C Offset Corrected)
figure(2)
plot(Fv, abs(FXdcoc(Iv))*2)
grid
title('Fourier Transform Of D-C Offset Corrected Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
[FXn_max,Iv_max] = max(abs(FXdcoc(Iv))*2); % Get Maximum Amplitude, & Frequency Index Vector
Wp = 2*Fv(Iv_max)/Fn; % Passband Frequency (Normalised)
Ws = Wp*2; % Stopband Frequency (Normalised)
Rp = 10; % Passband Ripple (dB)
Rs = 30; % Stopband Ripple (dB)
[n,Wn] = buttord(Wp,Ws,Rp,Rs); % Butterworth Filter Order
[b,a] = butter(n,Wn); % Butterworth Transfer Function Coefficients
[SOS,G] = tf2sos(b,a); % Convert to Second-Order-Section For Stability
figure(3)
freqz(SOS, 4096, Fs); % Filter Bode Plot
title('Lowpass Filter Bode Plot')
S = filtfilt(SOS,G,X); % Filter ‘X’ To Recover ‘S’
figure(4)
plot(t, X) % Plot ‘X’
hold on
plot(t, S, '-r', 'LineWidth',1.5) % Plot ‘S’
hold off
grid
legend('‘X’', '‘S’', 'Location','N')
title('Original Signal ‘X’ & Uncorrupted Signal ‘S’')
xlabel('Time (sec)')
ylabel('Amplitude')
Figure (4):
  7 件のコメント
Star Strider
Star Strider 2016 年 10 月 25 日
My pleasure.
That is the most exact value you can get with your original data. You can get increased frequency and amplitude resolution by zero-padding your signal. One way of doing that would be:
Xmc = X-mean(X); % Mean-Corrected Signal
NFFT = 2^(nextpow2(length(Xmc))+2); % Lengthen The Fourier Transform With Zero-Padding
FXdcocp = fft(Xmc,NFFT)/L; % Fourier Transform (D-C Offset Corrected)
Lp = length(FXdcocp);
Fvp = linspace(0, 1, fix(Lp/2)+1)*Fn; % Frequency Vector
Ivp = 1:length(Fvp); % Index Vector
[FXnp_max,Ivp_max] = max(abs(FXdcocp(Ivp))*2); % Get Maximum Amplitude, & Frequency Index Vector
Frq_max = Fvp(Ivp_max);
figure(5)
plot(Fvp, abs(FXdcocp(Ivp))*2)
grid
title('Fourier Transform Of D-C Offset Corrected Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
You can make ‘NFFT’ arbitrarily long by changing the ‘+2’ to a larger number. The amplitude resolution does not change by increasing its length beyond ‘+2’, but the frequency resolution does. (I did that experiment.) The frequency at the maximum is about 12.9E-006.
Niki AdiNegoro
Niki AdiNegoro 2020 年 1 月 20 日
Hai , i really love your code. it will be a big help with my final project. but it seems i got the format wrong. can you tell me what format should i use in X = csvread(filename,0,2,[0,2,1039,2]); if my file is like this

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2016 年 10 月 20 日
Say you have read your data from csv file into X.
X = csvread(filename) ;
Then use
Y = fft(X) ;
  6 件のコメント
buumms
buumms 2016 年 10 月 20 日
編集済み: buumms 2016 年 10 月 20 日
I finally got it with
X = csvread(filename,0,2,[0,2,1039,2]);
when i now do the fft transformation, is it all i have to do?
KSSV
KSSV 2016 年 10 月 21 日
If you got your whole data then that it is.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDigital Filter Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by