FFT from .csv voltage and current data file
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pawel Kolodziejski
am 13 Aug. 2022
Kommentiert: Star Strider
am 15 Aug. 2022
Hello everyone,
I am trying to use .csv files of current and voltage in function of time domain and get fft signal of those. Tried to find answers in that topic but found nothing that could help me solving my problem. I am attaching example files gathered from oscilloscope.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 13 Aug. 2022
Bearbeitet: Star Strider
am 13 Aug. 2022
These are not easy files to work with. That is likely the problem.
Try this —
C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1097265/1i.csv')
L1 = cell2mat(C1(strcmp(C1(:,1),'Record Length'),2));
Ts1 = cell2mat(C1(strcmp(C1(:,1),'Sample Interval'),2));
Fn1 = 1/(2*Ts1);
Lv1 = cellfun(@isnumeric,C1(:,1));
format long
Data1 = cell2mat(C1(Lv1,:));
tv1 = linspace(0, L1-1, L1)*Ts1;
C2 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1097270/1u.csv')
L2 = cell2mat(C1(strcmp(C2(:,1),'Record Length'),2));
Ts2 = cell2mat(C1(strcmp(C2(:,1),'Sample Interval'),2));
Fn2 = 1/(2*Ts2);
Lv2 = prod(cellfun(@(x)all(isnumeric(x),2),C2),2)>0;
Data2 = cell2mat(C2(Lv2,:));
LD2 = size(Data2,1);
tv2 = linspace(0, LD2-1, LD2)*Ts2;
figure
yyaxis left
plot(Data1(:,1), Data1(:,2))
ylabel('1i (V)')
yyaxis right
plot(Data2(:,1), Data2(:,2))
ylabel('1u (V)')
xlabel('Time (s)')
NFFT1 = 2^nextpow2(L1);
FT_1i = fft(Data1,NFFT1)/L1;
Fv1 = linspace(0, 1, NFFT1/2+1)*Fn1;
Iv1 = 1:numel(Fv1);
NFFT2 = 2^nextpow2(LD2);
FT_1u = fft(Data2,NFFT2)/LD2;
Fv2 = linspace(0, 1, NFFT2/2+1)*Fn2;
Iv2 = 1:numel(Fv2);
[peakamp1,idx1] = max(mag2db(abs(FT_1i(Iv1))*2));
[peakamp2,idx2] = max(mag2db(abs(FT_1u(Iv2))*2));
fprintf(1,'\nMax Amplitude 1i = %.3f dB\nFrequency = %.3f Hz\n',peakamp1,Fv1(idx1))
fprintf(1,'\nMax Amplitude 1u = %.3f dB\nFrequency = %.3f Hz\n\n',peakamp2,Fv2(idx2))
figure
yyaxis left
plot(Fv1, mag2db(abs(FT_1i(Iv1))*2))
ylabel('1i (dB)')
yyaxis right
plot(Fv2, mag2db(abs(FT_1u(Iv2))*2))
ylabel('1u (dB)')
xlabel('Frequency (Hz)')
Ax = gca;
Ax.XGrid = 'on';
xlim([0 1E+3])
The fft lengths (defined by ‘NFFT1’ and‘NFFT2’) are the same, making the analysis easier. Other than the effort required to get the relevant information from the files, this is straightforward. See the documentation on the various functions to understand how the code works.
EDIT — (13 Aug 2022 at 14:52)
Corrected typographical error.
.
2 Kommentare
Star Strider
am 15 Aug. 2022
As always, my pleasure!
My code as posted should work in R2020a just as it does here. I doubt that there were any significant changes in the relevant functions between those versions. I have no explanation for the problem.
Weitere Antworten (1)
dpb
am 13 Aug. 2022
readmatrix and fft have examples that match up with your Q? -- if you have Signal Processing Toolbox, then there are more user-friendly/sophisticated tools for PSD or the supplied interactive Signal Analyzer app for interactive use.
Give it a go -- "nothing ventured, nothing gained"...
0 Kommentare
Siehe auch
Kategorien
Mehr zu Bartlett finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!