Numpy, which is short for Numerical Python, is a library that helps work with multi-dimensional arrays and matrices in python. 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?
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. Here ‘ifft’ stands for ‘Inverse Fast Fourier Transform’. Direct conversion of discrete Fourier Transform into its inverse uses high computation power. So, we use numpy.ifft as it performs the inverse at a faster rate.
About Inverse Fourier Transform
Before going into NumPy’s ifft function, we shall learn first about what is inverse Fourier transform. Fourier Transform converts time signals to their frequency, and Inverse Fourier Transform converts it back into their respective time signals.
Image processing, image compression, analyzing signals, audio compression, image reconstruction, etc., are the various applications of Inverse Fourier Transform in python. By using inverse Fourier transform, we convert the signals from their frequency domain to their time domain.
Syntax of numpy ifft
The syntax of the ifft function in numpy is :
fft.ifft(a, n=None, axis=-1, norm=None)
a: It is the input array which has to be transformed.
n: It is the optional parameter whose value is None by default. It is the length of the transformed axis of the output. If n is not given, then the input length is according to the axis value specified. If n is smaller than the length of the input, then the input is cropped. Else if n is greater, then padding with zero is performed on the input.
axis: This is again an optional parameter that has the value of a negative one by default. If the n value is not given, then the axis variable is given the value of the axis over which the inverse has to be computed.
norm: It is an optional value that is ‘backward’ by default. It indicates the direction of the pair of transforms. The value can be either ‘forward,’ ‘backward,’ or ‘ortho.’
out : The output is the transformed complex n dimensional array.
Numpy ifft using python
Lets us now implement the ifft function in python. For that, first, we shall import the numpy library.
import numpy as np
Then, after importing numpy, we will create a one-dimensional array using the numpy array() function.
array = np.array([1,5, 4,3])
Here, we shall perform the Fourier transform of the given array first. For that, we shall use fft.fft() function present in numpy. The fft() function takes the same parameters as the ifft() function.
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().
ft = np.fft.fft(array)
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.
We shall pass the ‘ft’ variable as an argument to the ifft() function. This will perform the inverse of the Fourier transformation operation.
ift = np.fft.ifft(ft)
Now, we shall print both the variables – ‘ft’ and ‘ift’ obtained out of Fourier transformation and inverse Fourier transformation respectively.
The output is:
[13.+0.j -3.-2.j -3.+0.j -3.+2.j] [1.+0.j 5.+0.j 4.+0.j 3.+0.j]
As seen above, when we performed inverse of the Fourier transformation, we got our original array back.
[ 1. + 0.j , 5+0.j , 4+0.j , 3+0.j ] is equal to [ 1, 5, 4, 3 ].
Here as you can see, the output is in the form of complex numbers. Complex numbers are in the form of ‘a + bi’ where a is the real part of the number and b is the imaginary part.
If you don’t want the answer in complex numbers, we can convert it into an absolute form. For that, we can use the abs() function present in the numpy library.
The output will be:
[13. 3.60555128 3. 3.60555128] [1. 5. 4. 3.]
However, note that if you cannot use the absolute value obtained from the fft() function for performing inverse, it will not give the same result.
The key difference between rfft() and fft() is the speeds at which both work. Using rfft() is faster than using fft() because rfft() does not calculate the negative half of the frequency spectrum.
As the name suggests, the scipy.ifft() function is accessed through the scipy library and the numpy.ifft() function is accessed through the numpy library. The difference is that the scipy.ifft() contains more features than the numpy.ifft() function, and because of that, it is preferred compared to the numpy library.
That is all, folks! If you have anything to share, we would love to hear you in the comments.
Until then, Keep Learning!