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

조회 수: 5 (최근 30일)
JaeHyeong Park
JaeHyeong Park 2016년 9월 29일
편집: Paul 2021년 3월 31일
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

답변 (2개)

myFirst Name
myFirst Name 2021년 3월 30일
Hi,
move "syms k;" line to line before x=@...

Paul
Paul 2021년 3월 31일
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
Walter Roberson
Walter Roberson 2021년 3월 31일
You might want to modify sympref('HeavisideAtOrigin')
Paul
Paul 2021년 3월 31일
편집: Paul 2021년 3월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by