Need some matlab help adding sequences

In summary, the conversation discusses how to add sequences together by shifting the indices of one sequence and padding it with zeros to make it the same length as the other sequences. The code provided is on the right track, and the key is to make sure all sequences have the same length. The solution is to create a new index vector, shift the indices of one sequence, and add all the sequences together.
  • #1
kevinf
90
0

Homework Statement


The problem provides a sequence. Let x(n) ={1,-2,4,6,-5,8,10} where x(-4)=1, x(-3)=-2,etc.
Generate x1(n) = 3x(n+2)+x(n+4)-2x(n)

The Attempt at a Solution



PHP:
x = [0, 0, 1, -2, 4, 6, -5, 8, 10, 0, 0, 0, 0];
n = -6:6;

x1 = x * 3;
n1 = n-2;

x2 = x * 2;
n2 = n;

x3 = x;
n3 = n+4;
subplot(4,1,1), stem(n1,x1)
grid on
subplot(4,1,2), stem(n2,x2)
grid on
subplot(4,1,3), stem(n3,x3)
grid on
The above is the code I have so far to try to get each of the terms separately. but the difficult part is how to add these sequences together, since they start at different indices. I've tried some for loops but i can't figure out a way to get it to sweep the indices and compare them correctly. any help?? thanks
 
Last edited:
Physics news on Phys.org
  • #2


Hi there,

Thank you for sharing your code. It looks like you are on the right track. The key to adding the sequences together is to make sure they are all the same length. In your code, you have already padded the original sequence x(n) with zeros so that it is the same length as the other sequences. Now, you just need to shift the indices of x(n) so that they start at the same index as the other sequences.

Here is one way to do it:

1. Create a new vector n4 that starts at -6 and goes to 6, just like n. This will be the new index for your final sequence.

2. Shift the indices of x(n) by adding 6 to each index. This will make the indices go from 0 to 12, just like n4.

3. Add all the sequences together: x1(n)+x2(n)+x3(n)+x4(n). This will give you your final sequence.

Hope this helps! Let me know if you have any other questions.
 

Related to Need some matlab help adding sequences

What is Matlab?

Matlab is a programming language and interactive environment commonly used for scientific and engineering calculations, data analysis, and visualization.

How do I add sequences in Matlab?

To add sequences in Matlab, you can use the "sum" function. For example, if you have two sequences stored in variables A and B, you can use the command "sum(A,B)" to add them together.

Can I add sequences of different lengths in Matlab?

Yes, you can add sequences of different lengths in Matlab. However, the shorter sequence will be automatically padded with zeros to match the length of the longer sequence.

What if I want to add more than two sequences together?

You can add more than two sequences in Matlab by using the "sum" function multiple times. For example, if you have three sequences stored in variables A, B, and C, you can use the command "sum(A,B,C)" to add them all together.

Are there any other functions in Matlab that can be used to add sequences?

Yes, there are other functions in Matlab that can be used to add sequences, such as "cumsum" for cumulative sum and "conv" for convolution. You can also use loops or vectorization to add sequences in Matlab.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
941
  • Engineering and Comp Sci Homework Help
Replies
3
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
216
  • Engineering and Comp Sci Homework Help
Replies
1
Views
963
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
Back
Top