Created
November 27, 2013 07:39
-
-
Save PhotonEE/7671999 to your computer and use it in GitHub Desktop.
Javascript implementation of convolution function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a function to calculate the convolution of two vectors | |
// or to multiply the two algebraic expressions. | |
/* | |
** | |
idea : | |
------ | |
vec1 = [2,3,4] | |
vec2 = [1,2,3] | |
multiply vec2 by vec1[0] = 2 4 6 | |
multiply vec2 by vec1[1] = - 3 6 9 | |
multiply vec2 by vec1[2] = - - 4 8 12 | |
----------------------------------------------- | |
add the above three = 2 7 14 17 12 | |
the - above shows the displacement after each vector multiplication by element of another vector | |
** | |
*/ | |
function conv(vec1, vec2){ | |
var disp = 0; // displacement given after each vector multiplication by element of another vector | |
var convVec = []; | |
// for first multiplication | |
for (j = 0; j < vec2.length ; j++){ | |
convVec.push(vec1[0] * vec2[j]); | |
} | |
disp = disp + 1; | |
for (i = 1; i < vec1.length ; i++){ | |
for (j = 0; j < vec2.length ; j++){ | |
if ((disp + j) !== convVec.length){ | |
convVec[disp + j] = convVec[disp + j] + (vec1[i] * vec2[j]) | |
} | |
else{ | |
convVec.push(vec1[i] * vec2[j]); | |
} | |
} | |
disp = disp + 1; | |
} | |
return convVec; | |
} | |
/* | |
** | |
Usage: | |
------ | |
vecA = [2,3,2,1] | |
vecB = [4,1,2,3] | |
ans = conv(vecA, vecB); | |
** | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly optimized version: