csv to fft matlab
3 views (last 30 days)
Show older comments
Hello everybody,
I'm trying to convert two csv files to fft and I looked on different forums and tried a few solutions but nothing conclusive.
Someone could help me please.
Thanks
0 Comments
Answers (1)
Star Strider
on 9 Jun 2024
Edited: Star Strider
on 9 Jun 2024
You cannot calculate the Fourier transfomors without a corresponding sampling frequency or time vector.
Assuming the sampling frequency is 1 kHz, try this —
files = dir('*.csv');
Fs = 1E+3;
for k = 1:numel(files)
fn = files(k).name;
T{k} = readmatrix(fn);
t{k} = linspace(0, size(T{k},1)-1, size(T{k},1)).'/Fs;
[FTs1{k},Fv{k}] = FFT1(T{k},t{k});
figure
plot(t{k}, T{k})
xlabel('Time')
ylabel('Amplitude')
title(fn)
figure
plot(Fv{k}, abs(FTs1{k})*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title(fn)
Ax = gca;
Ax.XScale = 'log';
end
T{1}
T{2}
function [FTs1,Fv] = FFT1(s,t)
t = t(:);
L = numel(t);
if size(s,2) == L
s = s.';
end
Fs = 1/mean(diff(t));
Fn = Fs/2;
NFFT = 2^nextpow2(L);
FTs = fft((s - mean(s)) .* hann(L).*ones(1,size(s,2)), NFFT)/sum(hann(L));
Fv = Fs*(0:(NFFT/2))/NFFT;
% Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
FTs1 = FTs(Iv,:);
end
EDIT — Added time-domain plots.
.
2 Comments
Star Strider
on 9 Jun 2024
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
See Also
Categories
Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


