The error of MATLAB's own FFT compared to DFT

problem found

Recently, a problem was suddenly found in the simulated FFT transformation. The discrete Fourier transform of an ideal sinusoidal signal should be a single pulse spectrum, as shown in the figure below.

As a result, the results of matlab's fft processing show that it does not seem to be true, and it presents a Lorentzian line type, as shown in the figure below.

DFT comparison written by myself

Especially under the condition that the signal frequency is an integer, the error is extraordinarily large.
insert image description here

It is also different when the signal frequency is a fraction. . . . Is it because my MATLAB is a cracked version?
insert image description here

The procedure is as follows, welcome to correct

clc;clear;
close all;

T = 1;
fs = 3e3;
len = fs*T;
t = linspace(0,T,len);
I = cos(2*pi*510*t);        % 正弦信号

% MATLAB's FFT
real0 = 2*abs(fft(I))/len;        % fft
freq0 = linspace(0,len-1,len);

% my DFT
real1 = zeros(1,len);
for m = 1:len
    mid = mean(I.*exp(-2i*pi*(m-1)*t));        % 傅里叶变换的公式
    real1(m) = 2*abs(mid);
end
freq1 = linspace(0,len-1,len);

figure,plot(freq1,10*log10(real1),'g',freq0,10*log10(real0),'k');
legend('DFT','FFT');

I asked a classmate to take a look and said that the coordinate base did not match, just change the time coordinate to the following code, the result is as follows, Niubility! !

t = linspace(0,T-1/fs,len);

insert image description here

Guess you like

Origin blog.csdn.net/ruredfive/article/details/126128101
FFT