FFT using CSV data file

Hi guys, ive been struggling with a task. I have to import the data from .csv file and then do the fft from the data imported, im using code like this:
filename = 'pomiary1.csv';
X = csvread(filename);
Fs = 2.5;
T = 1/Fs;
L = 100000;
t = (0:L-1)*T;
Fn = Fs/2;
FX = fft(X)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
figure(1);
plot(Fv, abs(FX(Iv))*2)
grid
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
Code seems to be fine but the fft it gives is a straight line on 0 level (x axis) and i dont know why
I attached the data file first column is time second is the current

Answers (1)

I could not get csvread to read the file correctly, because each number in the second column has ‘;;;;’ following it in the file you attached.
This works:
[t,S] = xlsread(filename);
X = cellfun(@(c) sscanf(c,'%f;;;;'), S);
Ts = mean(diff(t));
Fs = 1/Ts;
L = length(t);
Fn = Fs/2;
FX = fft(X)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
figure(1);
semilogy(Fv, abs(FX(Iv))*2)
grid
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
I changed your code slightly to correct for the sampling time interval and to define a few other variables from the data.

6 Comments

Mateusz Bess
Mateusz Bess on 4 Jul 2017
Edited: Mateusz Bess on 4 Jul 2017
Thank you for fast answer! Is there any possibility to change this code to be more usefull, i mean for example i have different file, but same task to do and i just type the name of the file and then script goes? I guess then i have to add lines that take more information from the .csv file, or i can just use this script for another files ? By the way thats what i get on output: im analyzing the code at the momment but im not sure why the output looks like this
My pleasure!
You could make a function file from it. See the documentation on Function Basics (link) for details.
Just passing the name of the file to the function might not be enough, however. The problem I had with your file were the ‘;;;;’ after the numbers in the ‘S’ cell array. You would actually have to look at the file to determine whether something like that exists, or the funciton would throw an error and exit. I would read the data outside the function, then pass the time vector and signal vector (or matrix) to the function instead.
When in doubt, plot your data. Adding this:
figure(2)
plot(t, X)
grid
reveals that your data are a noisy sine with a period of about 0.02 (seconds?), or a frequency of 50 (Hz?).
Changing figure(1) to:
figure(1);
plot(Fv, abs(FX(Iv))*2)
grid
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
axis([0 500 ylim])
shows the 50 Hz peak, and that the rest of your signal is essentially noise.
Ok, so the code i have now consist of:
[t,S] = xlsread('pomiary1.csv');
X = cellfun(@(c) sscanf(c,'%f;;;;'), S);
Ts = mean(diff(t));
Fs = 1/Ts;
L = length(t);
Fn = Fs/2;
FX = fft(X)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
figure(1);
plot(Fv, abs(FX(Iv))*2)
grid
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
axis([0 500 ylim])
figure(2)
plot(t, X)
grid
end
for some reason i got strange output out of this, i attached the images. However the code compiles, but it doesnt give proper output. U asked about period and frequency, i also attached second file with parameters: sample interval is 0,4e-07 so that means the overall time of signal is 0,4s because 100k*0,4e-07 gives 0,4 and the frequency then is 1/0,4 which gives 2,5 (do i think right?) The .csv file is exact the same as the one i added previously, but it contais informations from osciloscope
My code works or I would not have posted it.
Your calculation for sampling frequency ‘Fs’ is incorrect. The sampling frequency is the inverse of the sampling interval, ‘Ts’ in my code. The sampling interval is 4E-7, and the sampling frequency is then 2.5E+6.
Try this new version of figure(3):
figure(3)
subplot(3,1,1)
plot(Fv, abs(FX(Iv))*2, 'LineWidth',1.5)
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
grid
subplot(3,1,2)
plot(Fv, abs(FX(Iv))*2)
title('Fourier Transform Of Original Signal ‘X’')
xlabel('Frequency (Hz) [Zoomed]')
ylabel('Amplitude')
axis([0 500 ylim])
grid
subplot(3,1,3)
plot(t, X)
grid
title('Time Domain Plot Of Original Signal ‘X’')
xlabel('Time (s)')
ylabel('Amplitude')
It should explain everything.
Thanks a lot for your time. I know your code works, i think its just my matlab issue, cause as u saw i dont have any plots just empty diagrams. I'm working on it, could you please send me a screenshot of this figure(3) i would be so grateful.
Here you go ...

Sign in to comment.

Categories

Tags

Asked:

on 4 Jul 2017

Commented:

on 4 Jul 2017

Community Treasure Hunt

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

Start Hunting!