The matlab code doesn't work?

6 views (last 30 days)
Alvin Nguyen
Alvin Nguyen on 7 Oct 2015
Edited: Thorsten on 7 Oct 2015
Here is the code I use for generating digital impulse signal and unit step signal
impulse signal impseq[n-n0] = 1 when n=n0 and 0 otherwhise
function [x,n] = impseq(n0,n1,n2)
n=n1:n2;
x = [(n-n0)==0];
end
unit step signal stepseq[n-n0] = 1 when n>= n0 and 0 otherwhise
function [x,n] = stepseq(n0,n1,n2)
n=n1:n2;
x = [(n-n0)>=0];
end
here is the program, that is supposed to work
close all; clear all;
n = [-10:10];
x = zeros(-10,length(n));
for k=-5:5
x=exp(-abs(k))*impseq(2*k,-10,10);
end
figure, stem(x,n);
here is the result I got
I can't see that the figure is wrong because at n = 0, the only number for impseq(2k,-5,5) different than 0 is k=0, that make exp(-abs(k)) = 1 so the value at 0 should be 1 but the figure show wrong value
Also, I used stem but there is no stem.

Answers (1)

Thorsten
Thorsten on 7 Oct 2015
Edited: Thorsten on 7 Oct 2015
What is the purpose of the for loop? It computes for 11 values of k (from -5 to 5) different x. But nothing is done with the x after it is computed. So the effect of the for loop is the same as computing x once for k = 5.
For k = 5 impseq has its only non-zero value of 1 at 2*k = 10. This value is multiplied by exp(-abs(k)). This is what the figure shows.
If you want to evaluate exp(-k) for different values, you can do so using
k = -5:5;
x = exp(-abs(k));
stem(k, x)

Community Treasure Hunt

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

Start Hunting!