convolution integral of two continious time signals

i need to evaluate the convolution between ( [ 0.5*( (a)^n ) + 0.5*( (-a)^n ) ] )*u[n] and ( u[n] - u[n-50] ). i tried to write a code by the help of another post but couldn't. The code is
syms n;
a = sqrt(b);
h(n) = (0.5*((a)^n) + 0.5*((-a)^n))*heaviside(n)
hcal(0,a);
x=0;
while (ans>0.01)
x = x+2;
hcal(x,a);
end
fprintf('The sallest n is %.f\n',x)
n = [-2:1:17];
stem(n,h(n));
%%rest of the code is the part that i wrote by help
jfun= @(n)(1.*( (n>=4) - (n>=6) ) );
lfun= @(n)( (0.5*((a)^n) + 0.5*((-a)^n)).*(n>=0))
tt = [1:1:50];
cint=0*tt;
for ii = 1:length(tt)
cfun = @(q,n)(jfun(q).*lfun(n-q));
cint(ii) = integral(@(q)cfun(q,tt(ii)),0,tt(ii));
end
zz1 = conv(jfun(tt),lfun(tt))*tt(2);
stem(tt,cint,'-x',(0:length(zz1)-1)*tt(2),zz1)
end
What is/are my mistake(s)?

Answers (1)

Paul
Paul on 12 Oct 2026 at 3:44
Edited: Paul on 12 Oct 2026 at 11:52
Because the signals in question are functions of n, and n typically denotes an integer, we can assume the question is really about computing the convolution sum. Also, the "50" in the question will change to "10" to make the plots shorter.
Define some variables
syms n k integer
syms a real
Define the discrete-time unit step function:
sympref('HeavisideAtOrigin',1);
u(n) = heaviside(n);
Define h(n)
h(n) = ( 0.5*( (a)^n ) + 0.5*( (-a)^n ) )*u(n);
h(n) = simplify(h(n))
h(n) = 
First, we find the convolution sum of h[n] and u[n], call it w[n]. Because both h[n] = 0 and u[n] = 0 for n < 0, we can compute the convolution sum of h[n] and u[n] from 0 to n using symsum
Not sure why this command results in an error (it doesn't in 2024a)
try
w(n) = simplify(symsum(h(n-k)*u(k),k,0,n))*u(n)
catch ME
ME.message
end
ans = 'Recursive definition: Reached maximal depth for nested procedure calls.'
I think that the problem is with the mutliple heavisides in the expression input to symsum. Instead, define h[n] as
h(n) = ( 0.5*( (a)^n ) + 0.5*( (-a)^n ) );
h(n) = simplify(h(n))
h(n) = 
where it's understood that h[n] = 0 for n < 0. Using the fact that u[n] = 1 for n > = 0, we have
w(n) = simplify(symsum(h(n-k)*1,k,0,n))*u(n)
w(n) = 
We really want the convolution of h[n] with x[n] where
x(n) = u(n) - u(n-10)
x(n) = 
Using the distributive and shift properties of the convolution sum we have
y[n] = h[n]*x[n] = h[n]*(u[n] - u[n-10]) = h[n]*u[n] - h[n]*u[n-10] = w[n] - w[n-10]
y(n) = simplify(w(n) - w(n-10))
y(n) = 
Plot 21 samples of y[n] for a = 0.7;
aval = 0.7;
N = 20;
figure
stem(0:N,subs(y(0:N),a,aval),DisplayName='symsum');
We could also get a numerical solution using conv.
Define anonymous functions for u, x, and h
ufun = @(n) double(n>=0);
xfun = @(n) ufun(n) - ufun(n-10);
hfun = @(n,a) ( 0.5*( (a).^n ) + 0.5*( (-a).^n ) ).*ufun(n);
making sure we have enough samples to cover the desired ouput
nval = 0:N;
yval = conv(xfun(nval),hfun(nval,aval));
and truncating the conv result to the desired length.
yval = yval(1:numel(nval));
hold on
stem(nval,yval,'d',DisplayName = 'conv');
As a check, we can also take the inverse z-transform of the product of the z-transforms of h[n] and x[n]
syms z
H(z) = ztrans(h(n),n,z);
X(z) = ztrans(x(n),n,z);
Y(z) = H(z)*X(z);
y1(n) = iztrans(Y(z),z,n)*u(n);
stem(0:N,subs(y1(0:N),a,aval),'x',DisplayName='ztrans');
legend;
xlabel('n'); ylabel('y[n]')

Categories

Asked:

on 14 Nov 2020

Edited:

on 13 Oct 2026 at 0:37

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!