MATLAB中两信号的数值卷积及输出曲线绘制

bgibtngc  于 2022-11-24  发布在  Matlab
关注(0)|答案(1)|浏览(237)

不使用conv()命令,我想卷积两个信号:

这是我写的代码:

syms n k i

f= @(n) 2.*(0<=n<=9);

h(n)=((8/9)^n).*heaviside(n-3);

L = length(f);
M = length(h(n));   
out=L+M-1;
y=zeros(1,out); 
for i = 1:L
    for k = 1:M
      y(i+k-1) = y(i+k-1) + h(k)*f;
    end
end

但是,我得到一个错误:

Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Error in untitled7 (line 13)
      y(i+k-1) = y(i+k-1) + h(k)*f;

Caused by:
    Error using symengine
    Unable to prove '(0.0 <= 0.0) <= 0.0' literally. Use 'isAlways' to test the statement mathematically.

我找不到一个方法来解决它,也不知道如何在年底,因为这一点。你能帮助我在这两个问题吗?

t2a7ltrp

t2a7ltrp1#

示例:如何不使用命令conv进行卷积

nstop=20;
 
n1=[-nstop:1:nstop];
n0=nstop+1  % n1(n0)=0

h=zeros(1,numel(n1));
x1=zeros(1,numel(n1));

h(n0+[3:nstop])=(8/9).^[3:nstop];
x1(n0+[0:9])=2;

X1=flip(hankel(x1),2)

H=repmat(h,numel(n1),1);

conv_x_h=sum(X1.*H,2)';
n2=[0:numel(n1)-1]; % time reference for resulting conv

figure;
stem(n1,x1)
hold on
stem(n1,h);
grid on
legend('x1(n)','h(n)')

figure
stem(n2,conv_x_h)
grid on
title('conv(x1,h)')

相关问题