SciPy - circulant() Function



The scipy.linalg.circulant() method produces a square matrix, in which each row represents a cyclic shift of the input sequence. The first row is the sequence itself and subsequent rows are created by shifting elements of the previous row to the right in a circular manner. The cyclic structure of circulant matrices makes them very useful for certain applications in signal processing and linear algebra.

Circulant matrices are used in signal processing, image filtering, and convolution problems. They well represent linear time-invariant systems and appear very extensively in Fourier analysis since circulants are diagonalized by the Discrete Fourier Transform.

This approach is unlikely to produce errors unless the input is invalid, such as non-1D data. To obtain reliable results, ensure that the input c is a valid 1D sequence.

This circulant() method can be combined with FFT() (Fast Fourier Transform) in order to efficiently compute convolutions, therefore, it is powerful for signal and image processing tasks.

Syntax

The syntax for the Scipy method is as follows −

.circulant(c)

Parameters

This method accepts the following parameters −

  • c (N,) array_like − 1D array-like sequence that defines the first row of the circulant matrix.

Return Value

A (N, N) ndarray − A square nn circulant matrix, where n is the length of c.

Example 1: Basic Circulant Matrix

The circulant approach constructs a matrix in which each row is a cyclic shift of the input sequence.

In the below code, the input array c=[1,2,3,4] determines the first row of the circulant matrix. The circulant() method accepts this array and returns a square matrix in which the first row is exactly c.

Each subsequent row is a cyclic shift of the previous row; that is, the last element shifts to the front, and all other elements shift one place to the right.

import numpy as np
from scipy.linalg import circulant

# Input: 1D array
c = [1, 2, 3, 4]

# Generate the circulant matrix
C = circulant(c)
print("Circulant Matrix:\n", C)

When we run above program, it produces following result

Circulant Matrix:
 [[1 4 3 2]
 [2 1 4 3]
 [3 2 1 4]
 [4 3 2 1]]

Example 2: Circulant Matrix and FFT for Efficient Convolution

The circulant() method creates a circulant matrix from a periodic signal represented by the array signal=[1,1,1,1]. The circulant matrix models the signal's cyclic nature, with each row representing a cyclic shift in the input sequence.

The circulant matrix's Fourier Transform is then computed by using the fft() function to its columns. This transformation encodes the signal in the frequency domain, allowing for efficient convolution processes. The output gives the converted data, which can be used for further analysis, including filtering or signal reconstruction.

import numpy as np
from scipy.linalg import circulant
from scipy.fft import fft

# Input: Signal array
signal = [1, -1, 1, -1]

# Generate the circulant matrix
C = circulant(signal)

# Compute the FFT of the circulant matrix
fft_result = fft(C, axis=0)
print("FFT of Circulant Matrix:\n", fft_result)

Following is an output of the above code

FFT of Circulant Matrix:
 [[ 0.-0.j  0.-0.j  0.-0.j  0.-0.j]
 [ 0.+0.j  0.+0.j  0.+0.j  0.+0.j]
 [ 4.-0.j -4.-0.j  4.-0.j -4.-0.j]
 [ 0.-0.j  0.-0.j  0.-0.j  0.-0.j]]

Example 3: Solving Linear Systems with Circulant Matrices

In the code below, the input array c=[4,1,0] creates a circulant matrix C by using circulant() method, while the vector b=[1,2,3] represents the right-hand side of the equation Cx=b. The solve() method uses the circulant matrix's cyclic properties to efficiently compute the solution vector (x). The output shows x, which satisfies the linear system.

import  numpy as np
from scipy.linalg import circulant, solve

# Input: Circulant matrix and right-hand side vector
c = [4, 1, 0]
b = [1, 2, 3]

# Generate the circulant matrix
C = circulant(c)

# Solve the linear system Cx = b
solution = solve(C, b)
print("Solution of the Linear System:\n", solution)

Output of the above code is as follows

Solution of the Linear System:
[0.09230769 0.47692308 0.63076923]
scipy_special_matrices_functions.htm
Advertisements