
- 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 - convolution_matrix() Function
The scipy.linalg.convolution_matrix() method creates a convolution matrix for a 1D input array of size n. The convolution matrix is a structured matrix that represents the convolution operation using matrix-vector multiplication.
- This method is very useful for signal processing, filtering, and numerical analysis since it allows for efficient convolution operations through matrix-based computations.
- This method is performed to carry out convolution operations, construct linear filters, and compute solutions to related systems of convolutions in, for example image and signal processing.
Errors arise if the size of the input array an or dimension n is invalid. For example, n need to be strictly greater than the size of a when mode = 'full', and providing wrong dimensions or unallowed modes: ('valid,' 'full', or 'same') may lead to errors.
The convolution matrix is often combined with methods like solve() for solving linear systems or fft() for performing convolution in the frequency domain.
Syntax
The syntax for the SciPy convolution_matrix() method is as follows −
.convolution_matrix(a, n, mode='full')
Parameters
This method accepts the following parameters −
a (m,) array_like − The input sequence to be used for creating the convolution matrix.
n (int) − The size of the output matrix/vector depending on the chosen mode.
mode (str) − The mode parameter defines the output shape: 'full' gives the largest matrix with all overlaps, 'valid' includes only fully overlapping sections, and 'same' matches the size of n.
Return Value
c (k, n) ndarray −The convolution matrix representing the specified mode of convolution for the input array.
Example 1
The 'full' mode generates the largest matrix, which includes all potential overlaps.
In the below code, the input array [1,2,3] is utilized to build a full convolution matrix (n=5) using convolution_matrix() method.
The approach generates a (n+m1)n matrix, where m is the size of a. Each row of the output shows a shifted version of the input sequence.
import numpy as np from scipy.linalg import convolution_matrix # Input array and size a = [1, 2, 3] n = 5 # Generate the convolution matrix conv_matrix = convolution_matrix(a, n, mode='full') print("Full Convolution Matrix:\n", conv_matrix)
When we run above program, it produces following result
Full Convolution Matrix: [[1 0 0 0 0] [2 1 0 0 0] [3 2 1 0 0] [0 3 2 1 0] [0 0 3 2 1] [0 0 0 3 2] [0 0 0 0 3]]r
Example 2
The 'valid' mode produces a smaller matrix containing only fully overlapping portions.
The input array [1,2,3] and n=5 generate a convolution matrix() in which only rows with full overlap are maintained. The resulting matrix size is smaller than in the 'full' mode.
import numpy as np from scipy.signal import convolve a = np.array([1, 2, 3]) # Kernel x = np.array([4, 5, 6, 7, 8]) # Input signal result = convolve(x, a, mode='valid') print("Valid Convolution Result:", result)
Following is an output of the above code
Valid Convolution Matrix: [[3 2 1 0 0] [0 3 2 1 0] [0 0 3 2 1]]
Example 3
The convolution matrix simulates signal filtering by applying a predetermined filter to a signal.
The input array [1,0.5,0.25] functions as a low-pass filter. The convolution matrix is applied to a signal b=[2,4,6,8,10], and the output reflects the filtered signal. This example shows how convolution matrices simulate filtering in time-domain signals.
import numpy as np from scipy.linalg import convolution_matrix # Input filter and signal a = [1, 0.5, 0.25] b = [2, 4, 6, 8, 10] # Generate the convolution matrix conv_matrix = convolution_matrix(a, len(b), mode='same') # Apply the filter to the signal filtered_signal = conv_matrix @ np.array(b) print("Convolution Matrix:\n", conv_matrix) print("Filtered Signal:", filtered_signal)
Output of the above code is as follows
Convolution Matrix: [[0.5 1. 0. 0. 0. ] [0.25 0.5 1. 0. 0. ] [0. 0.25 0.5 1. 0. ] [0. 0. 0.25 0.5 1. ] [0. 0. 0. 0.25 0.5 ]] Filtered Signal: [ 5. 8.5 12. 15.5 7. ]