Home:ALL Converter>Plot two fourier coeficients with python

Plot two fourier coeficients with python

Ask Time:2019-08-23T05:15:59         Author:Sie. Theos

Json Formatter

I am working with the discrete Fourier transform in python. In my python code, how can I plot two Fourier coefficients in the complex plane.

I have seen that in matlab they use the following code to do it, they use dsearchn to plot it, as show here:

% 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')

I've been told that I can use the package from scipy: scipy.spatial.cKDTree, but I don't know how to implement it in a python code taking the example from the matlab code. Can anyone help me

Thanks in advance

Author:Sie. Theos,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57617142/plot-two-fourier-coeficients-with-python
yy