FFT from CSV data file
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
buumms
il 20 Ott 2016
Commentato: Niki AdiNegoro
il 20 Gen 2020
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 Commenti
Prerak Chapagain
il 13 Giu 2018
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.
Risposta accettata
Star Strider
il 23 Ott 2016
‘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 Commenti
Star Strider
il 25 Ott 2016
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
il 20 Gen 2020
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
Più risposte (1)
KSSV
il 20 Ott 2016
Say you have read your data from csv file into X.
X = csvread(filename) ;
Then use
Y = fft(X) ;
6 Commenti
Vedere anche
Categorie
Scopri di più su Digital Filter Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!