How to take the FFT of CSV data collected from Arduino

19 views (last 30 days)
I have a CSV data that I want to use spectral analysis on. I've tried looking at examples but they all create their own signal (for example sin(2*PI*f*t)) and that works out, but what about when you just have the data, since I can't model my signal? I input a sin function my code below to test the code. The output should have a peak at 2 Hz but it doesn't look anything like that although the time domain graph shows the correct sin wave. The sin CSV data had 2000 samples.
if true
%code starts here
array = csvread('fakesig.csv'); %Signal import
signaltime = array(:, 2); %Creating signal time
signaldata = array(:, 1); %Creating signal data
N = 2^nextpow2(length(signaldata));
p = abs(fft(signaldata,N))/N; %Power of signal
Fs = 2000; %Sampling frequency
f = Fs/2*linspace(0,1,N); %Frequency spacing
subplot(1,2,1), plot(signaltime,signaldata)
subplot(1,2,2), plot(f,p)
xlabel('Frequency (Hz)'),ylabel('Amplitude')
and heres the output:

Answers (2)

Geoff Hayes
Geoff Hayes on 29 Dec 2014
Dutchy - what is the length of the signalData which determines the block size N? The discrete outputs from the FFT N-point transform can only be associated with frequencies that are multiples of Fs/N where the multiples are
m*Fs/N for m=0,1,2,...,N-1
If there does not exist an m such that m*Fs/N is equal to 2, then you will not see the peak at 2 Hz.
Try changing N to 2000, and see what happens.

Star Strider
Star Strider on 29 Dec 2014
Change your ‘frequency spacing’ line to:
f = Fs/2*linspace(0,1,N/2+1); %Frequency spacing
and you should get the result you want.

Community Treasure Hunt

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

Start Hunting!