bs=size(b)
cs=size(c)
i=any(bs-cs)
if i
error('error')
end
i=any(~(bs-1))
if ~i
error('error')
end
ko=0
if bs(1)>bs(2)
b=b'
c=c'
ko=1
end
bs=size(b)
cs=size(c)
ss=2*bs(2)-1
a=zeros(1,ss)
for i=1:cs(2)
q=zeros(1,i-1)
p=zeros(1,ss-cs(2)+1-i)
ba=[q,c,p]
ma=b(i)*ba
a=a+ma
end
if ko
a=a'
end
end
测试了一下,跟conv计算的结果偏差很小(我测试的结果是10^-15左右),执行效率略低(用cputime 测试rand(1,99),差了0.2964) 商业软件就是牛啊 真想知matlab中这个函数的源代码
一维卷积用conv(A,B,'same')二维卷积用conv2(A,B,'same')
CONV Convolution and polynomial multiplication.
C = CONV(A, B) convolves vectors A and B. The resulting vector is
length MAX([LENGTH(A)+LENGTH(B)-1,LENGTH(A),LENGTH(B)]). If A and B are
vectors of polynomial coefficients, convolving them is equivalent to
multiplying the two polynomials.
C = CONV(A, B, SHAPE) returns a subsection of the convolution with size
specified by SHAPE:
'full' - (default) returns the full convolution,
'same' - returns the central part of the convolution
that is the same size as A.
'valid' - returns only those parts of the convolution
that are computed without the zero-padded edges.
LENGTH(C)is MAX(LENGTH(A)-MAX(0,LENGTH(B)-1),0).
Class support for inputs A,B:
float: double, single
CONV2 Two dimensional convolution.
C = CONV2(A, B) performs the 2-D convolution of matrices A and B.
If [ma,na] = size(A), [mb,nb] = size(B), and [mc,nc] = size(C), then
mc = max([ma+mb-1,ma,mb]) and nc = max([na+nb-1,na,nb]).
C = CONV2(H1, H2, A) convolves A first with the vector H1 along the
rows and then with the vector H2 along the columns. If n1 = length(H1)
and n2 = length(H2), then mc = max([ma+n1-1,ma,n1]) and
nc = max([na+n2-1,na,n2]).
C = CONV2(..., SHAPE) returns a subsection of the 2-D
convolution with size specified by SHAPE:
'full' - (default) returns the full 2-D convolution,
'same' - returns the central part of the convolution
that is the same size as A.
'valid' - returns only those parts of the convolution
that are computed without the zero-padded edges.
size(C) = max([ma-max(0,mb-1),na-max(0,nb-1)],0).
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)