假设我们有两个不同长度的简单序列:
x = rand(3,1);
y = rand(2,1);
我计算了它们之间的互相关,并绘制如下:
r_1 = xcorr(x,(y));
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
subplot(2,2,1); stem(tr,r_1); title('XC');
我想使用卷积计算互相关,并证明它的结果等于使用xcorr()时的结果。但是当我像这样实现它时:
r_2 = conv(x,fliplr(y));
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
subplot(2,2,1); stem(tr,r_2); title('XC');
r_1和r_2的长度不同,并且出现此错误:
Error using stem (line 43)
X must be same length as Y.
感谢您的帮助。
您需要使用Modulo-n圆卷积cconv
,在下面的链接中,您了解了函数cconv
需要提供输出矢量长度的位置。
固定的代码段是:
x = rand(3,1);
y = rand(2,1);
r_1 = xcorr(x,(y));
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
subplot(2,2,1); stem(tr,r_1); title('XC');
r_2 = cconv(x,fliplr(y), length(tr));
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
subplot(2,2,2); stem(tr,r_2); title('XC');
结果图(固定subplot
的索引之后]: