FFT from .csv voltage and current data file

17 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 13 Aug 2022
Edited: Star Strider on 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')
C1 = 100014×2 cell array
{'Model' } {'MSO4054' } {'Firmware Version' } {[ 2.1500]} {'Point Format' } {'Y' } {'Horizontal Units' } {'S' } {'Horizontal Scale' } {[ 0.0040]} {'Sample Interval' } {[ 4.0000e-07]} {'Record Length' } {[ 100000]} {'Gating' } {'0.0% to 100.0%'} {'Probe Attenuation'} {[ 10]} {'Vertical Units' } {'V' } {'Vertical Offset' } {[ 0]} {'Vertical Scale' } {[ 100]} {'Label' } {'U1' } {'TIME' } {'CH2' } {[ -0.0200]} {[ 2.2656]} {[ -0.0200]} {[ 2.1406]}
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')
C2 = 100014×2 cell array
{'Model' } {'MSO4054' } {'Firmware Version' } {'lut.15' } {'Point Format' } {'Y' } {'Horizontal Units' } {'S' } {'Horizontal Scale' } {[0.004000000000000]} {'Sample Interval' } {'4,00E-07' } {'Record Length' } {[ 100000]} {'Gating' } {'0.0% to 100.0%' } {'Probe Attenuation' } {[ 1]} {'Vertical Units' } {'V' } {'Vertical Offset' } {[ 0]} {'Vertical Scale' } {[0.002000000000000]} {'Label' } {1×1 missing } {'TIME' } {'CH1' } {[-0.020000000000000]} {[0.001188440000000]} {[-0.019999600000000]} {[0.001229370000000]}
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))
Max Amplitude 1i = -35.396 dB Frequency = 19.073 Hz
fprintf(1,'\nMax Amplitude 1u = %.3f dB\nFrequency = %.3f Hz\n\n',peakamp2,Fv2(idx2))
Max Amplitude 1u = -35.391 dB Frequency = 19.073 Hz
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 Comments
Pawel Kolodziejski
Pawel Kolodziejski on 15 Aug 2022
@Star Strider thanks for answer this is really helpful never tried it that way. But still the code doesnt show the orange '1u' graph in frequency domain just like the picture you provided, i find it hard to understand :/
Star Strider
Star Strider on 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.

Sign in to comment.

More Answers (1)

dpb
dpb on 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"...

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!