<script>
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);
</script>