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 comentarios

Star Strider
Star Strider el 23 de Oct. de 2016
Attach your data. Use the ‘paperclip’ icon.
buumms
buumms el 23 de Oct. de 2016
Hey, please find attached the data.csv the data i want to use the FFT is on 3rd place in the csv (that numbers around 1200, 1410, 1720, ...)
Prerak Chapagain
Prerak Chapagain el 13 de Jun. de 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.
Niki AdiNegoro
Niki AdiNegoro el 20 de En. de 2020
Can you please attach the data.csv you used? thank you

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 23 de Oct. de 2016

4 votos

‘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 comentarios

buumms
buumms el 23 de Oct. de 2016
This is great! Thank you very much! One last question - where can I see the frequency components? I also tried to solve the problem within Excel and after my calculations the frequency components should be at 0Hz and 2Hz?!
Star Strider
Star Strider el 23 de Oct. de 2016
My pleasure!
You can see the frequency components in figure(1) and figure(2). I used your data for ‘Fs’ to define the frequency vector (and frequency axis on the Fourier transform plots). If ‘Fs’ is actually different from what you posted, you can change it without needing to change any of the rest of the code.
I am not certain what result Excel produced, since I did not run Excel on your data. You have a large d-c offset (the amplitude at 0 Hz) that I included in figure(1) and subtracted out in figure(2). The highest peak in the code I wrote to plot figure(2) occurred at the index value of 5, or 0.000014538447 Hz or 14.538447E-006 Hz.
buumms
buumms el 23 de Oct. de 2016
So my calculation may be right with 0 Hz.
Thanks a lot for your big help!
Star Strider
Star Strider el 23 de Oct. de 2016
My pleasure!
The 0 Hz is the d-c (direct current or constant) offset, not a true frequency peak.
If my Answer solved your problem, please Accept it!
buumms
buumms el 25 de Oct. de 2016
Editada: buumms el 25 de Oct. de 2016
Thanks again. My last question, how do i get that exact value? When I have a look at the peek in figure 2, tho most accurate value i get is 1.1454e-5 ?
And what does this exactly mean?
plot(Fv, abs(FX(Iv))*2)
just like
plot(Fv, abs(FXdcoc(Iv))*2)
Star Strider
Star Strider el 25 de Oct. de 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
Niki AdiNegoro el 20 de En. de 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

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 20 de Oct. de 2016

0 votos

Say you have read your data from csv file into X.
X = csvread(filename) ;
Then use
Y = fft(X) ;

6 comentarios

buumms
buumms el 20 de Oct. de 2016
Editada: buumms el 20 de Oct. de 2016
Thank you for this first answer :)
After trying your code, Matlab says:
Undefined variable "data" or function "data.csv".
X = csvread(data.csv);
Undefined variable "data" or function "data.csv".
And some extra information, the entries are at the 3rd position. I first have seconds, than hh:mm:ss and than my relevant entries, starting at row 2.
And how can i give the range form my starting point to the end?
KSSV
KSSV el 20 de Oct. de 2016
You have to load the data first.....doc csvread
Adam
Adam el 20 de Oct. de 2016
You have to put data.csv in a string too as 'data.csv'
buumms
buumms el 20 de Oct. de 2016
Editada: buumms el 20 de Oct. de 2016
Ah ok, thank you so far. I now just have to find out how i only get only the 3rd out of 5 entries. With
X = csvread(filename,0,2);
I get the 3rd entry but also the 4th and 5th so i have to Read Specific Range from CSV File.
I go crazy ... i cant get only the 3rd entry out of my data...
buumms
buumms el 20 de Oct. de 2016
Editada: buumms el 20 de Oct. de 2016
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 el 21 de Oct. de 2016
If you got your whole data then that it is.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 20 de Oct. de 2016

Comentada:

el 20 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by