数据的傅里叶逆变换没有给出正确的振幅
inverse fourier transform of data does not give correct amplitude
我正在尝试使用 Matlab 计算某些数据的傅里叶逆变换。我从频域中的原始数据开始,想要可视化时域中的数据。这是我的 MWE:
a = 1.056;
% frequency of data (I cannot change this)
w = linspace(-100,100,1e6);
L = length(w); % no. sample points
ts = L/1000; % time sampling
Ts = ts/L; % sampling rate
Fs = 1/Ts; % sampling freq
t = (-L/2:L/2-1)*ts/L; % time
Y = sqrt(pi/a)*exp(-w.^2/(4*a)); % my data
yn = Fs*ifftshift(ifft(fftshift(Y(end:-1:1)))) % numerical soln
ya = exp(-a*t.^2); % analytic solution
figure; hold on
plot(t,yn,'.')
plot(t,ya,'-')
xlabel('time, t')
legend('numerical','analytic')
xlim([-5,5])
我改编了的代码,但是幅度太大:
你能告诉我我做错了什么吗?
您的代码存在三个问题:
你定义 ts = L/1000
然后计算 Fs
,这给你 1000。但是 Fs
是由你的 w
数组给出的设置:w
的全范围是2*pi*Fs
:
Fs = -w(1)/pi; % sampling freq
Ts = 1/Fs; % sampling rate
或者,等效地,设置 Fs = mean(diff(w))*L / (2*pi)
定义了w
,但不包括0。就像你仔细定义t
,把0放在正确的地方,你也应该定义w
在正确的位置包含 0。一种简单的方法是再定义一个值,然后删除最后一个值:
w = linspace(-100,100,1e6+1);
w(end) = [];
如果您的输入数据不包含 0 频率,您应该对其重新采样以使其包含。 DFT (FFT) 期望频率为 0。
您正在使用 ifftshift
和 fftshift
颠倒:fftshift
将原点从最左边的数组元素移到中间,而 ifftshift
将它从中间移到左边。您用中间的原点定义信号,因此您需要在其上使用 ifftshift
将原点移动到 fft
和 ifft
函数期望的位置。在这两个函数的输出上使用 fftshift
将原点居中显示。因为您的数据大小均匀,所以这两个函数做的事情完全相同,您不会注意到其中的区别。但是,如果数据的大小是奇数,您就会看到差异。
下面的代码给出了一个完美的匹配:
a = 1.056;
% frequency of data (I cannot change this)
w = linspace(-100,100,1e6+1); w(end) = [];
L = length(w); % no. sample points
Fs = -w(1)/pi; % sampling freq
Ts = 1/Fs; % sampling rate
t = (-L/2:L/2-1)*Ts; % time
Y = sqrt(pi/a)*exp(-w.^2/(4*a)); % my data
yn = Fs*fftshift(ifft(ifftshift(Y(end:-1:1)))); % numerical soln
ya = exp(-a*t.^2); % analytic solution
figure; hold on
plot(t,yn,'.')
plot(t,ya,'-')
xlabel('time, t')
legend('numerical','analytic')
xlim([-5,5])
我正在尝试使用 Matlab 计算某些数据的傅里叶逆变换。我从频域中的原始数据开始,想要可视化时域中的数据。这是我的 MWE:
a = 1.056;
% frequency of data (I cannot change this)
w = linspace(-100,100,1e6);
L = length(w); % no. sample points
ts = L/1000; % time sampling
Ts = ts/L; % sampling rate
Fs = 1/Ts; % sampling freq
t = (-L/2:L/2-1)*ts/L; % time
Y = sqrt(pi/a)*exp(-w.^2/(4*a)); % my data
yn = Fs*ifftshift(ifft(fftshift(Y(end:-1:1)))) % numerical soln
ya = exp(-a*t.^2); % analytic solution
figure; hold on
plot(t,yn,'.')
plot(t,ya,'-')
xlabel('time, t')
legend('numerical','analytic')
xlim([-5,5])
我改编了
你能告诉我我做错了什么吗?
您的代码存在三个问题:
你定义
ts = L/1000
然后计算Fs
,这给你 1000。但是Fs
是由你的w
数组给出的设置:w
的全范围是2*pi*Fs
:Fs = -w(1)/pi; % sampling freq Ts = 1/Fs; % sampling rate
或者,等效地,设置
Fs = mean(diff(w))*L / (2*pi)
定义了w
,但不包括0。就像你仔细定义t
,把0放在正确的地方,你也应该定义w
在正确的位置包含 0。一种简单的方法是再定义一个值,然后删除最后一个值:w = linspace(-100,100,1e6+1); w(end) = [];
如果您的输入数据不包含 0 频率,您应该对其重新采样以使其包含。 DFT (FFT) 期望频率为 0。
您正在使用
ifftshift
和fftshift
颠倒:fftshift
将原点从最左边的数组元素移到中间,而ifftshift
将它从中间移到左边。您用中间的原点定义信号,因此您需要在其上使用ifftshift
将原点移动到fft
和ifft
函数期望的位置。在这两个函数的输出上使用fftshift
将原点居中显示。因为您的数据大小均匀,所以这两个函数做的事情完全相同,您不会注意到其中的区别。但是,如果数据的大小是奇数,您就会看到差异。
下面的代码给出了一个完美的匹配:
a = 1.056;
% frequency of data (I cannot change this)
w = linspace(-100,100,1e6+1); w(end) = [];
L = length(w); % no. sample points
Fs = -w(1)/pi; % sampling freq
Ts = 1/Fs; % sampling rate
t = (-L/2:L/2-1)*Ts; % time
Y = sqrt(pi/a)*exp(-w.^2/(4*a)); % my data
yn = Fs*fftshift(ifft(ifftshift(Y(end:-1:1)))); % numerical soln
ya = exp(-a*t.^2); % analytic solution
figure; hold on
plot(t,yn,'.')
plot(t,ya,'-')
xlabel('time, t')
legend('numerical','analytic')
xlim([-5,5])