SciPy - fftshift() Function



The fftshift() function in SciPy moves the zero-frequency part of a Fourier transform to the middle of the spectrum. FFT puts the zero-frequency part at the beginning of the output, but to analyze and visualize, it makes more sense to have it in the center. fftshift() method reorganizes the data to do this.

In signal or image processing, after performing an FFT, it's common to use fftshift for visualization. For example in spectogram using fftshift puts the spectrum in the middle, which helps you tell high and low frequencies by centering the spectrum.

It's also useful in physics when you're looking at diffraction patterns. By moving the zero frequency to the center, you can examine patterns around the origin more. This makes the whole analysis process smoother.

Syntax

Following is the syntax of the SciPy fftshift() method −

.fftshift(x, axes=None)

Parameters

Following are the parameters of this method −

  • x (array_like) − The input array (e.g., a Fourier-transformed signal or image).

  • axes(optional) − The axes along which the shift is performed. By default, it shifts along all axes. when axes = 0, shifts the zero frequency component along the rows (vertical axis) and when, axes = 1, shifts the zero frequency component along the columns (horizontal axis).

Return Value

The fftshift() method returns a new array where the zero-frequency component is centered.

Example 1: Shiffting 1D Frequencies

This is the basic example of how fftshift() transforms a 1D frequency array, it shifts the zero-frequency component from the start of the spectrum to its center. Following is the code −

import numpy as np

data = np.fft.fftfreq(8, 0.2) 
print("Original Frequencies:", data)
shifted_freq = np.fft.fftshift(data)
print("Shifted Frequencies:", shifted_freq)

Output of the above code is as follows −

Original Frequencies: [ 0.     0.625  1.25   1.875 -2.5   -1.875 -1.25  -0.625]
Shifted Frequencies: [-2.5   -1.875 -1.25  -0.625  0.     0.625  1.25   1.875]

Example 2: Shifting 2D FFT Frequencies

This example applies fftshift() to a 2D FFT result, shifting the zero-frequency component in order to better present frequency data in a 2D array. Following is the code −

import numpy as np
from scipy.fft import fft2, fftshift

arr_2d = np.arange(16).reshape(4, 4)
print("image is :\n", arr_2d)
fft = fft2(arr_2d)
shifted_result = fftshift(fft)
print("\nFFT Result (Before Shift):\n", np.round(fft.real, 2))
print("\nFFT Result (After Shift):\n", np.round(shifted_result.real, 2))

Output of the above code is as follows −

image is :
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

FFT Result (Before Shift):
[[120.  -8.  -8.  -8.]
 [-32.   0.   0.   0.]
 [-32.   0.   0.   0.]
 [-32.   0.   0.   0.]]

FFT Result (After Shift):
[[  0.   0. -32.   0.]
 [  0.   0. -32.   0.]
 [ -8.  -8. 120.  -8.]
 [  0.   0. -32.   0.]]

Example 3: fftshift() using axes parameter

in the below code let us create a 4 * 4 matrix using np.fft.fftfreq with 16 points. Then by using fftshift(freqs, axes=1) we shift the frequency components along the rows, centering the zero-frequency components.

import numpy as np

freqs = np.fft.fftfreq(16, d=1./16).reshape(4, 4)

# Apply fftshift along rows (axes=0)
shifted_freqs = np.fft.fftshift(freqs, axes=1)

print("Original Frequency Matrix:")
print(freqs)

print("\nShifted Frequency Matrix:")
print(shifted_freqs)

Output of the above code is as follows −

Original Frequency Matrix:
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [-8. -7. -6. -5.]
 [-4. -3. -2. -1.]]

Shifted Frequency Matrix:
[[ 2.  3.  0.  1.]
 [ 6.  7.  4.  5.]
 [-6. -5. -8. -7.]
 [-2. -1. -4. -3.]]
scipy_discrete_fourier_transform.htm
Advertisements