0

without using conv() command, I want to convolve two signals:

enter image description here

This is the code I write:

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

However, I get an error:

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.

I cannot find a way to fix it and also I do not know how to plot it at the end because of this. Can you help me in both of these issues please?

blit
  • 75
  • 1
  • 8
  • 2
    Have you considered to try it without using symbolic variables? – Irreducible Oct 11 '22 at 05:30
  • You need to call `f` with an argument, since it's a bare (symbolic) function handle now. E.g. something as `y(i+k-1) + h(k)*f(k)`. Also you might want to consider not using `i` or `j` as variable names, [since they denote the imaginary unit](https://stackoverflow.com/q/14790740/5211833). – Adriaan Oct 11 '22 at 07:24
  • 3
    Just to make sure its clear what others are saying: this is a numerical problem, therefore do not use symbolic variables. – Ander Biguri Oct 11 '22 at 11:04
  • `0<=n<=9` does not mean what you think it does in MATLAB. It evaluates `0<=n`, the result is either true or false, then it compares that result to 9. – Cris Luengo Nov 22 '22 at 14:15

1 Answers1

0

example how to convolve without command 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)')

enter image description here

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

enter image description here

John BG
  • 690
  • 4
  • 12