I have an initial function u(x,0) = -sin(x)
and I want to derive the FFT coefficients for an odd-parity solution in the form of u(x,t) = $\sum_{k \geq 1} a_{k} sin (kx)$
. I tried using the normal expansion of the function in terms of $\exp{ikx}$
but it adds some error to the solution.
Can anyone suggest me the procedure of how to filter the Fourier coefficients which remains odd throughout the solution using numpy.fft.fft
?
If the function is inherently odd (like the sine functions) then only the imaginary part of the fft
function will be non-zero. I think your problem is that your function is not periodic as it should be, you should exclude the last point:
import numpy as np
x=np.linspace(-np.pi,np.pi,50,endpoint=False)
y=-np.sin(x)
yf=np.fft.fft(y)
even_part=yf.real
odd_part=yf.imag
Here only odd_part[1]
is non-zero.
If your function is not odd and you want to force it, you can either use sdt
as I mentioned in the comments, or add the inverse of your function on left side then use fft
.
Another point, if your input is not complex, then it's faster and more time efficient to use rfft