
- 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 - irfftn() Function
SciPy's irfftn() function calculates the n-dimensional Inverse Real Fast Fourier Transform, which it uses to transform frequency-domain input to spatial or time domain. It is for multivariate data and can efficiently transform real-valued datasets coming from rfftn.
The irfftn() function is widely used in 3D imaging, weather simulations, and spectrum filtering when the original data is a real-valued one.
Errors may arise when the input data does not possess the conjugate symmetry expected. In such cases, reconstructions will be incorrect. ValueError will arise if the dimensions of the s parameter are mismatched or if the axes provided are incompatible. Further, if the wrong input that is not obtained from rfftn is used, then surprising results may arise.
The irfftn() technique allows for parallelism through the workers parameter for performance-critical operations and can also support custom scaling through the norm parameter to adapt to various computing requirements.
Syntax
The syntax for the Scipy irfftn() method is as follows −
.irfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, plan=None)
Parameters
This method accepts the following parameters −
x (array_like) − input array
s (sequence of ints, optional) −
axes (sequence of ints, optional) − The axes over which the inverse FFT is computed. Default is all axes.
norm (backward, ortho, forward), optional − The normalization mode allows control over scaling: "backward" (default, no scaling), "ortho" (unitary scaling for energy preservation), and "forward" (scales the output by 1/n, where n is the transform length).
overwrite_x (bool, optional) − If True, allows the input array x to be overwritten for better performance. Default is False.
workers (int, optional) − Number of parallel workers to use for computation. Default is None, meaning a single thread is used.
plan (plan, optional) − Reserved for precomputed FFT plans for performance optimization.
Return Value
out ndarray − returns a real-valued n-dimensional array reconstructed from the input frequency-domain data.
Example 1: Reconstructing 3D Spatial Data Using irfftn
The below example, applies irfftn() method to a 3D Frequency domain data to reconstruct into a original spatial data.
import scipy.fft import numpy as np # Frequency-domain data (3D) x = np.array([[[8 + 0j, 0 + 0j], [0 + 0j, 0 + 0j]]]) spatial_data = scipy.fft.irfftn(x, s=(2, 2, 2)) print("Reconstructed Spatial Data:", spatial_data)
When we run above program, it produces following result
Reconstructed Spatial Data: [[[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]]]
Example 2: Combining irfftn() with rfftn() method
The below code, starts with a 3D spatial dataset, transforms it to the frequency domain by applying rfftn() method, and then reconstructs the original dataset using irfftn() method in such a way that the output matches the input data.
import scipy.fft import numpy as np spatial_data = np.array([[[2, 4], [6, 8]], [[10, 12], [14, 16]]]) frequency_data = scipy.fft.rfftn(spatial_data) reconstructed_data = scipy.fft.irfftn(frequency_data, s=(2, 2, 2)) print("Original Spatial Data:", spatial_data) print("Reconstructed Spatial Data:", reconstructed_data)
Following is an output of the above code −
Original Spatial Data: [[[ 2 4] [ 6 8]] [[10 12] [14 16]]] Reconstructed Spatial Data: [[[ 2. 4.] [ 6. 8.]] [[10. 12.] [14. 16.]]]
Example 3: Comparing Outputs of irfftn with Different Norms
The norm parameter of irfftn() impacts the scaling of the rebuilt data. "backward" doesn't scale the data by default, "forward" scales it to equalize with the forward transformation, and "ortho" enforces unitary, symmetric transformations.
In the below code, we Use irfftn() method and three different norms to reconstruct a 3D spatial data set from frequency-domain data and compare outputs from "backward", "forward" and "ortho" to illustrate how scaling the input affects the reconstruction outcome.
from scipy.fft import irfftn import numpy as np # Simulated frequency-domain data x_data = np.array([[[8 + 0j, 0 + 0j], [0 + 0j, 0 + 0j]]]) # Compute the inverse n-dimensional RFFT with different norms x_backward = irfftn(x_data, s=(2, 2, 2), norm="backward") x_forward = irfftn(x_data, s=(2, 2, 2), norm="forward") x_ortho = irfftn(x_data, s=(2, 2, 2), norm="ortho") print("Reconstructed Data with Backward Norm:\n", x_backward) print("Reconstructed Data with Forward Norm:\n", x_forward) print("Reconstructed Data with Ortho Norm:\n", x_ortho)
Output of the above code is as follows −
Reconstructed Data with Backward Norm: [[[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]]] Reconstructed Data with Forward Norm: [[[8. 8.] [8. 8.]] [[8. 8.] [8. 8.]]] Reconstructed Data with Ortho Norm: [[[2.82842712 2.82842712] [2.82842712 2.82842712]] [[2.82842712 2.82842712] [2.82842712 2.82842712]]]
Example 4: Low-Pass Filtered Data Reconstruction with irfftn
irfftn() plays a key role to rebuild spatial data after filtering in the frequency domain, like getting rid of noise or pulling out features.
In this code, we make a 3D dataset in the frequency domain and use a low-pass filter. We do this by setting frequency_data[2:,:,:] = 0 to cut out high frequencies in the first dimension (indices after 1) and frequency_data[: 2:,:] = 0 to cut out high frequencies in the second dimension (indices after 1). At the end, we use irfftn() to rebuild the dataset in the spatial domain keeping the low-frequency parts.
import scipy.fft import numpy as np frequency_data = np.ones((4, 4, 3)) frequency_data[2:, :, :] = 0 frequency_data[:, 2:, :] = 0 spatial_data = scipy.fft.irfftn(frequency_data, s=(4, 4, 4)) print("Reconstructed Spatial Data After Low-Pass Filter:\n", spatial_data)
Output of the above code is as follows −
Reconstructed Spatial Data After Low-Pass Filter: [[[ 0.25 0. 0. 0. ] [ 0.125 -0.0625 0. 0.0625] [ 0. 0. 0. 0. ] [ 0.125 0.0625 0. -0.0625]] [[ 0.125 -0.0625 0. 0.0625] [ 0. -0.0625 0. 0.0625] [ 0. 0. 0. 0. ] [ 0.125 0. 0. 0. ]] [[ 0. 0. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0. 0. 0. ]] [[ 0.125 0.0625 0. -0.0625] [ 0.125 0. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0.0625 0. -0.0625]]]