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