且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matlab没有绘制确切的傅立叶信号

更新时间:2023-02-27 14:38:26

fft函数采用单位时间步长.为了校正非单位时间步长,您需要根据奈奎斯特速率定义频率分量.以下代码以正确的频率轴绘制fft的大小.

clc;
clear all;
close all;
x=1:0.001:10;
%     ^ this is your sampling time step
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);

% bounds of fourier transform based on sampling rate
Fs = 1/0.001;
ff = linspace(-Fs/2,Fs/2,numel(x));

F1 = fftshift(fft(f1)/numel(x));
F2 = fftshift(fft(f2)/numel(x));
F3 = fftshift(fft(f3)/numel(x));

figure();
plot(ff,abs(F1),'-r'); hold on;
plot(ff,abs(F2),'-b');
plot(ff,abs(F3),'-k');

要在评论中回答OP的问题.

以归一化的频率单位讲话(假设采样率为1). fft函数将频率响应从0弧度返回到2*pi弧度,但是由于某些信号处理属性以及执行FFT时解释离散信号的方式,该信号实际上是周期性的,因此pi可以2*pi部分与-pi0部分相同.为了显示以直流分量(频率为0)为中心的曲线,我们使用fftshift,该圆周移动等于fft返回的数据上信号长度的1/2.拿ifft之前,请确保使用ifftshift将其放回正确的位置.

Edit2:归一化项(/numel(x))对于使用离散傅立叶变换估计连续时间傅立叶变换是必需的.我不记得确切的数学原因,而是关于MIT OpenCourseWare的讲座.

I'm trying to plot a simple signal in fourier domain using Matlab. It's not plotting the correct signal. Here is my code:

clc;
clear all;
close all;
x=1:0.001:10;
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);
f=f1+f2+f3;
plot(2*pi*x,fft(f1));
figure
plot(x,fft(f1));

I've expected a peak at 10 since the frequency is 10. But it is giving a peak at some other point

Here are the two plot images:

This is the image for plot(x,fft(f1))

This is the image for plot(2*pi*x,fft(f1))

It is not showing the peak at 10.I even tried using abs(fft(f1)). No luck :/

Isn't it the correct way to plot signal in fourier domain?

The fft function assumes unit time step. In order to correct for non unit time step you need to define the frequency component based on the nyquist rate. The following code plots the magnitude of the fft with the correct frequency axis.

clc;
clear all;
close all;
x=1:0.001:10;
%     ^ this is your sampling time step
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);

% bounds of fourier transform based on sampling rate
Fs = 1/0.001;
ff = linspace(-Fs/2,Fs/2,numel(x));

F1 = fftshift(fft(f1)/numel(x));
F2 = fftshift(fft(f2)/numel(x));
F3 = fftshift(fft(f3)/numel(x));

figure();
plot(ff,abs(F1),'-r'); hold on;
plot(ff,abs(F2),'-b');
plot(ff,abs(F3),'-k');

Edit: To answer OPs question in the comment.

Speaking in normalized frequency units (assuming sampling rate of 1). The fft function returns the frequency response from 0 to 2*pi radians, but due to some signal processing properties and the way that discrete signals are interpreted when performing an FFT, the signal is actually periodic so the pi to 2*pi section is identical to the -pi to 0 section. To display the plot with the DC component (0 frequency) in the center we use fftshift which does a circular shift equal to 1/2 the length of the signal on the data returned by fft. Before you take the ifft make sure you use ifftshift to put it back in the right place.

Edit2: The normalization term (/numel(x)) is necessary to estimate the continuous time fourier transform using the discrete fourier transform. I don't remember the precise mathematical reason off the top of my head but the examples in the MATLAB documentation also imply the necessity of this normalization.

Edit 3: The original link that I had is down. I may come back to add a more detailed answer but in the mean time I definitely recommend that anyone interested in understanding the relationship between the fundamentals of the FS, FT, DTFT, and DFT watch Professor Oppenheim's hilariously old, but amazingly informative and straightforward lectures on MIT OpenCourseWare.