# Python Scipy FFT [11 Helpful Examples]

This Python tutorial will learn about the “Python Scipy FFT” where we will use mathematical Fourier transform to solve problems related to signal, etc. And additionally, cover the following topics.

• Python Scipy FFT
• Python Scipy FFT Rfft
• Python Scipy FFT Shift
• Python Scipy FFT Freq
• Python Scipy FFT Convolve
• Python Scipy FFT Fft
• Python Scipy FFT Fft2
• Python Scipy FFT Fftn
• Python Scipy FFT Rfft2
• Python Scipy FFT Hfft
• Python Scipy FFT ihfft

## Python Scipy FFT

The Python Scipy module `scipy.fft` deals with Fourier analysis is a technique for describing a function as a sum of periodic components and extracting the signal from those components. The discrete Fourier transform is created by replacing both the function and its Fourier transform with discretized versions.

The Fast Fourier Transform, an extremely fast approach for performing the DFT, has helped it become a cornerstone of numerical computing. Let’s know the “What is Fourier transform” in simple words.

The Fourier transform is a mathematical function that takes a time-based sequence and determines the total rotation speed, strength and cycle offset for each of the pattern’s possible cycles. Waveforms, which are essential functions of time, space, or any other variable, are transformed using the Fourier transform.

A waveform is decomposed into a sinusoid via the Fourier transform, which gives another way to express a waveform.

Here in the upcoming subsections, we will learn about the most commonly used method of Fourier transform.

## Python Scipy FFT Rfft

The Python SciPy has a method `rfft()` in a module `sicpy.fft` that calculates the discrete Fourier Transform in one dimension of the given real input.

The syntax is given below.

``scipy.fft.rfft2(x, axis=-1, n=None, overwrite_x=True, norm=None, plan=None, workers=None)``

Where parameters are:

• X(array_data): It is an array provided as input.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if true. By default, it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers that can be used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `rfft` returns `out` of type ndarray. The input that has been truncated or zero-padded and converted along the axis supplied by axis, or the final one if the axis is not specified.

Let’s take an example by executing the below python code.

``````from scipy.fft import rfft
rfft([1, 2, 4, 0])``````

In the above code, we have imported the method `rfft` from the module `scipy.fft` and passed the array `[1, 2, 4, 0]` to a method to calculate the discrete Fourier Transform.

This is how to use the method `rfft()` of Python SciPy.

## Python Scipy FFT Shift

The Python Scipy has a method `fftshift()` within module `scipy.fft` that shift the spectrum’s zero-frequency component to the centre.

The syntax is given below.

``scipy.fft.fftshift(x, axes=None)``

The parameters are:

• x(array_data): It is an array as input.
• axes(shape tuple, int): Shift the axes. None, which shifts all axes, is the default.

The method `fftshift()` returns the `y`( which is shifted array) of type ndarray.

Let’s take an example by following the below steps:

Import the required libraries using the below python code

``from scipy.fft import fftshift, fftfreq``

Compute the frequencies using the below code.

``frequncies = fftfreq(5,2)``

Now, perform pass the `frequencies` to a method `fftshift()` using the below code.

``fftshift(frequncies)``

This is how to use the method `fftshift()` of Python SciPy to shift the spectrum’s zero-frequency component to the centre of given frequencies.

## Python Scipy FFT Freq

The Python SciPy contains a method `fftfreq()` in module `scipy.fft` that returns the Fourier Discrete Change in the frequency of the samples. The frequency bin centres in cycles per unit of sample spacing are returned in the float array f.

The syntax is given below.

``scipy.fftpack.fftfreq(n, d=2.0)``

Where parameters are:

• n(int): It is the length of the window.
• d(scalar): It is used to specify the sample spacing.

The method `fftfreq()` returns `f`(The sample frequencies are stored in an array of length n) of type ndarary.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

``````import numpy as np
from scipy.fft import fft, fftfreq``````

Create a signal and transform the signal using Fourier as shown below code.

``````sig = np.array([5,-2, 3, 0, 4, 1, 6, 8], dtype=float)
ft = fft(sig)
n_data = sig.size
time_step = 0.1``````

Calculate the sample frequencies using the below code.

``````frequency = fftfreq(n_data, d=time_step)
frequency``````

This is how to use the method `fftfreq()` of Python SciPy.

## Python Scipy FFT Convolve

The Scipy has a method `fftconvolve()` in module `scipy.signal` that convolves n-dimensional arrays using the method of FFT (Fast Fourier Transform).

The syntax is given below.

``scipy.signal.fftconvolve(in1, in2, mode='full', method='auto')``

Where parameters are:

• in1(array_data): It is used to input the first signal in the form of an array.
• in2(array_data): It is used to input the second signal in the form of an array, the dimension must be the same as the first input array.
• mode: It is used to specify the string that determines output size. The mode can be `same`, `full` and `valid`.
• method: It is used to specify the method that computes the convolution. The method can be `auto`, `direct` and `fft`.

Let’s understand with an example by following the below steps:

Import the required library using the below python code.

``````from scipy.signal import fftconvolve
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline``````

Generate a random noise signal and apply the method `fftconvolveI()` using the below code.

``````random_gen = np.random.default_rng()
s = random_gen.standard_normal(800)
autocorrelation = fftconvolve(s, s[::-1], mode='full')``````

Let’s plot the above-convolved signal using the below code.

``````fig, (orig_axes, mag_axes) = plt.subplots(2, 1)
orig_axes.plot(s)
orig_axes.set_title('It is noise')
mag_axes.plot(np.arange(-len(s)+1,len(s)), autocorrelation)
mag_axes.set_title('It is autocorrelation')
fig.tight_layout()
fig.show()``````

This is how to use the method `fftconvolve()` of Python SciPy to convolve an n-dimensional array.

## Python Scipy FFT Fft

The Python SciPy has a method `fft()` within the module `scipy.fft` that calculates the discrete Fourier Transform in one dimension.

The syntax is given below.

``scipy.fft.fft(x, n=None, norm=None, axis=- 0, overwrite_x=True,plan=None, workers=None,)``

Where parameters are:

• X(array_data): It is an array provided as input.
• n(int): Dimensions of the output’s converted axis. The input will be cropped if n is less than the length of the input. The input will be padded with zeros if it is greater. If n is omitted, the input’s length along the axis indicated by the axis is utilised instead.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers that can be used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `fft()` returns `out`(The input that has been truncated or zero-padded and converted along the axis supplied by axis, or the final one if the axis is not provided) of type complex ndarray.

Let’s take an example by following the below steps:

``````from scipy import fft
import numpy as np``````

compute the `fft` of the given values using the below python code.

``scipy.fft.fft(np.exp(1j * np.pi * np.arange(5) / 5))``

This is how to compute the FFT of the given below using the method `fft()` of Python SciPy.

## Python Scipy FFT Fft2

The Python SciPy has a method `fft2()` within the module `scipy.fft` that calculates the discrete Fourier Transform in two dimensions.

The syntax is given below.

``scipy.fft.fft(x, n=None, norm=None, axis=- 0, overwrite_x=True,plan=None, workers=None,)``

Where parameters are:

• X(array_data): It is an array provided as input.
• n(int): Dimensions of the output’s converted axis. The input will be cropped if n is less than the length of the input. The input will be padded with zeros if it is greater. If n is omitted, the input’s length along the axis indicated by the axis is utilised instead.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `fft2()` returns `out`(The input that has been truncated or zero-padded and converted along the axis supplied by axis, or the final one if the axis is not provided) of type complex ndarray.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

``````from scipy.fft import fft2
import numpy as np``````

Create a two-dimensional array using the method `np.mgrid()` as shown below code.

``twod_array = np.mgrid[:8, :8][0]``

Perform FFT2 on the above created two-dimensional array using the below code.

``fft2(twod_array)``

This is how to use the method `fft2()` of Python SciPy to calculate the discrete Fourier Transform in two dimensions.

## Python Scipy FFT Fftn

The Python SciPy contains a method `fftn()` within module `scipy.fft` that uses the Fast Fourier Transform, this function calculates the n-dimensional discrete Fourier Transform in an M-D array across any number of axes.

The syntax is given below.

``scipy.fft.fftn(x, s=None, n=None, norm=None, axis=- 0, overwrite_x=True,plan=None, workers=None,)``

Where parameters are:

• X(array_data): It is an array provided as input.
• s( int in sequence): The output’s shape (the length of each modified axis).
• n(int): Dimensions of the output’s converted axis. The input will be cropped if n is less than the length of the input. The input will be padded with zeros if it is greater. If n is omitted, the input’s length along the axis indicated by the axis is utilised instead.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `fftn()` returns `out`(The input has been truncated or zero-padded and has been converted along the axes given by axes, or by a mix of s and x) of type ndarray.

Import the required libraries using the below python code.

``````from scipy.fft import fftn
import numpy as np``````

Create a two-dimensional array using the method `np.mgrid()` shown below code.

``twod_array = np.mgrid[:4, :4 :4][0]``

Perform FFTN on the above created two-dimensional array using the below code.

``fftn(twod_array)``

This is how to use the method `fftn()` of Python SciPy to calculate the n-dimensional discrete Fourier Transform in an M-D array across any number of axes.

## Python Scipy FFT Rfft2

The Python SciPy has a method `rfft2()` in a module `sicpy.fft` that calculates the discrete Fourier Transform in two dimensions of the given real input.

The syntax is given below.

``scipy.fft.rfft2(x, axis=-1, n=None, overwrite_x=True, norm=None, plan=None, workers=None)``

Where parameters are:

• X(array_data): It is an array provided as input.
• s(int in sequence): The FFT’s shape.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers that can be used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `rfft2` returns `out`(The actual 2-D FFT’s result) of type ndarray.

Let’s take an example by executing the below python code.

``````from scipy.fft import rfft2
rfft2([[1, 2, 4, 0],
[3, 5, 4, 8]])``````

This is how to use the method `rrft2()` of Python SciPy which calculates the discrete Fourier Transform in two dimensions of the given real input.

## Python Scipy FFT Hfft

The Python SciPy contains a method `hfft()` in a module `scipy.fft` that calculates the FFT of a Hermitian symmetric signal.

The syntax is given below.

``scipy.fft.hfft(x, axis=-1, n=None, overwrite_x=True, norm=None, plan=None, workers=None)``

Where parameters are:

• x(array_data): It is an array provided as input.
• n(int): The length of the output’s converted axis.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `hfft()` returns `out`(The input that has been truncated or zero-padded and converted along the axis supplied by axis, or the final one if the axis is not provided) of type complex ndarray.

Let’s understand with an example by following the below steps:

Import the required libraries using the below python code.

``````from scipy import fft
import numpy as np``````

Create a signal using the below code.

``````array_data = 3 * np.pi * np.arange(20) / 20
sig = np.cos(array_data) + 2j * np.sin(2 * array_data)
sig``````

Now calculate the HFFT of the above-created signal.

``fft.hfft(sig, 15)``

This is how to use the method `hfft()` of Python SciPy that calculates the FFT of a Hermitian symmetric signal.

## Python Scipy FFT ihfft

The method `ihfft()` calculate the inverse FFT of a Hermitian-symmetric signal. of Python SciPy which exists in a module `scipy.fft`.

The syntax is given below.

``scipy.fft.ihfft(x, axis=-1, n=None, overwrite_x=True, norm=None, plan=None, workers=None)``

Where parameters are:

• x(array_data): It is an array provided as input.
• n(int): The length of the output’s converted axis.
• axis(int): The axis on which the FFT is computed.
• overwrite_x (boolean): The contents of x can be destroyed if True. by default it is false.
• norm: Which kind of normalization mode to use like `ortho`, `forward`, `bakcward`.
• plan(object): This parameter is for passing a downstream FFT vendor’s precomputed plan.
• workers(int): The maximum number of workers used in parallel processing. If the value is negative, it wraps around from `os.cpu count ()`.

The method `ihfft()` returns `out`(The input that has been truncated or zero-padded and converted along the axis supplied by axis, or the final one if the axis is not provided) of type complex ndarray.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

``````from scipy import fft
import numpy as np``````

Create a signal spectrum using the below code.

``````sign_spectrum = np.array([ -1, 0, -4, 15, -4, 0])
sign_spectrum``````

Compute the hfft of that signal using the below code.

``fft.ihfft(sign_spectrum)``

This is how to use the method `ihfft()` of Python SciPy which calculates the inverse FFT of a Hermitian-symmetric signal.

You may also like to read the following SciPy tutorials.

So, in this tutorial, we have learned about the “Python Scipy FFT” and covered the following topics.

• Python Scipy FFT
• Python Scipy FFT Rfft
• Python Scipy FFT Shift
• Python Scipy FFT Freq
• Python Scipy FFT Convolve
• Python Scipy FFT Fft
• Python Scipy FFT Fft2
• Python Scipy FFT Fftn
• Python Scipy FFT Rfft2
• Python Scipy FFT Hfft
• Python Scipy FFT ihfft