Circular Convolution using Matrix Method | C # program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program
using System ; class GFG { readonly static int MAX_SIZE = 10 ; static void convolution ( int [ ] x , int [ ] h , int n , int m ) { int [ ] row_vec = new int [ MAX_SIZE ] ; int [ ] col_vec = new int [ MAX_SIZE ] ; int [ ] out_ = new int [ MAX_SIZE ] ; int [ , ] circular_shift_mat = new int [ MAX_SIZE , MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j , i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out_ [ i ] += circular_shift_mat [ i , j ] * col_vec [ j ] ; } Console . Write ( out_ [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] x = { 5 , 7 , 3 , 2 } ; int n = x . Length ; int [ ] h = { 1 , 5 } ; int m = h . Length ; convolution ( x , h , n , m ) ; } }