如何使用 cconv 在 2 个函数之间进行循环卷积?

How to do circular convolution between 2 functions with cconv?

我被要求使用函数 cconv 通过对它们进行采样来在两个函数之间进行循环卷积。这种卷积的已知结果是:CCONV( sin(x), sin(x) ) == -pi*cos(x)

为了测试以上内容,我做了:

w = linspace(0,2*pi,1000);
l = linspace(0,2*pi,1999);
stem(l,cconv(sin(w),sin(w))

但我得到的结果是:

绝对不是-pi*cos(x)

任何人都可以解释我的代码有什么问题以及如何修复它吗?

cconv 的文档中说:

c = cconv(a,b,n) circularly convolves vectors a and b. n is the length of the resulting vector. If you omit n, it defaults to length(a)+length(b)-1. When n = length(a)+length(b)-1, the circular convolution is equivalent to the linear convolution computed with conv.

我认为你的问题的原因是你没有指定第 3 个输入到 cconv,然后选择默认值,这不适合你。我做了一个动画,显示了选择n的不同值时会发生什么。

如果将我的 n=200 结果与您的图进行比较,您会发现数据的幅度大 10 倍,而 linspace 的长度大 10 倍。这意味着需要一些规范化,可能是乘以 linspace 步骤。

确实,在适当缩放和选择 n 之后,我们得到了正确的结果:

res = 100;                % resolution
w = linspace(0,2*pi,res);
dx = diff(w(1:2));        % grid step
stem( linspace(0,2*pi,res), dx * cconv(sin(w),sin(w),res) );


这是我用于动画的代码:

hF = figure(); 
subplot(1,2,1); hS(1) = stem(1,cconv(1,1,1)); title('Autoscaling');
subplot(1,2,2); hS(2) = stem(1,cconv(1,1,1)); xlim([0,7]); ylim(50*[-1,1]); title('Constant limits');

w = linspace(0,2*pi,100);
for ind1 = 1:200
  set(hS,'XData',linspace(0,2*pi,ind1));
  set(hS,'YData',cconv(sin(w),sin(w),ind1));
  suptitle("n = " + ind1);
  drawnow
  % export_fig(char("D:\BLABLA\F" + ind1 + ".png"),'-nocrop');
end