Hello,
How would I go about plotting a seqeunce over the below intervals?
A) 𝑥(𝑛) = 2𝛿(𝑛 + 1)− 𝛿(𝑛 −6), −10 ≤ 𝑛 ≤ 10.
B) 𝑥(𝑛) = {… , 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, … }; −20 ≤ 𝑛 ≤ 12.
Thanks

6 件のコメント

Connor Wright
Connor Wright 2020 年 11 月 13 日
The full question I have is this:
Sequences are one of the most implemented techniques to describe signals and systems performances. The Generate and plot each of the following sequences over the indicated interval:
a. 𝑥(𝑛) = 2𝛿(𝑛 + 1)− 𝛿(𝑛 −6), −10 ≤ 𝑛 ≤ 10.
b. 𝑥(𝑛) = {… , 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, … }; −20 ≤ 𝑛 ≤ 12.
Jon
Jon 2020 年 11 月 13 日
This looks like a homework problem. Please attempt to solve the problem, and then once you are having specific problems with the MATLAB code you have written ask about these problems. If you don't know how to get started please get help from your professor or TA
Connor Wright
Connor Wright 2020 年 11 月 13 日
This is the issue I am having, my tutor has been unresponsive on this for a day or so and I have lost all faith in institutional teaching. I am now basically teaching myself.
I have been attempting to solve this problem since around 2pm yesterday using the help section and can't seem to find the right information.
Jon
Jon 2020 年 11 月 13 日
Sorry you haven't been able to get any help. Maybe I can try to get you started.
Let's start with problem a.
First note that the unit sample function, delta(arg) has a value of 1 when arg = 0 and otherwise is zero. So for example delta(0) = 1 delta(-3) = 0, delta(4) = 0 etc and delta(n) = 1 when n = 0 and delta(n) = 0 when n is not equal zero. You can also shift the argument relative to n, so delta(n + 1) = 1 when n = - 1 (because the argument n+1 =0 when n = -1) and so on. So now look at your series and start plugging in values for n starting at n = -10. x(-10) = 2*delta(-10+1) - delta(-10 -6) So x(n) = 2*delta(-9) -delta(-16) ,x(n) = 2*0 -0 x(n) = 0 Now repeat for n = -9
x(-9) = 2*delta(-8) - delta(-15) , x(-9) = 0 and so on. Some terms will finally be non zero
Now think about how you would automate this is MATLAB, you could use a for loop like this
n = -10:10
x = zeros(length(n),1) % preallocate array of zeros to hold result
for k = 1:length(n)
arg1 = n(k) + 1
if arg1 == 0
delta1 = 1
else
delta1 = 0 % 2*0
end
arg2 = n(k) - 6
if arg2 == 0
delta2 = 1
else
delta2 = 0
end
% compute x for this element in seqence
x(k) = 2*delta1 - delta2
end
% plot result
stem(n,x)
But this isn't very good coding practice because we shouldn't cut and paste the same code twice. Otherwise you might fix something in the first part and forget to fix it in other, plus it has lots of extra lines to read. Instead you could define a unit sample function and do it like this
n = -10:10
x = zeros(length(n),1) % preallocate array of zeros to hold result
for k = 1:length(n)
% evaluate x(n)
x(k) = 2*usmpl(n(k)+ 1) - usmpl(n(k)-6);
end
% plot result
stem(n,x)
% define helper function
function delta = usmpl(n)
% define unit sample function
if n == 1
delta = 1
else
delta = 0
end
end
Connor Wright
Connor Wright 2020 年 11 月 16 日
Thanks for the help, just been looking through and doing some playing around with the code you have provided, I have run into an issue with usmpl repeatedly. MatLab throws the error, Unrecognized function or variable 'usmp1'.
Is this part of an add in package?
Jon
Jon 2020 年 11 月 19 日
I define it as a helper function in my script above

サインインしてコメントする。

 採用された回答

Jon
Jon 2020 年 11 月 19 日

1 投票

You could do it like this
n = -10:10
x = zeros(length(n),1) % preallocate array of zeros to hold result
for k = 1:length(n)
% evaluate x(n)
x(k) = 2*usmpl(n(k)+ 1) - usmpl(n(k)-6);
end
% plot result
stem(n,x)
% define helper function
function delta = usmpl(n)
% define unit sample function
if n == 1
delta = 1
else
delta = 0
end
end

6 件のコメント

Jon
Jon 2020 年 11 月 19 日
I copied this from the comments, as it was actually an answer
Jon
Jon 2020 年 11 月 19 日
編集済み: Jon 2020 年 11 月 19 日
Thanks, by the way were you able to crack question b. once you got going on a. ?
Connor Wright
Connor Wright 2020 年 11 月 19 日
Managed the second part of the question pretty easily, the code was essentially the same just with a few tweaks to get it to work with the repeating sequence
Jon
Jon 2020 年 11 月 19 日
That's good, please ask more questions if you get stuck. I'm assuming your taking some kind of Signals and Systems course, good stuff but it can be difficult.
Connor Wright
Connor Wright 2020 年 11 月 19 日
I'm studying Electronic and Electrical Engineering, I am a fabricator, machinist, welder by trade and blacksmith by hobby who doesn't have any formal Mathematics qualification. So with that said you can belive I have many questions 😂
Jon
Jon 2020 年 11 月 20 日
Great that your working on developing your skills, sounds like you have lots of talents, definitely post some new questions if you have more MATLAB questions. I've learned a lot from this site, there are a lot of knowledgeable people answering questions. By the way, you may already be beyond this, but if not the MATLAB On Ramp course is good for getting a quick start https://www.mathworks.com/learn/tutorials/matlab-onramp.html

サインインしてコメントする。

その他の回答 (5 件)

SaiDileep Kola
SaiDileep Kola 2020 年 11 月 16 日

0 投票

Hi Connor,
Start by copying complete code provided by Jon and usmpl is just a local function created to use. Don't use usmpl in the terminal without defining, that will solve your issue.

7 件のコメント

Connor Wright
Connor Wright 2020 年 11 月 16 日
This is what I am struggling with here, what is usmpl supposed to be defined as?
Correct me if I am wrong which is a very very real possibility, the definition of these terms seems to be the final section of code.
SaiDileep Kola
SaiDileep Kola 2020 年 11 月 16 日
That is already defined, it is just a local function.
Copy everything in to a new matlab file and execute it.
You can replace "usmpl", with some "qwerty", it shouldn't change anything.
Connor Wright
Connor Wright 2020 年 11 月 16 日
To clarify, when you say file do you mean overall workspace or a live script? When I attempt to input x(k) = 2*usmpl(n(k)+1) - usmpl(n(k)-6); I get unrecognised function or variable 'usmpl'
I have put it into a live script and called it and it seems to have worked however the graph it produces doesn't look how I would of expected it to.
Jon
Jon 2020 年 11 月 16 日
Hi thanks to SaiDileep Kola for helping Connor with this.
I'm glad you finally got it running without errors. You can run the code as a live script, or just as a plain script (just save it as a .m file.
Note I tried to make the example self contained by including the function definition for the unit sample function (usmpl) inside of the script. If you want to make it available to other scripts, or at the command line then you can cut and paste that function definition into it's own m file. As long as the location for that file is on the MATLAB search path it will then be available to use on the command line or within another script or function. Note if it is saved in the folder from which you are currently running MATLAB it will already be on the path.
When you say the graph does not look as you expect, maybe you can be more specific. Note I just quickly provided the code to give you an example to gety you started on how to think about this going about how to do this. I didn't carefully check to see that the answer was correct. You should check the details of the code to make sure that the calculations make sense to you, and possible correct any errors if you find them.
Jon
Jon 2020 年 11 月 19 日
編集済み: Jon 2020 年 11 月 19 日
Hi Connor, I think actually my answer rather than SaiDileep actually answered your question, (accepted answer) SaiDileep only explained how to get my code to run by copying and pasting it into a file. It probably would have been better if SaiDileep had put this as a comment to the orignal answer rather than as a new answer to avoid confusion. Probably would be easier for someone trying to follow this thread if the accepted answer was the one that had the original details in it. Also although I guess it may be a little petty I felt like I had put quite a bit of effort into getting you started on this when you sounded so frustrated, and I guess getting an answer accepted is a nice form of recognition.
Connor Wright
Connor Wright 2020 年 11 月 19 日
I understand completely Jon, if your answer was posted on the answers I would of accepted yours. I do truly appreciate your help however, after a little digging around I managed to sort the graph output out turns out that all that needed to be done was to change the delta value to 2*1.
Jon
Jon 2020 年 11 月 19 日
Oops now I feel kind of silly, didn't realize I had just continued my original comment. I will try to copy and paste into an answer.

サインインしてコメントする。

Noor Hossen
Noor Hossen 2021 年 7 月 18 日
編集済み: Noor Hossen 2021 年 7 月 18 日

0 投票

(A).
Step 1. At first, needed to make a script file of delta function. It would be
Step 2. Then write another on script file following the problem statement
Output-----------

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

2020 年 11 月 13 日

回答済み:

2022 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by