【发布时间】:2019-12-28 05:43:52
【问题描述】:
我正在使用 python 中的离散傅立叶变换。在我的 python 代码中,如何在复平面上绘制两个傅立叶系数。
我已经看到在 matlab 中他们使用以下代码来执行此操作,他们使用 dsearchn 来绘制它,如下所示:
% create the signal
srate = 1000; % hz
time = 0:1/srate:2; % time vector in seconds
pnts = length(time); % number of time points
signal = 2.5 * sin( 2*pi*4*time ) ...
+ 1.5 * sin( 2*pi*6.5*time );
% prepare the Fourier transform
fourTime = (0:pnts-1)/pnts;
fCoefs = zeros(size(signal));
% compute frequencies vector
hz = linspace(0,srate/2,floor(pnts/2)+1);
%% plot two Fourier coefficients
coefs2plot = dsearchn(hz',[4 4.5]');
% extract magnitude and angle
mag = abs(fCoefs(coefs2plot));
phs = angle(fCoefs(coefs2plot));
figure(2), clf
plot( real(fCoefs(coefs2plot)) , imag(fCoefs(coefs2plot)) ,'o','linew',2,'markersize',10,'markerfacecolor','r');
% make plot look nicer
axislims = max(mag)*1.1;
set(gca,'xlim',[-1 1]*axislims,'ylim',[-1 1]*axislims)
grid on, hold on, axis square
plot(get(gca,'xlim'),[0 0],'k','linew',2)
plot([0 0],get(gca,'ylim'),'k','linew',2)
xlabel('Real axis')
ylabel('Imaginary axis')
title('Complex plane')
有人告诉我可以使用 scipy 中的包:scipy.spatial.cKDTree,但我不知道如何在 python 代码中实现它,以 matlab 中的示例为例代码。 谁能帮帮我
提前致谢
【问题讨论】:
标签: python-3.x matlab scipy