FFT from CSV data file
62 views (last 30 days)
Show older comments
Hello guys,
I have a .csv data from a distance laser. The laser measured an amplitude of a swinging modul for drilling and now I need to find out what the main frequency of the swinging is. The tool rotated with n=500 1/min and the sampling frequency was 20000 Hz. Could anyone help me how to find out what the frequency is?
Thank you for any solution.
Best regards
Mathias
1 Comment
William Rose
on 15 Dec 2023
Edited: William Rose
on 15 Dec 2023
@Mathias Braun, what have you tried so far?
[edit: correct typo in my comments]
First, you need to get your data into the Matlab workspace:
data=importdata('test_amplitude_50.2 (1).csv');
y=data.data;
How long is the vector of measurements?
N=length(y); disp(N)
Now you know the length of the data.
You know the sampling rate. Let's call it fs:
fs=20000;
What will be the frequency vector associated with the fft?
Answers (3)
Star Strider
on 18 Dec 2023
This corroborates previous analyses.
What other information do you want from the data?
% S = fileread('test_amplitude_50.2 (1).csv')
T1 = readtable('test_amplitude_50.2 (1).csv', 'HeaderLines',2, 'VariableNamingRule','preserve');
VN = T1.Properties.VariableNames;
T1.('Protocol TimeStamp').Format = 'yyyy-MM-dd HH:mm:ss.SSSSSS'
t = T1{:,1};
s = T1{:,2};
figure
plot(t, s)
grid
Fs = 1/mean(seconds(diff(T1{:,1})));
Fn = Fs/2;
L = numel(t);
NFFT = 2^nextpow2(L);
FTs = fft((s-mean(s)).*hann(L), NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
[pks,locs] = findpeaks(abs(FTs(Iv))*2, 'MinPeakProminence',0.025);
figure
plot(Fv, abs(FTs(Iv))*2)
grid
xlim([0 100])
text(Fv(locs), pks, sprintf('\\leftarrow Magnitude = %.4f Scaled Distance Units\n Frequency = %.4f Hz\n Period = %.4f s', pks,Fv(locs),1/Fv(locs)), 'Vert','top')
.
2 Comments
William Rose
on 17 Dec 2023
data=importdata('test_amplitude_50.2 (1).csv');
y=data.data; % signal
N=length(y); % length of signal
fs=20000; % sampling rate (Hz)
f=fs*(0:N-1)/N; % vector of frequencies
Y=fft(y-mean(y)); % Y=Fourier transform of y
[~,ind]=max(abs(Y(1:round(N/2)))); % index of max value of Y in the lower half of the 2-sided spectrum
fmaxpow=f(ind);
fprintf('Frequency of max. power=%.1f Hz.\n',fmaxpow)
Plot the FFT and highlight the peak. I will plot the full FFT and I will zoom in on the frequencies of interest.
figure; subplot(211); semilogy(f,abs(Y),'-r',f(ind),abs(Y(ind)),'b*');
grid on; xlabel('Frequency (Hz)');
subplot(212); plot(f,abs(Y),'-r',f(ind),abs(Y(ind)),'b*');
grid on; xlabel('Frequency (Hz)'); xlim([0 50])
This is not the frequency I expected, based on your description ("500 cycles/minute", which equals 8.3 Hz). Therefore let us plot the original signal, to see the oscillation in the time domain. Plot one-second-long segments of y(t), at the start and the middle. Plot final 2 seconds.
t=(1:N)/fs;
figure; subplot(311), plot(t,y,'-r'); grid on; xlim([0 1]); xlabel('Time (s)');
subplot(312), plot(t,y,'-r'); grid on; xlim([(N/2)/fs, (N/2)/fs+1]); xlabel('Time (s)');
subplot(313), plot(t,y,'-r'); grid on; xlim([4 6.1]); xlabel('Time (s)');
The time domain plots confirm that the oscillation frequency is 13 Hz.
Good luck.
2 Comments
Harald
on 15 Dec 2023
Hi Mathias,
after you have imported the signal using readmatrix, readtable, or readtimetable, you can calculate the fft of the amplitude. The documentation of fft has nice examples. You can then, in the easiest case, just look at the frequency with maximum power.
You may find some of the Onramps helpful: https://matlabacademy.mathworks.com/#getting-started, especially the Signal Processing Onramp.
Best wishes,
Harald
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!