示例
要记录功能,使用使用您的功能的示例脚本通常会很有帮助。然后,可以使用Matlab中的发布功能来生成带有嵌入式图片,代码,链接等的帮助文件。可以在此处找到用于记录代码的语法。
函数该函数在Matlab中使用“校正” FFT。
function out_sig = myfft(in_sig)
out_sig = fftshift(fft(ifftshift(in_sig)));
end
示例脚本这是一个单独的脚本,解释了输入,输出,并提供了一个示例,说明为什么需要进行校正。感谢此函数的原始作者Wu,Kan。
%% myfft
% This function uses the “proper” fft in matlab. Note that the fft needs to
% be multiplied by dt to have physical significance.
% For a full description of why the FFT should be taken like this, refer
% to: Why_use_fftshift(fft(fftshift(x)))__in_Matlab.pdf included in the
% help folder of this code. Additional information can be found:
%
%
%% Inputs
% *in_sig* – 1D signal
%
%% Outputs
% *out_sig* – corrected FFT of *in_sig*
%
%% Examples
% Generate a signal with an analytical solution. The analytical solution is
% then compared to the fft then to myfft. This example is a modified
% version given by Wu, Kan given in the link aboce.
%%
% Set parameters
fs = 500; %sampling frequency
dt = 1/fs; %time step
T=1; %total time window
t = -T/2:dt:T/2-dt; %time grids
df = 1/T; %freq step
Fmax = 1/2/dt; %freq window
f=-Fmax:df:Fmax-df; %freq grids, not used in our examples, could be used by plot(f, X)
%%
% Generate Gaussian curve
Bx = 10; A = sqrt(log(2))/(2*pi*Bx); %Characteristics of Gaussian curve
x = exp(-t.^2/2/A^2); %Create Gaussian Curve
%%
% Generate Analytical solution
Xan = A*sqrt(2*pi)*exp(-2*pi^2*f.^2*A^2); %X(f), real part of the analytical Fourier transform of x(t)
%%
% Take FFT and corrected FFT then compare
Xfft = dt *fftshift(fft(x)); %FFT
Xfinal = dt * myfft(x); %Corrected FFT
hold on
plot(f,Xan);
plot(f,real(Xfft));
plot(f,real(Xfinal),’ro’);
title(‘Comparison of Corrected and Uncorrected FFT’);
legend(‘Analytical Solution’,’Uncorrected FFT’,’Corrected FFT’);
xlabel(‘Frequency’); ylabel(‘Amplitude’);
DT = max(f) – min(f);
xlim([-DT/4,DT/4]);
输出可以在下面的imageSimple函数文档中突出显示的“发布”选项卡下找到“发布”选项。
Matlab将运行脚本,并保存显示的图像以及命令行生成的文本。可以将输出保存为许多不同类型的格式,包括HTML,Latex和PDF。
上面给出的示例脚本的输出可以在下图中看到。