Circular Convolution using Matrix Method | JavaScript 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
let MAX_SIZE = 10 ; function convolution ( x , h , n , m ) { let row_vec = new Array ( MAX_SIZE ) ; row_vec . fill ( 0 ) ; let col_vec = new Array ( MAX_SIZE ) ; col_vec . fill ( 0 ) ; let out = new Array ( MAX_SIZE ) ; out . fill ( 0 ) ; let circular_shift_mat = new Array ( MAX_SIZE ) ; circular_shift_mat . fill ( 0 ) ; for ( let i = 0 ; i < MAX_SIZE ; i ++ ) { circular_shift_mat [ i ] = new Array ( MAX_SIZE ) ; for ( let j = 0 ; j < MAX_SIZE ; j ++ ) { circular_shift_mat [ i ] [ j ] = 0 ; } } let maxSize = n > m ? n : m ; for ( let i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( let i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } let k = 0 , d = 0 ; for ( let i = 0 ; i < maxSize ; i ++ ) { let curIndex = k - d ; for ( let j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( let i = 0 ; i < maxSize ; i ++ ) { for ( let j = 0 ; j < maxSize ; j ++ ) { out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; } document . write ( out [ i ] + " " ) ; } } let x = [ 5 , 7 , 3 , 2 ] ; let n = x . length ; let h = [ 1 , 5 ] ; let m = h . length ; convolution ( x , h , n , m ) ;