csv to fft matlab

3 views (last 30 days)
tim
tim on 9 Jun 2024
Commented: Star Strider on 9 Jun 2024
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

Answers (1)

Star Strider
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}
ans = 10000x1
-0.0456 2.3588 3.1085 3.7611 4.6293 6.3481 7.5509 8.0792 8.1042 9.3831
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T{2}
ans = 10000x1
0.0882 0.9211 1.4539 1.5864 1.8279 2.4375 2.7633 3.1545 3.7334 3.8463
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
tim
tim on 9 Jun 2024
Thanks a lot for your answers !!
Star Strider
Star Strider on 9 Jun 2024
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

Categories

Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!