Matlab FFT differs from theory and oscilloscope

Hello together,
I am currently trying to reproduce simple results from my oscilloscope-FFT. I generated a simple sine wave signal with 300 MHz Frequency and used a simple rectangular window: the voltage data u1 range in time from -10 Microseconds to 10 Microseconds.
Why does the Matlab FFT show the windowing effect and the integral and the oscilloscope not? I am kind of worried since I rather trust the oscilloscope than I trust the Matlab FFT... Have I forgot something essential?
I am running out of ideas and would be happy for some help. The code:
close all
clear
% Import Oscilloscope Data
modus = "sinus";
m = importdata("C1--" + modus + "--00000.dat");
time = m(1:end-1,1);
N = size(time,1);
u1 = m(1:end-1,2);
% Determine Frequencies
DeltaT = (time(size(time,1))-time(1))/(N-1);
Fs = 1/DeltaT;
df = Fs/(N-1);
f_four = (0:df:Fs/2)';
% Theoretical DFT
B1 = zeros(size(f_four,1),1);
for f=1:size(f_four,1)
disp(f)
B1(f) = 2*sum(u1.*exp(-1i*time*f_four(f)*2*pi)/N);
end
% Matlab FFT
Bh2 = fft(u1)/N;
Bh2 = fftshift(Bh2);
B2 = 2*Bh2((N+1)/2:end);
% Plot
f_compare = importdata("F1--" + modus + "-rechteck--00000.dat"); % Oscilloscope FFT
figure(1)
plot(f_four/1e06, 20*log10(abs(B1)), f_four/1e06, 20*log10(abs(B2)), f_compare(:,1)/1e06, 20*log10(f_compare(:,2)));
set(gca, 'FontSize', 14);
axis([250 350 -300 0]);
legend('Fourier Integral', 'Matlab-FFt', 'Oscilloscope', 'Location', 'southwest');
xlabel('$\textit{f}$ in MHz', 'Interpreter', 'Latex');
ylabel('dBV');
Thank you in advance!

4 Comments

Your "DFT" looks strange.
B1(f) = 2*sum(u1.*exp(-1i*time*f_four(f)*2*pi)/N);
The Discrete Fourier Transform should not involve continuous time and frequency. That's why it is discrete.
what happens if you divide Bh2 by 2 instead of multiplying it by 2? In the other words, is it the problem of wrong normalization or the shape of the curve is also differ from the theoretical result (after normalization)?
To the first comment:
Okay, interesting, when using the indizes like
timediscrete = linspace(-(N-1)/2,(N-1)/2, N)';
for f=1:size(f_four,1)
disp(f)
B1(f) = 2*sum(u1.*exp(-1i*timediscrete*f*2*pi/N)/N);
end
I get the same result as the Matlab FFT. But I am still questioning myself whether the oscilloscope is right or this function...
To the second comment:
The Factor of 2 is necessary since the peak coincides only with a factor of 2. Therefore it should not be a problem of wrong normalization, the shape of the curve is broader and can't be scaled to the shape of the other curve. But it is interesting that the total energy of all three spectra is the same!
G A
G A on 12 Apr 2021
Edited: G A on 12 Apr 2021
I thought may be difference comes from the different number of points when you calculate theoretically and when you do fft(), i.e. length(u1) is twice larger than length(f_four).

Sign in to comment.

 Accepted Answer

Is the oscilloscope computing the FFT, or is it trying to compute a continuous Fourier transform approximation? If the latter, it should differ from the FFT by a factor of DeltaT.

6 Comments

I've found the difference. The exponential term differs!
MATLAB:
Oscilloscope:
I believe that it should be N-1 ! Why? You've got DeltaT = TotalTime / (N-1) and DeltaF = 1/TotalTime since there are (N-1) spaces between the N points. If you now want to compute with the indizes in the discrete form, use Frequency = f * DeltaF and Time = t * DeltaT. The denominator N-1 remains.
I believe that it should be N-1 ! Why? You've got DeltaT = TotalTime / (N-1) and DeltaF = 1/TotalTime
No, in an N-point DFT, the frequency sampling is given by
DeltaF=1/(N*DeltaT)
and N*DeltaT is the time between the first sample and its next periodic repetition.
Matlab's definition is the one I've always known and also agrees with,
G A
G A on 14 Apr 2021
Edited: G A on 14 Apr 2021
Here for oscilloscope it is N, not N-1: https://www.edn.com/fft-equations-and-history/
And also in your calculation, which gives the result close to the one from your oscilloscope, you are using N.
Thanks for your answers!
Maybe you can have a look whether I get it right? If I observe a signal with duration e.g. T = 10 s and I want to use N = 5 points for the DFT. I know that the separation of the time steps is DT = 2.5 s since there are N-1 spaces between N points. Therefore we need DT = T / (N-1).
The sampling frequency is Fs = 1 / DT = 0.4 Hz.
The frequency step is DeltaF = Fs / N = 4/5 Hz = 0.08 Hz.
The frequency vector should be [-0.16 -0.08 0 0.08 0.16].
I checked it. Now I get the same result as the Matlab FFT in the case I use time and frequency instead of indices as terms in the exponential. It remains the question what the oscilloscope did to the data...
Maybe you can have a look whether I get it right?
Here is how I would set things up,
N=5;
T=10;
dT=T/(N-1) %time sampling interval
dT = 2.5000
dF=1/dT/N %frequency sampling interval
dF = 0.0800
NormalizedAxis= (0:N-1) -ceil((N-1)/2);
time_axis=NormalizedAxis*dT
time_axis = 1×5
-5.0000 -2.5000 0 2.5000 5.0000
frequency_axis=NormalizedAxis*dF
frequency_axis = 1×5
-0.1600 -0.0800 0 0.0800 0.1600

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!