I need the inverse Fourier transform of a complex array. ifft
should return a real array, but it returns another complex array.
In MATLAB,
a=ifft(fft(a))
, but in Python it does not work like that.
a = np.arange(6)
m = ifft(fft(a))
m # Google says m should = a, but m is complex
Output :
array([0.+0.00000000e+00j, 1.+3.70074342e-16j, 2.+0.00000000e+00j,
3.-5.68396583e-17j, 4.+0.00000000e+00j, 5.-3.13234683e-16j])
The Numpy ifft is a function in python’s numpy library that is used for obtaining the one-dimensional inverse discrete Fourier Transform. It computes the inverse of the one dimensional discrete Fourier Transform which is obtained by numpy.fft. The main application of using the numpy.ifft function is for analyzing signals.
Using numpy, the arrays in python can be processed at a faster rate. It is an open-source library for performing scientific computations and logical and mathematical operations on python arrays. Numpy has several inbuilt functions, and in this article, we shall be talking about one such function – Numpy ifft. What is the Numpy ifft Function?
In Python, there are very mature FFT functions both in numpy and scipy. In this section, we will take a look of both packages and see how we can easily use them in our work.
The only difference is that it computes a one dimensional discrete Fourier Transform whereas ifft () shall do the inverse of the value obtained by fft (). Now, to do inverse Fourier transform on the signal, we use the ifft () funtion. We use the ‘np.fft.ifft () ‘ syntax to access the iffit () function.
The imaginary part is result floating precision number calculation error. If it is very small, it rather can be dropped.
Numpy has built-in function real_if_close
, to do so:
>>> np.real_if_close(np.fft.ifft(np.fft.fft(a)))
array([0., 1., 2., 3., 4., 5.])
You can read about floating system limitations here: https://docs.python.org/3.8/tutorial/floatingpoint.html
if the imaginary part is close to zero you could discard it:
import numpy as np
arr = np.array(
[
0.0 + 0.00000000e00j,
1.0 + 3.70074342e-16j,
2.0 + 0.00000000e00j,
3.0 - 5.68396583e-17j,
4.0 + 0.00000000e00j,
5.0 - 3.13234683e-16j,
]
)
if all(np.isclose(arr.imag, 0)):
arr = arr.real
# [ 0. 1. 2. 3. 4. 5.]
(that's what real_if_close
does in one line as in R2RT's answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With