Skip to content Skip to sidebar Skip to footer

Fourier Series From Discrete Fourier Transform

I'm trying to recreate a function from a discrete fourier transform. In Matlab it would be done like this: function [y] = Fourier(dft,x) n = length(dft); y = cos(pi*(x+1)'*(0:n-1))

Solution 1:

You were running two nested loops instead of one. Try this:

y = ([(dft[nn].real)*np.cos(np.pi*x*nn) + (dft[nn].imag)*np.cos(np.pi*x*nn) for nn in range(0,n)])

Solution 2:

You actually should not use a Python loop at all. You get more readable and much more efficient code if you vectorise the expression. Assuming dft is a complex-valued NumPy array, you could use

xn = x * np.arange(n)
y = dft.real * np.cos(xn) + dft.imag * np.sin(xn)

(Note that your Matlab code, your Python code and the formula you gave do three different things. The code I gave is closest to the Matlab code.)

Post a Comment for "Fourier Series From Discrete Fourier Transform"