I generate a stepped sinusoidal signal of 5 kHz each 2 µs from 100 data samples on the 32-bit MCU (ATSAMD21). How to properly interpret the results of the FFT/DFT calculated from an Excel CSV file acquired on the oscilloscope? I am mainly interested to find out the 5 kHz peak in the frequency characteristic. DoIneedtosomehownormalizethedatabeforeprocessing?
I have not programmed in any version of C (or FORTRAN) in the 25+ years since I began using MATLAB, although I was before then reasonably proficient in both.
Maybe it should be possible to generate C++ code for embedded 32-bit MCU (ATSAMD21J18) to callulate Frq = 4.8828e+03 through Embedded Coder - MATLAB & Simulink.
I don’t have the Embedded Coder, so I have no experience with it.
It might be possible to use it here, since it is available using the Onlline Run feature (I checked). The problem is that anything using GUIs will not be possible here because the Online Run feature doesn’t support them. If it doesn’t require GUIs, then it could be possible to use it here. MATLAB Online is limited to what you have personal licences for, and it is also limited to applications that do not use GUis.
What you want to do may also require a specific Support Package. See Hardware Support Packages & Services and Third-Party Products & Services for that information. I’m not certain that using them would be possible with the Online Run feature here, although it’s certainly worth doing that experiment to see if it is. There are a number of Code Generation - Examples that could help.
This is far from my areas of expertise. This may be the limit of my ability to help with the C++ programming part of it.
Thank you for your guidance. I am little worried it may be overkill for my simple purpose. Maybe I will try to understand the functions used in your code and program them in C++.
I tried to do that with a function version of my code (attached), however it will not work with the Online Run feature —
type data_fft_display.m
function data_fft_display(filename)
T1 = readtable(filename)
t = T1{:,1}*0.5E-6; % Added: 't' in 0.5 µs Steps
s = T1{:,2};
figure
plot(t, s)
grid
xlabel('Time (units)')
ylabel('Amplitude (units)')
Ts = mean(diff(t));
Fs = 1/Ts;
Fn = Fs/2;
L = size(T1,1);
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);
[smax,idx] = max(abs(FTs(Iv))*2);
Frq = Fv(idx)
figure
plot(Fv, abs(FTs(Iv))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
xline(Frq, '-r', sprintf('Frequency = %.4fx10^3 Hz',Frq*1E-3))
xlim([0 1E+4]) % Zoom To See Detail
end
codegen data_fft_display -argx{filename}
Unable to run the 'fevalJSON' function because it calls the 'codegen' function, which is not supported for this product offering.
So apparently codegen isn’t available here, as I thought it would be. I’ll likely never use it (I haven’t needed it thus far, and it’s likely fairly expensive), however the Embedded Coder could be available as a trial if you want to go that route. That’s a one-off, so if I remember correctly, after the trial is over, another one isn’t possible. You’d need to buy it to keep using it.
I sampled the generated stepped sinusoidal signal by free running ADC to get 1024 samples. Total time of sampling is 5.95 ms i.e. 5.81 µs/sample. Attached is the one column text table with ADC Voltage results.
How to modify the script to process this new data?
Yes. The grid of the oscilloscope has twelve 50 µs rectangles which give the total sum 12 x 50 µs = 600 µs / 1200 (data samples in Excel CSV file) = 0.5 µs/data sample. The period of the signal is twenty one grid subdivisions i.e. 21 x 10 µs (50 / 5) = 210 µs i.e. 4 762 Hz.