Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does IFFT return in Python?

Tags:

python

fft

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])
like image 338
Saten Harutyunyan Avatar asked May 23 '19 09:05

Saten Harutyunyan


People also ask

What is the use of iFFT in Python?

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.

What is the NumPy Ifft function?

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?

Are there any FFT functions in Python?

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.

What is the difference between Ifft() and FFT() in R?

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.


2 Answers

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

like image 124
R2RT Avatar answered Oct 22 '22 12:10

R2RT


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).

like image 36
hiro protagonist Avatar answered Oct 22 '22 10:10

hiro protagonist