How to interpret results of FFT/DFT?

5 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 26 Jan 2023
Edited: Star Strider on 27 Jan 2023
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')
T1 = 1200×2 table
Var1 Var2 ____ _____ 0 0.284 1 0.292 2 0.288 3 0.276 4 0.28 5 0.276 6 0.276 7 0.268 8 0.272 9 0.264 10 0.276 11 0.26 12 0.264 13 0.256 14 0.264 15 0.256
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)
Frq = 4.8828e+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
EDIT — (27 Jan 2023 at 13:50)
Scaled ‘t’ to be appropriate by multiplying it by so that the sampling interval ‘Ts’ is .
.
  14 Comments
Dan Richter
Dan Richter on 3 Feb 2023
You are expert, thank you very much. I started to learn MATLAB.

Sign in to comment.

More Answers (2)

Dan Richter
Dan Richter on 26 Jan 2023
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
Paul on 27 Jan 2023
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 Comments
Paul
Paul on 27 Jan 2023
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)
Dan Richter
Dan Richter on 27 Jan 2023
Thank you. This is I was looking for.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!