how can i use symsum to make function with a variable apart from syms k?

6 views (last 30 days)
hello
i'm trying to perform convolution and plot the result without using conv function in matlab.
i first created two function with variable n to perform convolution.
then, by using the definition of convolution in discrete domain,
i used symsum function to find the sum of each product in the series.
the problem is that the symsum keeps giving me errors.
what should i do to solve the problem? please help
below are the codes i made
close all
clear all
clc
n=-40:40;
unitstep=@(n)round(heaviside(n));
discretedelta=@(n)(n==0);
x=@(n)unitstep(n)-unitstep(n-21);
y=@(n)discretedelta(n-1)+2*discretedelta(n-2)+3*discretedelta(n-3)+2*discretedelta(n-4)+discretedelta(n-5);
syms k;
convxy=@(n)symsum(x(k)*y(n-k),k,-inf,inf);
subplot(2,2,1),
stem(n,x(n))
axis([-4 21 0 3])
hold on
subplot(2,2,2),
stem(n,y(n));
axis([-4 21 0 3])
hold on
subplot(2,2,[3 4]),
stem(n,convxy(n))
axis([-inf inf 0 inf])
hold on

Answers (2)

myFirst Name
myFirst Name on 30 Mar 2021
Hi,
move "syms k;" line to line before x=@...

Paul
Paul on 31 Mar 2021
This code works for the specified sequences. It would need to be modified if either x[n] or y[n] take on non-zero values for n < 0
syms n k real
x(n) = heaviside(n) - heaviside(n-21);
y(n) = kroneckerDelta(n-1) + 2*kroneckerDelta(n-2) + 3*kroneckerDelta(n-3) + 2*kroneckerDelta(n-4) + kroneckerDelta(n-5);
convxy(n) = symsum(x(k)*y(n-k),k,0,n); % summation limits based on fact that x and y are zero for n < 0
% conv solution for comparison
xnum = ones([1 21]);
ynum = [0 1 2 3 2 1];
cnum = conv(xnum,ynum);
stem(0:25,cnum)
hold on;
stem(0:28,double(convxy(sym(0:28))),'x')
  2 Comments
Paul
Paul on 31 Mar 2021
Edited: Paul on 31 Mar 2021
Great point. I changed mine long ago and it's reflected in the code posted above.
>> sympref('HeavisideAtOrigin')
ans =
1
It's too bad the SMT doesn't have a standard, discrete unit step function.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!