
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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.]