scipy.fft() method in Python computes the Fast Fourier Transform (FFT) of a 1D array, converting a time-domain signal into its frequency-domain form. If no parameters are provided, it uses default settings.
Fast Fourier Transformation Formula:
y[k] = \sum_{n=0}^{N-1} e^{-2\pi j \frac{kn}{N}} x[n]
Where:
- 𝑥
[
𝑛
]
: input time-domain signal
- 𝑁: number of points
- 𝑗: imaginary unit
Example:
Python
import numpy as np
from scipy.fft import fft
x = np.array([1, 2, 3, 4])
res = fft(x)
print(res)
Output
[10.-0.j -2.+2.j -2.-0.j -2.-2.j]
Explanation: This performs the FFT on a real-valued signal [1, 2, 3, 4]. The output is a complex array representing frequency components.
Syntax of scipy.fft()
scipy.fft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None)
Parameters:
Parameter | Type | Description |
|---|
x | array_like | Input array. |
|---|
n | int, optional | Output length pads with zeros if n > len(x). |
|---|
axis | int, optional | Axis to perform FFT on; defaults to last. |
|---|
norm | {None, "ortho"} | Normalization mode, "ortho" uses orthonormal scaling. |
|---|
overwrite_x | bool, optional | Allows input overwrite for memory efficiency. |
|---|
workers | int, optional | Threads for computation (SciPy 1.5.0+). |
|---|
Returns: A NumPy array of complex numbers representing the frequency components of the input signal.
Exceptions:
- ValueError: If input is not array-like or incompatible with FFT computation.
- TypeError: If parameters are passed incorrectly.
Examples
Example 1: Use zero-padding with n parameter
Python
import numpy as np
from scipy.fft import fft
x = np.array([1, 2, 3, 4])
res = fft(x, n=8)
print(res)
Output
[10. -0.j -0.41421356-7.24264069j -2. +2.j
2.41421356-1.24264069j -2. -0.j 2.41421356+1.24264069j
-2. -2.j -0.41421356+7.24264069j]
Explanation: By specifying n=8, the input is zero-padded to a length of 8 before applying FFT.
Example 2: Apply FFT along a specific axis of a 2D array
Python
import numpy as np
from scipy.fft import fft
x = np.array([[1, 2], [3, 4]])
res = fft(x, axis=0)
print(res)
Output
[[ 4.-0.j 6.-0.j]
[-2.-0.j -2.-0.j]]
Explanation: FFT is computed column-wise (i.e., along axis 0) for each column in the 2D array.
Related Articles
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice