How to interpret results of FFT/DFT?
5 次查看(过去 30 天)
显示 更早的评论
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. Do I need to somehow normalize the data before processing?
采纳的回答
Star Strider
2023-1-26
编辑:Star Strider
2023-1-27
The time vector needs to be scaled correctly if the frequency vector is to be correct.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1275525/CSV_5k.csv')
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
EDIT — (27 Jan 2023 at 13:50)
Scaled ‘t’ to be appropriate by multiplying it by so that the sampling interval ‘Ts’ is .
.
14 个评论
Dan Richter
2023-1-27
Thank you your exact solution which includes the calculated frequency of the generated signal.
Dan Richter
2023-1-30
Any advice on how to modify attached C++ VS2022 program so I can use it on embedded 32-bit MCU (ATSAMD21) system?
I will get following results:
0.000000 0.000000
7.939106 1666.666748
16.939116 3333.333496
153.005859 5000.000244
15.244903 6666.666992
8.394692 8333.333740
5.840863 10000.000488
4.610853 11666.667236
...
How to calculate the peak frequency 4.8828e+03 more precisely?
C++ VS2022 program code:
// Source:
// https://www.geeksforgeeks.org/discrete-fourier-transform-and-its-inverse-using-c/
#include <iostream>
// C program for the above approach
#include <math.h>
#include <stdio.h>
const int len = 1200;
// Function to calculate the DFT
void calculateDFT()
{
//int xn[len] = { 840, 920, 880, 760, 800, 760, 760, 680, 720, 640, 760, 600, 640, 560, 640, 560, 520, 640, 600, 480, 520, 480, 560, 440, 480, 440, 440, 480, 400, 480, 440, 360, 440, 360, 360, 440, 400, 360, 360, 440, 360, 440, 440, 360, 400, 400, 440, 360, 360, 480, 440, 360, 480, 440, 480, 400, 440, 480, 440, 560, 440, 560, 560, 480, 520, 600, 600, 520, 560, 640, 680, 600, 640, 760, 760, 680, 680, 840, 800, 880, 760, 880, 880, 960, 880, 000, 040, 960, 040, 960, 80, 160, 160, 80, 160, 240, 280, 200, 240, 400, 320, 400, 360, 520, 520, 440, 440, 640, 640, 560, 600, 760, 720, 800, 760, 920, 880, 920, 880, 80, 80, 040, 040, 240, 200, 240, 200, 320, 320, 400, 320, 520, 560, 480, 480, 680, 640, 760, 640, 800, 880, 800, 800, 920, 960, 040, 960, 80, 80, 200, 200, 120, 240, 360, 320, 400, 320, 520, 520, 440, 480, 720, 600, 680, 640, 840, 840, 760, 800, 000, 920, 000, 880, 120, 80, 160, 80, 280, 280, 240, 160, 400, 400, 320, 360, 560, 560, 480, 480, 680, 680, 600, 600, 760, 800, 720, 680, 880, 840, 920, 800, 000, 920, 040, 040, 920, 040, 120, 80, 040, 120, 160, 200, 120, 160, 320, 200, 320, 200, 400, 360, 280, 320, 440, 400, 320, 360, 480, 400, 480, 400, 520, 520, 440, 440, 560, 480, 560, 480, 560, 560, 480, 480, 600, 600, 480, 600, 520, 600, 520, 520, 600, 520, 600, 520, 600, 560, 480, 480, 560, 440, 520, 480, 560, 400, 520, 520, 400, 400, 480, 440, 360, 400, 320, 320, 400, 360, 240, 240, 320, 320, 160, 160, 240, 200, 80, 80, 160, 160, 000, 000, 80, 80, 880, 920, 960, 960, 800, 800, 880, 840, 680, 760, 680, 760, 560, 560, 640, 640, 440, 440, 520, 480, 320, 360, 280, 360, 200, 240, 120, 200, 120, 80, 000, 80, 000, 000, 840, 920, 840, 880, 680, 720, 760, 720, 520, 640, 520, 600, 400, 360, 480, 400, 240, 240, 320, 320, 80, 80, 160, 160, 920, 880, 960, 000, 720, 840, 720, 800, 600, 640, 600, 640, 480, 520, 400, 480, 280, 320, 240, 240, 320, 160, 120, 200, 80, 040, 960, 000, 920, 920, 800, 800, 920, 800, 680, 760, 640, 720, 520, 560, 520, 560, 400, 480, 400, 440, 280, 280, 360, 280, 160, 120, 240, 240, 040, 120, 040, 120, 960, 920, 000, 960, 840, 880, 800, 880, 760, 720, 840, 800, 680, 640, 720, 640, 720, 680, 600, 640, 560, 520, 560, 600, 480, 520, 440, 520, 480, 520, 400, 440, 480, 480, 400, 400, 440, 440, 360, 440, 360, 360, 440, 360, 400, 400, 360, 320, 440, 440, 360, 480, 360, 360, 440, 440, 400, 360, 480, 480, 400, 400, 480, 440, 520, 520, 440, 480, 600, 520, 560, 560, 600, 560, 640, 680, 600, 600, 720, 680, 760, 680, 760, 880, 760, 800, 840, 840, 960, 880, 960, 920, 040, 000, 040, 000, 160, 80, 160, 120, 280, 200, 280, 200, 400, 320, 400, 320, 520, 480, 440, 480, 640, 600, 680, 600, 760, 760, 720, 760, 920, 880, 960, 880, 040, 120, 000, 040, 200, 240, 160, 160, 280, 320, 400, 320, 480, 480, 560, 560, 480, 600, 720, 720, 640, 760, 880, 800, 920, 880, 040, 040, 960, 000, 200, 120, 200, 200, 320, 360, 280, 320, 520, 440, 520, 480, 680, 600, 720, 640, 840, 840, 760, 760, 960, 880, 000, 920, 160, 160, 040, 80, 240, 200, 280, 200, 440, 360, 400, 360, 560, 560, 480, 480, 640, 600, 680, 600, 760, 800, 720, 800, 760, 800, 960, 840, 920, 920, 000, 960, 000, 960, 120, 040, 120, 040, 200, 120, 240, 160, 320, 240, 320, 240, 400, 280, 360, 280, 400, 360, 440, 360, 480, 480, 400, 360, 520, 520, 400, 520, 440, 480, 560, 480, 600, 600, 480, 600, 480, 600, 520, 600, 520, 520, 560, 600, 520, 600, 520, 600, 520, 560, 480, 560, 480, 440, 560, 480, 520, 520, 400, 400, 520, 480, 360, 480, 360, 440, 320, 280, 360, 400, 240, 360, 240, 280, 160, 240, 160, 280, 120, 80, 200, 160, 000, 80, 000, 80, 920, 880, 960, 960, 760, 800, 880, 840, 720, 760, 640, 760, 560, 560, 600, 640, 520, 520, 400, 400, 520, 400, 280, 360, 280, 280, 160, 160, 240, 160, 000, 000, 120, 040, 840, 920, 840, 960, 680, 720, 800, 760, 560, 640, 560, 600, 400, 360, 480, 440, 240, 320, 200, 280, 80, 160, 040, 160, 920, 920, 960, 000, 760, 840, 760, 760, 640, 600, 680, 680, 520, 520, 400, 520, 360, 360, 280, 240, 360, 240, 80, 200, 80, 120, 960, 040, 960, 040, 840, 760, 880, 840, 640, 720, 680, 760, 560, 520, 600, 560, 400, 360, 480, 440, 280, 360, 240, 320, 160, 240, 120, 240, 040, 120, 040, 80, 960, 960, 920, 000, 840, 920, 800, 880, 800, 800, 720, 800, 680, 640, 760, 720, 680, 680, 600, 560, 640, 600, 520, 600, 480, 600, 480, 560, 480, 520, 400, 440, 480, 480, 400, 480, 400, 400, 480, 360, 440, 400, 400, 360, 440, 360, 440, 400, 360, 360, 440, 360, 440, 360, 480, 440, 360, 400, 480, 440, 520, 400, 480, 440, 520, 440, 520, 480, 560, 480, 600, 520, 640, 560, 640, 640, 600, 600, 720, 640, 720, 760, 680, 800, 760, 760, 840, 800, 920, 840, 960, 880, 040, 040, 960, 000, 160, 160, 80, 120, 280, 240, 160, 200, 360, 360, 280, 320, 520, 520, 440, 480, 600, 640, 560, 600, 760, 720, 800, 720, 880, 920, 840, 840, 000, 000, 120, 040, 120, 160, 240, 160, 240, 280, 360, 320, 400, 400, 560, 480, 560, 520, 720, 640, 760, 680, 880, 880, 760, 800, 040, 960, 040, 960, 200, 200, 120, 160, 360, 360, 280, 320, 560, 520, 440, 440, 680, 600, 680, 600, 840, 760, 840, 760, 000, 000, 920, 920, 120, 80, 160, 040, 240, 280, 200, 200, 360, 320, 440, 320, 440, 440, 560, 480, 560, 600, 680, 680, 600, 640, 800, 800, 680, 760, 880, 880, 840, 880, 040, 960, 040, 920, 120, 120, 040, 040, 200, 200, 120, 120, 240, 200, 320, 200, 360, 360, 280, 280, 400, 320, 440, 320, 480, 400, 480, 400, 520, 440, 520, 440, 560, 560, 440, 560, 480, 600, 480, 520, 560, 480, 600, 520, 600, 520, 600, 600, 520, 520, 600, 600, 480, 560, 480, 480, 560, 560, 440, 480, 560, 560, 440, 400, 520, 520, 360, 400, 480, 440, 320, 400, 280, 360, 240, 240, 360, 360, 160, 240, 160, 280, 120, 80, 160, 160, 000, 80, 000, 040, 920, 880, 000, 880, 960, 880, 800, 880, 760, 720, 640, 680, 760, 560, 640, 640, 560, 560, 400, 400, 520, 480, 280, 360, 280, 320, 120, 240, 160, 160, 000, 80, 000, 040, 880, 840, 960, 880, 680, 760, 680, 760, 560, 640, 520, 640, 400, 480, 400, 480, 280, 320, 240, 320, 120, 160, 040, 160, 000, 000, 880, 960, 840, 840, 720, 840, 720, 680, 560, 600, 680, 520, 440, 400, 480, 480, 240, 280, 360 };
float xn[len] = { 0.284, 0.292, 0.288, 0.276, 0.280, 0.276, 0.276, 0.268, 0.272, 0.264, 0.276, 0.260, 0.264, 0.256, 0.264, 0.256, 0.252, 0.264, 0.260, 0.248, 0.252, 0.248, 0.256, 0.244, 0.248, 0.244, 0.244, 0.248, 0.240, 0.248, 0.244, 0.236, 0.244, 0.236, 0.236, 0.244, 0.240, 0.236, 0.236, 0.244, 0.236, 0.244, 0.244, 0.236, 0.240, 0.240, 0.244, 0.236, 0.236, 0.248, 0.244, 0.236, 0.248, 0.244, 0.248, 0.240, 0.244, 0.248, 0.244, 0.256, 0.244, 0.256, 0.256, 0.248, 0.252, 0.260, 0.260, 0.252, 0.256, 0.264, 0.268, 0.260, 0.264, 0.276, 0.276, 0.268, 0.268, 0.284, 0.280, 0.288, 0.276, 0.288, 0.288, 0.296, 0.288, 0.300, 0.304, 0.296, 0.304, 0.296, 0.308, 0.316, 0.316, 0.308, 0.316, 0.324, 0.328, 0.320, 0.324, 0.340, 0.332, 0.340, 0.336, 0.352, 0.352, 0.344, 0.344, 0.364, 0.364, 0.356, 0.360, 0.376, 0.372, 0.380, 0.376, 0.392, 0.388, 0.392, 0.388, 0.408, 0.408, 0.404, 0.404, 0.424, 0.420, 0.424, 0.420, 0.432, 0.432, 0.440, 0.432, 0.452, 0.456, 0.448, 0.448, 0.468, 0.464, 0.476, 0.464, 0.480, 0.488, 0.480, 0.480, 0.492, 0.496, 0.504, 0.496, 0.508, 0.508, 0.520, 0.520, 0.512, 0.524, 0.536, 0.532, 0.540, 0.532, 0.552, 0.552, 0.544, 0.548, 0.572, 0.560, 0.568, 0.564, 0.584, 0.584, 0.576, 0.580, 0.600, 0.592, 0.600, 0.588, 0.612, 0.608, 0.616, 0.608, 0.628, 0.628, 0.624, 0.616, 0.640, 0.640, 0.632, 0.636, 0.656, 0.656, 0.648, 0.648, 0.668, 0.668, 0.660, 0.660, 0.676, 0.680, 0.672, 0.668, 0.688, 0.684, 0.692, 0.680, 0.700, 0.692, 0.704, 0.704, 0.692, 0.704, 0.712, 0.708, 0.704, 0.712, 0.716, 0.720, 0.712, 0.716, 0.732, 0.720, 0.732, 0.720, 0.740, 0.736, 0.728, 0.732, 0.744, 0.740, 0.732, 0.736, 0.748, 0.740, 0.748, 0.740, 0.752, 0.752, 0.744, 0.744, 0.756, 0.748, 0.756, 0.748, 0.756, 0.756, 0.748, 0.748, 0.760, 0.760, 0.748, 0.760, 0.752, 0.760, 0.752, 0.752, 0.760, 0.752, 0.760, 0.752, 0.760, 0.756, 0.748, 0.748, 0.756, 0.744, 0.752, 0.748, 0.756, 0.740, 0.752, 0.752, 0.740, 0.740, 0.748, 0.744, 0.736, 0.740, 0.732, 0.732, 0.740, 0.736, 0.724, 0.724, 0.732, 0.732, 0.716, 0.716, 0.724, 0.720, 0.708, 0.708, 0.716, 0.716, 0.700, 0.700, 0.708, 0.708, 0.688, 0.692, 0.696, 0.696, 0.680, 0.680, 0.688, 0.684, 0.668, 0.676, 0.668, 0.676, 0.656, 0.656, 0.664, 0.664, 0.644, 0.644, 0.652, 0.648, 0.632, 0.636, 0.628, 0.636, 0.620, 0.624, 0.612, 0.620, 0.612, 0.608, 0.600, 0.608, 0.600, 0.600, 0.584, 0.592, 0.584, 0.588, 0.568, 0.572, 0.576, 0.572, 0.552, 0.564, 0.552, 0.560, 0.540, 0.536, 0.548, 0.540, 0.524, 0.524, 0.532, 0.532, 0.508, 0.508, 0.516, 0.516, 0.492, 0.488, 0.496, 0.500, 0.472, 0.484, 0.472, 0.480, 0.460, 0.464, 0.460, 0.464, 0.448, 0.452, 0.440, 0.448, 0.428, 0.432, 0.424, 0.424, 0.432, 0.416, 0.412, 0.420, 0.408, 0.404, 0.396, 0.400, 0.392, 0.392, 0.380, 0.380, 0.392, 0.380, 0.368, 0.376, 0.364, 0.372, 0.352, 0.356, 0.352, 0.356, 0.340, 0.348, 0.340, 0.344, 0.328, 0.328, 0.336, 0.328, 0.316, 0.312, 0.324, 0.324, 0.304, 0.312, 0.304, 0.312, 0.296, 0.292, 0.300, 0.296, 0.284, 0.288, 0.280, 0.288, 0.276, 0.272, 0.284, 0.280, 0.268, 0.264, 0.272, 0.264, 0.272, 0.268, 0.260, 0.264, 0.256, 0.252, 0.256, 0.260, 0.248, 0.252, 0.244, 0.252, 0.248, 0.252, 0.240, 0.244, 0.248, 0.248, 0.240, 0.240, 0.244, 0.244, 0.236, 0.244, 0.236, 0.236, 0.244, 0.236, 0.240, 0.240, 0.236, 0.232, 0.244, 0.244, 0.236, 0.248, 0.236, 0.236, 0.244, 0.244, 0.240, 0.236, 0.248, 0.248, 0.240, 0.240, 0.248, 0.244, 0.252, 0.252, 0.244, 0.248, 0.260, 0.252, 0.256, 0.256, 0.260, 0.256, 0.264, 0.268, 0.260, 0.260, 0.272, 0.268, 0.276, 0.268, 0.276, 0.288, 0.276, 0.280, 0.284, 0.284, 0.296, 0.288, 0.296, 0.292, 0.304, 0.300, 0.304, 0.300, 0.316, 0.308, 0.316, 0.312, 0.328, 0.320, 0.328, 0.320, 0.340, 0.332, 0.340, 0.332, 0.352, 0.348, 0.344, 0.348, 0.364, 0.360, 0.368, 0.360, 0.376, 0.376, 0.372, 0.376, 0.392, 0.388, 0.396, 0.388, 0.404, 0.412, 0.400, 0.404, 0.420, 0.424, 0.416, 0.416, 0.428, 0.432, 0.440, 0.432, 0.448, 0.448, 0.456, 0.456, 0.448, 0.460, 0.472, 0.472, 0.464, 0.476, 0.488, 0.480, 0.492, 0.488, 0.504, 0.504, 0.496, 0.500, 0.520, 0.512, 0.520, 0.520, 0.532, 0.536, 0.528, 0.532, 0.552, 0.544, 0.552, 0.548, 0.568, 0.560, 0.572, 0.564, 0.584, 0.584, 0.576, 0.576, 0.596, 0.588, 0.600, 0.592, 0.616, 0.616, 0.604, 0.608, 0.624, 0.620, 0.628, 0.620, 0.644, 0.636, 0.640, 0.636, 0.656, 0.656, 0.648, 0.648, 0.664, 0.660, 0.668, 0.660, 0.676, 0.680, 0.672, 0.680, 0.676, 0.680, 0.696, 0.684, 0.692, 0.692, 0.700, 0.696, 0.700, 0.696, 0.712, 0.704, 0.712, 0.704, 0.720, 0.712, 0.724, 0.716, 0.732, 0.724, 0.732, 0.724, 0.740, 0.728, 0.736, 0.728, 0.740, 0.736, 0.744, 0.736, 0.748, 0.748, 0.740, 0.736, 0.752, 0.752, 0.740, 0.752, 0.744, 0.748, 0.756, 0.748, 0.760, 0.760, 0.748, 0.760, 0.748, 0.760, 0.752, 0.760, 0.752, 0.752, 0.756, 0.760, 0.752, 0.760, 0.752, 0.760, 0.752, 0.756, 0.748, 0.756, 0.748, 0.744, 0.756, 0.748, 0.752, 0.752, 0.740, 0.740, 0.752, 0.748, 0.736, 0.748, 0.736, 0.744, 0.732, 0.728, 0.736, 0.740, 0.724, 0.736, 0.724, 0.728, 0.716, 0.724, 0.716, 0.728, 0.712, 0.708, 0.720, 0.716, 0.700, 0.708, 0.700, 0.708, 0.692, 0.688, 0.696, 0.696, 0.676, 0.680, 0.688, 0.684, 0.672, 0.676, 0.664, 0.676, 0.656, 0.656, 0.660, 0.664, 0.652, 0.652, 0.640, 0.640, 0.652, 0.640, 0.628, 0.636, 0.628, 0.628, 0.616, 0.616, 0.624, 0.616, 0.600, 0.600, 0.612, 0.604, 0.584, 0.592, 0.584, 0.596, 0.568, 0.572, 0.580, 0.576, 0.556, 0.564, 0.556, 0.560, 0.540, 0.536, 0.548, 0.544, 0.524, 0.532, 0.520, 0.528, 0.508, 0.516, 0.504, 0.516, 0.492, 0.492, 0.496, 0.500, 0.476, 0.484, 0.476, 0.476, 0.464, 0.460, 0.468, 0.468, 0.452, 0.452, 0.440, 0.452, 0.436, 0.436, 0.428, 0.424, 0.436, 0.424, 0.408, 0.420, 0.408, 0.412, 0.396, 0.404, 0.396, 0.404, 0.384, 0.376, 0.388, 0.384, 0.364, 0.372, 0.368, 0.376, 0.356, 0.352, 0.360, 0.356, 0.340, 0.336, 0.348, 0.344, 0.328, 0.336, 0.324, 0.332, 0.316, 0.324, 0.312, 0.324, 0.304, 0.312, 0.304, 0.308, 0.296, 0.296, 0.292, 0.300, 0.284, 0.292, 0.280, 0.288, 0.280, 0.280, 0.272, 0.280, 0.268, 0.264, 0.276, 0.272, 0.268, 0.268, 0.260, 0.256, 0.264, 0.260, 0.252, 0.260, 0.248, 0.260, 0.248, 0.256, 0.248, 0.252, 0.240, 0.244, 0.248, 0.248, 0.240, 0.248, 0.240, 0.240, 0.248, 0.236, 0.244, 0.240, 0.240, 0.236, 0.244, 0.236, 0.244, 0.240, 0.236, 0.236, 0.244, 0.236, 0.244, 0.236, 0.248, 0.244, 0.236, 0.240, 0.248, 0.244, 0.252, 0.240, 0.248, 0.244, 0.252, 0.244, 0.252, 0.248, 0.256, 0.248, 0.260, 0.252, 0.264, 0.256, 0.264, 0.264, 0.260, 0.260, 0.272, 0.264, 0.272, 0.276, 0.268, 0.280, 0.276, 0.276, 0.284, 0.280, 0.292, 0.284, 0.296, 0.288, 0.304, 0.304, 0.296, 0.300, 0.316, 0.316, 0.308, 0.312, 0.328, 0.324, 0.316, 0.320, 0.336, 0.336, 0.328, 0.332, 0.352, 0.352, 0.344, 0.348, 0.360, 0.364, 0.356, 0.360, 0.376, 0.372, 0.380, 0.372, 0.388, 0.392, 0.384, 0.384, 0.400, 0.400, 0.412, 0.404, 0.412, 0.416, 0.424, 0.416, 0.424, 0.428, 0.436, 0.432, 0.440, 0.440, 0.456, 0.448, 0.456, 0.452, 0.472, 0.464, 0.476, 0.468, 0.488, 0.488, 0.476, 0.480, 0.504, 0.496, 0.504, 0.496, 0.520, 0.520, 0.512, 0.516, 0.536, 0.536, 0.528, 0.532, 0.556, 0.552, 0.544, 0.544, 0.568, 0.560, 0.568, 0.560, 0.584, 0.576, 0.584, 0.576, 0.600, 0.600, 0.592, 0.592, 0.612, 0.608, 0.616, 0.604, 0.624, 0.628, 0.620, 0.620, 0.636, 0.632, 0.644, 0.632, 0.644, 0.644, 0.656, 0.648, 0.656, 0.660, 0.668, 0.668, 0.660, 0.664, 0.680, 0.680, 0.668, 0.676, 0.688, 0.688, 0.684, 0.688, 0.704, 0.696, 0.704, 0.692, 0.712, 0.712, 0.704, 0.704, 0.720, 0.720, 0.712, 0.712, 0.724, 0.720, 0.732, 0.720, 0.736, 0.736, 0.728, 0.728, 0.740, 0.732, 0.744, 0.732, 0.748, 0.740, 0.748, 0.740, 0.752, 0.744, 0.752, 0.744, 0.756, 0.756, 0.744, 0.756, 0.748, 0.760, 0.748, 0.752, 0.756, 0.748, 0.760, 0.752, 0.760, 0.752, 0.760, 0.760, 0.752, 0.752, 0.760, 0.760, 0.748, 0.756, 0.748, 0.748, 0.756, 0.756, 0.744, 0.748, 0.756, 0.756, 0.744, 0.740, 0.752, 0.752, 0.736, 0.740, 0.748, 0.744, 0.732, 0.740, 0.728, 0.736, 0.724, 0.724, 0.736, 0.736, 0.716, 0.724, 0.716, 0.728, 0.712, 0.708, 0.716, 0.716, 0.700, 0.708, 0.700, 0.704, 0.692, 0.688, 0.700, 0.688, 0.696, 0.688, 0.680, 0.688, 0.676, 0.672, 0.664, 0.668, 0.676, 0.656, 0.664, 0.664, 0.656, 0.656, 0.640, 0.640, 0.652, 0.648, 0.628, 0.636, 0.628, 0.632, 0.612, 0.624, 0.616, 0.616, 0.600, 0.608, 0.600, 0.604, 0.588, 0.584, 0.596, 0.588, 0.568, 0.576, 0.568, 0.576, 0.556, 0.564, 0.552, 0.564, 0.540, 0.548, 0.540, 0.548, 0.528, 0.532, 0.524, 0.532, 0.512, 0.516, 0.504, 0.516, 0.500, 0.500, 0.488, 0.496, 0.484, 0.484, 0.472, 0.484, 0.472, 0.468, 0.456, 0.460, 0.468, 0.452, 0.444, 0.440, 0.448, 0.448, 0.424, 0.428, 0.436 };
float Xr[len];
float Xi[len];
int i, k, n, N = 1200;
float x[len];
double f[len];
float mean, total_sum = 0, fmax;
double fdelta;
i = len;
while(i --> 0) {
total_sum += xn[i];
}
mean = total_sum / len;
i = len;
while (i --> 0) {
x[i] = xn[i] - mean;
}
fmax = (double)(len - 1) / len / 0.5E-6;
fdelta = fmax / (len - 1);
i = len;
f[0] = 0;
for (i = 1; i < len; i++) {
f[i] = f[i - 1] + fdelta;
}
for (k = 0; k < len; k++) {
Xr[k] = 0;
Xi[k] = 0;
for (n = 0; n < len; n++) {
Xr[k]
= (Xr[k]
+ x[n] * cos(2 * 3.141592 * k * n / N));
Xi[k]
= (Xi[k]
- x[n] * sin(2 * 3.141592 * k * n / N));
}
//printf("(%f) + j(%f)\n", Xr[k], Xi[k]);
printf("%f %f\n", sqrt(Xr[k] * Xr[k] + Xi[k] * Xi[k]), f[k]);
}
}
// Driver Code
int main()
{
calculateDFT();
return 0;
}
Star Strider
2023-1-30
Unfortunately, no.
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.
Dan Richter
2023-1-30
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.
Star Strider
2023-1-30
When I ran:
coder.Dictionary
the result was:
'coder.Dictionary' requires Embedded Coder.
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.
.
Dan Richter
2023-1-30
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++.
Star Strider
2023-1-30
As always, my pleasure!
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.
It won’t work online here. At least now we know.
.
Dan Richter
2023-2-3
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?
Star Strider
2023-2-3
Modifying it requires very few changes —
s = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1283561/ADC_5k_1024.txt')
s = 1024×1
1.2000
1.1600
1.0800
1.0200
0.9400
0.9700
1.0000
1.0900
1.1700
1.2900
L = numel(s) % Vector Length
L = 1024
Ts = 5.81E-6; % Sampling Interval
t = linspace(0, L-1, L)*Ts; % Create Time Vector
figure
plot(t, s)
grid
xlabel('Time (s)')
ylabel('Amplitude (V)')
% Ts = mean(diff(t));
Fs = 1/Ts;
Fn = Fs/2;
% L = size(s,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)
Frq = 4.8744e+03
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
My apologies for the delay — off doing other things for a few minutes.
.
更多回答(2 个)
Dan Richter
2023-1-26
But how to scale the time vector to get frequency vector correctly? It looks like that time step for 1 200 data points is 500 µs.
Picture is my DFT calculation in C++ VS2022:
C++ code:
// https://www.geeksforgeeks.org/discrete-fourier-transform-and-its-inverse-using-c/
#include <iostream>
#include <math.h>
#include <stdio.h>
const int len = 1200;
// Function to calculate the DFT
void calculateDFT()
{
int xn[len] = { 840, 920, 880, 760, 800, 760, 760, 680, 720, 640, 760, 600, 640, 560, 640, 560, 520, 640, 600, 480, 520, 480, 560, 440, 480, 440, 440, 480, 400, 480, 440, 360, 440, 360, 360, 440, 400, 360, 360, 440, 360, 440, 440, 360, 400, 400, 440, 360, 360, 480, 440, 360, 480, 440, 480, 400, 440, 480, 440, 560, 440, 560, 560, 480, 520, 600, 600, 520, 560, 640, 680, 600, 640, 760, 760, 680, 680, 840, 800, 880, 760, 880, 880, 960, 880, 000, 040, 960, 040, 960, 80, 160, 160, 80, 160, 240, 280, 200, 240, 400, 320, 400, 360, 520, 520, 440, 440, 640, 640, 560, 600, 760, 720, 800, 760, 920, 880, 920, 880, 80, 80, 040, 040, 240, 200, 240, 200, 320, 320, 400, 320, 520, 560, 480, 480, 680, 640, 760, 640, 800, 880, 800, 800, 920, 960, 040, 960, 80, 80, 200, 200, 120, 240, 360, 320, 400, 320, 520, 520, 440, 480, 720, 600, 680, 640, 840, 840, 760, 800, 000, 920, 000, 880, 120, 80, 160, 80, 280, 280, 240, 160, 400, 400, 320, 360, 560, 560, 480, 480, 680, 680, 600, 600, 760, 800, 720, 680, 880, 840, 920, 800, 000, 920, 040, 040, 920, 040, 120, 80, 040, 120, 160, 200, 120, 160, 320, 200, 320, 200, 400, 360, 280, 320, 440, 400, 320, 360, 480, 400, 480, 400, 520, 520, 440, 440, 560, 480, 560, 480, 560, 560, 480, 480, 600, 600, 480, 600, 520, 600, 520, 520, 600, 520, 600, 520, 600, 560, 480, 480, 560, 440, 520, 480, 560, 400, 520, 520, 400, 400, 480, 440, 360, 400, 320, 320, 400, 360, 240, 240, 320, 320, 160, 160, 240, 200, 80, 80, 160, 160, 000, 000, 80, 80, 880, 920, 960, 960, 800, 800, 880, 840, 680, 760, 680, 760, 560, 560, 640, 640, 440, 440, 520, 480, 320, 360, 280, 360, 200, 240, 120, 200, 120, 80, 000, 80, 000, 000, 840, 920, 840, 880, 680, 720, 760, 720, 520, 640, 520, 600, 400, 360, 480, 400, 240, 240, 320, 320, 80, 80, 160, 160, 920, 880, 960, 000, 720, 840, 720, 800, 600, 640, 600, 640, 480, 520, 400, 480, 280, 320, 240, 240, 320, 160, 120, 200, 80, 040, 960, 000, 920, 920, 800, 800, 920, 800, 680, 760, 640, 720, 520, 560, 520, 560, 400, 480, 400, 440, 280, 280, 360, 280, 160, 120, 240, 240, 040, 120, 040, 120, 960, 920, 000, 960, 840, 880, 800, 880, 760, 720, 840, 800, 680, 640, 720, 640, 720, 680, 600, 640, 560, 520, 560, 600, 480, 520, 440, 520, 480, 520, 400, 440, 480, 480, 400, 400, 440, 440, 360, 440, 360, 360, 440, 360, 400, 400, 360, 320, 440, 440, 360, 480, 360, 360, 440, 440, 400, 360, 480, 480, 400, 400, 480, 440, 520, 520, 440, 480, 600, 520, 560, 560, 600, 560, 640, 680, 600, 600, 720, 680, 760, 680, 760, 880, 760, 800, 840, 840, 960, 880, 960, 920, 040, 000, 040, 000, 160, 80, 160, 120, 280, 200, 280, 200, 400, 320, 400, 320, 520, 480, 440, 480, 640, 600, 680, 600, 760, 760, 720, 760, 920, 880, 960, 880, 040, 120, 000, 040, 200, 240, 160, 160, 280, 320, 400, 320, 480, 480, 560, 560, 480, 600, 720, 720, 640, 760, 880, 800, 920, 880, 040, 040, 960, 000, 200, 120, 200, 200, 320, 360, 280, 320, 520, 440, 520, 480, 680, 600, 720, 640, 840, 840, 760, 760, 960, 880, 000, 920, 160, 160, 040, 80, 240, 200, 280, 200, 440, 360, 400, 360, 560, 560, 480, 480, 640, 600, 680, 600, 760, 800, 720, 800, 760, 800, 960, 840, 920, 920, 000, 960, 000, 960, 120, 040, 120, 040, 200, 120, 240, 160, 320, 240, 320, 240, 400, 280, 360, 280, 400, 360, 440, 360, 480, 480, 400, 360, 520, 520, 400, 520, 440, 480, 560, 480, 600, 600, 480, 600, 480, 600, 520, 600, 520, 520, 560, 600, 520, 600, 520, 600, 520, 560, 480, 560, 480, 440, 560, 480, 520, 520, 400, 400, 520, 480, 360, 480, 360, 440, 320, 280, 360, 400, 240, 360, 240, 280, 160, 240, 160, 280, 120, 80, 200, 160, 000, 80, 000, 80, 920, 880, 960, 960, 760, 800, 880, 840, 720, 760, 640, 760, 560, 560, 600, 640, 520, 520, 400, 400, 520, 400, 280, 360, 280, 280, 160, 160, 240, 160, 000, 000, 120, 040, 840, 920, 840, 960, 680, 720, 800, 760, 560, 640, 560, 600, 400, 360, 480, 440, 240, 320, 200, 280, 80, 160, 040, 160, 920, 920, 960, 000, 760, 840, 760, 760, 640, 600, 680, 680, 520, 520, 400, 520, 360, 360, 280, 240, 360, 240, 80, 200, 80, 120, 960, 040, 960, 040, 840, 760, 880, 840, 640, 720, 680, 760, 560, 520, 600, 560, 400, 360, 480, 440, 280, 360, 240, 320, 160, 240, 120, 240, 040, 120, 040, 80, 960, 960, 920, 000, 840, 920, 800, 880, 800, 800, 720, 800, 680, 640, 760, 720, 680, 680, 600, 560, 640, 600, 520, 600, 480, 600, 480, 560, 480, 520, 400, 440, 480, 480, 400, 480, 400, 400, 480, 360, 440, 400, 400, 360, 440, 360, 440, 400, 360, 360, 440, 360, 440, 360, 480, 440, 360, 400, 480, 440, 520, 400, 480, 440, 520, 440, 520, 480, 560, 480, 600, 520, 640, 560, 640, 640, 600, 600, 720, 640, 720, 760, 680, 800, 760, 760, 840, 800, 920, 840, 960, 880, 040, 040, 960, 000, 160, 160, 80, 120, 280, 240, 160, 200, 360, 360, 280, 320, 520, 520, 440, 480, 600, 640, 560, 600, 760, 720, 800, 720, 880, 920, 840, 840, 000, 000, 120, 040, 120, 160, 240, 160, 240, 280, 360, 320, 400, 400, 560, 480, 560, 520, 720, 640, 760, 680, 880, 880, 760, 800, 040, 960, 040, 960, 200, 200, 120, 160, 360, 360, 280, 320, 560, 520, 440, 440, 680, 600, 680, 600, 840, 760, 840, 760, 000, 000, 920, 920, 120, 80, 160, 040, 240, 280, 200, 200, 360, 320, 440, 320, 440, 440, 560, 480, 560, 600, 680, 680, 600, 640, 800, 800, 680, 760, 880, 880, 840, 880, 040, 960, 040, 920, 120, 120, 040, 040, 200, 200, 120, 120, 240, 200, 320, 200, 360, 360, 280, 280, 400, 320, 440, 320, 480, 400, 480, 400, 520, 440, 520, 440, 560, 560, 440, 560, 480, 600, 480, 520, 560, 480, 600, 520, 600, 520, 600, 600, 520, 520, 600, 600, 480, 560, 480, 480, 560, 560, 440, 480, 560, 560, 440, 400, 520, 520, 360, 400, 480, 440, 320, 400, 280, 360, 240, 240, 360, 360, 160, 240, 160, 280, 120, 80, 160, 160, 000, 80, 000, 040, 920, 880, 000, 880, 960, 880, 800, 880, 760, 720, 640, 680, 760, 560, 640, 640, 560, 560, 400, 400, 520, 480, 280, 360, 280, 320, 120, 240, 160, 160, 000, 80, 000, 040, 880, 840, 960, 880, 680, 760, 680, 760, 560, 640, 520, 640, 400, 480, 400, 480, 280, 320, 240, 320, 120, 160, 040, 160, 000, 000, 880, 960, 840, 840, 720, 840, 720, 680, 560, 600, 680, 520, 440, 400, 480, 480, 240, 280, 360 };;
float Xr[len];
float Xi[len];
int i, k, n, N = 0;
printf("Enter the number of "
"points in the DFT: ");
scanf_s("%d", &N);
for (k = 0; k < len; k++) {
Xr[k] = 0;
Xi[k] = 0;
for (n = 0; n < len; n++) {
Xr[k]
= (Xr[k]
+ xn[n] * cos(2 * 3.141592 * k * n / N));
Xi[k]
= (Xi[k]
- xn[n] * sin(2 * 3.141592 * k * n / N));
}
//printf("(%f) + j(%f)\n", Xr[k], Xi[k]);
printf("%f\n", sqrt(Xr[k] * Xr[k] + Xi[k] * Xi[k]));
}
}
// Driver Code
int main()
{
calculateDFT();
return 0;
}
Paul
2023-1-27
By my calculations, if that sine wave is supposed to be 4.82 kHz, then the sampling frequency must be
Fs = 1928000 Hz
and the sampling period
Ts = 1/1928000 % sec
Ts = 5.1867e-07
which isn't close to 2 micro-sec.
5 个评论
Dan Richter
2023-1-27
The sine wave is for sure 4.82 kHz which is 4,820 Hz ;-). Period is 207 µs and in zomm you can see five 2 µs steps in 10 µs.
There is no sampling period. The data of the generated sine wave are only copied from the oscilloscope to Excel CSV file.
Dan Richter
2023-1-27
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.
Paul
2023-1-27
All that information would have been helpful from the start.
If the data is sampled at 0.5 microsec (not 2 microsec as implied in the Question) then we have
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1275525/CSV_5k.csv');
x = T1.Var2 - mean(T1.Var2);
Ts = 0.5e-6;
f = (0:(numel(x)-1))/numel(x)/Ts; % Hz
X = fft(x);
plot(f,abs(X))
xlim([0 10000])
xline(4.82e3)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)