matlab 编写一维/二维卷积算法
源代码:
function [ out ] = myconv( fx,gx )
[m,n] = size(fx);
[a,b] = size(gx);
out = zeros(m+a-1,n+b-1);
for i = 1:n+b-1
for j = 1:m+a-1
for k = 1:b
for l = 1:a
if (i-k + 1 >= 1) && (j - l + 1 >= 1) && (i-k + 1 <= n) && (j - l + 1 <= m)
out(j,i) = out(j,i) + fx(j - l + 1,i - k + 1)*gx(l,k);
end
end
end
end
end
end
代码测试和matlab自带conv 以及 imfilter结果比较:
clc;
clear;
a = [ 1 2 3 4 5 6 7 8 9];
b = [ 3 2 1];
c = myconv(a,b);
d = conv(a,b