SciPy - fftfreq() Function



The fftfreq() function in SciPy's scipy.fft module is crucial to map the frequencies in a Fourier Transform result. Without it, identifying which frequency corresponds to each bin in the FFT would be challenging, making frequency analysis less practical.

When you run a Fourier Transform, this function helps change time-domain data to frequency-domain data. This allows for the identification of dominant frequencies, which is essential for signal analysis and filtering.

People use fftfreq() often in signal processing sound analysis, communication systems, and scientific studies. It makes breaking down the frequency parts in data simpler.

By giving accurate information on frequency bins, fftfreq() is key in many scientific and engineering uses. Its output helps with jobs like filtering out unwanted frequencies looking at spectral data, or making communication systems better based on how frequencies behave.

Syntax

Following is the syntax of the SciPy method −

.fftfreq(n, d=1.0)

Parameters

This method accepts the following parameters −

  • n (int) − The number of sample points (i.e., the length of the signal or array). −

  • d − The sampling rate inverse or sample spacing which, defines the distance between consecutive data points. The sampling rate by default is 1.0, meaning one sample per unit of time.

Return Value

It returns a 1D array of sample frequencies, which are spaced according to the given sampling interval (d). The length of the output array is the same as the input size n, and it represents the frequencies associated with the FFT bins.

Example 1

Following is the basic SciPy fftfreq() method that computes the frequency bins for a given number of data points and sample spacing, helping to translate the FFT result into meaningful frequency values for further analysis.

This example determines the frequency bins for eight data points with a sample spacing of 0.1 seconds. For each point in the FFT result, the output will give us the corresponding frequency values.

import numpy as np
from scipy.fft import fftfreq

n_points = 8
spacing = 0.1

# Compute frequency bins
frequencies = fftfreq(n_points, d=spacing)
print("Frequencies:", frequencies)

When we run above program, it produces following result −

Frequencies: [ 0.    1.25  2.5   3.75 -5.   -3.75 -2.5  -1.25]

Example 2

In this case, we create a 10 Hz sine wave and use the fftfreq() method to work out the frequency bins. This helps us spot the frequency parts of the signal, which is crucial to analyze spectra and handle signalprocessing.

In the below output it shows a main frequency of 10 Hz, which matches the sine wave's frequency. This helps us to identify important parts for further analysis or filtering.

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

# Generate a sine wave signal with 10 Hz frequency
sample_rate = 20  # 20 samples per second
T = 1.0 / sample_rate  # Sampling interval
t = np.arange(0.0, 1.0, T)  # Time vector

signal = np.sin(2 * np.pi * 10 * t)  # Sine wave at 10 Hz

# Compute the FFT of the signal
N = len(signal)
yf = fft(signal)

# Calculate the corresponding frequency bins using fftfreq
xf = fftfreq(N, T)

print("Frequencies:", xf)

Following is an output of the above code −

Frequencies: [  0.   1.   2.   3.   4.   5.   6.   7.   8.   9. -10.  -9.  -8.  -7.
  -6.  -5.  -4.  -3.  -2.  -1.]
scipy_discrete_fourier_transform.htm
Advertisements