Skip to content

Discrete Fourier Transform Preprocessor

This computes the 1D discrete Fourier transform (DFT) of the input signal. The Fourier transform is efficiently computed using the fast Fourier transform algorithm utilizing symmetries in the computed terms. The algorithm is fastest for 2 powers of \(N\) i.e. \(2^{N}\). See numpy.fft.fft for more details. The 1D DFT is defined as:

\[ X_k = \sum_{n=0}^{N-1} x_n e^{-2\pi i k n / N}, \quad k = 0, \ldots, N-1. \]

where \(N\) is the number of samples and \(k\) is the frequency index.

Limitations

  • The input signal must be real-valued.
  • The transform is sensitive to noise and outliers.

Bases: Preprocess

1D Discete Fourier Transform (DFT) using Fast-Fourier Transform (FFT).

__call__(signal, n=None, norm='backward', where=lambda : not np.isnan(x))

Compute the 1D Discete Fourier Transform (DFT) using Fast-Fourier Transform (FFT) on the values in x.

Parameters:

Name Type Description Default
signal ndarray

The array to compute the DFT of.

required
n Union[int, int_]

The number of points to use for the FFT. If None, the length of x is used. Default is None.

None
norm str

The normalization mode to use. Default is 'backward'. See numpy.fft for more information. Options include:

'backward': The backward transform is scaled by `1/n`.
'ortho': The forward and backward transforms are scaled by `1/sqrt(n)`.
'forward': The forward transform is not scaled.
'backward'
where Callable[[Union[int, float, int_, float_]], Union[bool, bool_]]

A function that takes a value and returns True or False. Default is lambda x: not np.isnan(x) i.e. a measurement is valid if it is not a NaN value.

lambda : not numpy.isnan(x)

Returns:

Type Description
ndarray

The 1D DFT of x. It should be noted that the result contains complex numbers. The absolute value of the result can be considered for further processing.

Examples

We define as signal as \(f(t) = 2 \sin(2 \pi t) + \sin(10 \cdot 2 \pi t)\) for \(t \in [1, 10]\) with a sampling rate of 100 samples per second. The signal is then transformed using the DFT preprocessor.

Transform Signal

import numpy as np
import autonfeat as aft

start_time = 1  # Start time in seconds
end_time = 10    # End time in seconds
sampling_rate = 100  # Number of samples per second
num_samples = int((end_time - start_time) * sampling_rate)

# Signal = 2 x sin(2 x pi x t) + sin(10 x 2 x pi x t)
time = np.linspace(start_time, end_time, num_samples)
signal = 2 * np.sin(2 * np.pi * time) + np.sin(10 * 2 * np.pi * time)

# Create Preprocessor
preprocessor = aft.preprocess.DFTPreprocessor()

# Preprocess and transform signal
transformed_signal = preprocessor(signal)

Visualize Transform

We then visualize the signal and its Fourier transform. The signal in the frequency domain may be able to identify important features in the signal that were otherwise not visible in the time domain.

import matplotlib.pyplot as plt

# Plot results
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 6))

ax1.plot(time, signal)
ax1.set_xlabel("Time (s)")
ax1.set_ylabel("f(t)")

ax2.plot(
    np.fft.fftfreq(num_samples, 1 / sampling_rate), 
    np.abs(transformed_signal)
)
ax2.set_xlabel("Frequency (Hz)")
ax2.set_ylabel("| FFT(f(x)) |")

plt.tight_layout()
plt.show()

DFT

If you enjoy using AutonFeat, please consider starring the repository ⭐️.