Lab Act No.1 - 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

14-02258

*Note: All codes from 1 to 10


A. Generate the input vectors for each problem using the codes below. Identify if the following systems
represented by the equations are linear or not. Use a1 as the number of letters in your first name and a2 as the
number of letters in your surname.
NOTE: All codes from numbers 1 to 10 are initialized with this code that generates x1[n], x2[n], n1, a1 and a2.
rng('shuffle'); x1 = randi(20,1,21); n1 = [-10:10];
rng('shuffle','twister'); x2 = randi(20,1,21);
a1 = 15; a2 = 9; % Number of letters in name

Also, to check if the system equation is linear, equality of τ1 and τ2 is checked using this code:
if T2== T1
display('The system equation is linear')
else
display('The system equation is not linear')
end

Solution (This applies to all of part A.):

To check if the system is linear, I checked if T{a1x1[n] + a2x2[n]} = a1T{x1[n]} + a2T{x2[n]}. I first
excited x1[n] and x2[n] separately, this yields y1 = T{x1[n]} and y2 = T{x2[n]}. Then I added the two, resulting
to T1 = a1y1 + a2y2 = a1T{x1[n]} + a2T{x2[n]}.

On the other hand, I joined x1[n] and x2[n] first, yielding xnew[n]= a1x1[n] + a2x2[n]. Then I excited
xnew, resulting to T2 = T{xnew[n]} = T{a1x1[n] + a2x2[n]}. Then if T1 = T2, the system equation is linear,
otherwise, its not.

1. y[n] = nx[n]
Code:
y1 = (n1.*x1); y2 = (n1.*x2);
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1);stem(n1,T1);title('Subplot 1: a1T{x1[n]} +
a2T{x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


T2 = (xnew).*n1; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n1,T2);title('Subplot 2: T{a1x1[n] +
a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:
2. y[n] = x[n2]
Code
y1 = [x1(1,1), x1(1,2), x1(1,5), x1(1,10), x1(1,17)]; n3 = [0,1,4,9,16];
y2 = [x2(1,1), x2(1,2), x2(1,5), x2(1,10), x2(1,17)]; n4 = n3;
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n3,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


T2 = [xnew(1,1), xnew(1,2), xnew(1,5), xnew(1,10), xnew(1,17)]; n5 = n3; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n5,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:

3. y[n] = x2[n]
Code:
y1 = (n1.*x1); y2 = (n1.*x2);
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1);stem(n1,T1);title('Subplot 1: a1T{x1[n]} +
a2T{x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';
xnew = (a1*x1 + a2*x2);
T2 = (xnew).^2; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n1,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:

4. y[n] = 3x[n] + 1
Code:
y1 = 3*x1 + 1; y2 = 3*x2 + 1;
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n1,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

xnew = (a1*x1 + a2*x2);


T2 = 3*(xnew) + 1; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2); stem(n1,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
Result:
5. y[n] = ex[n]
Code:
y1 = exp(x1); y 2 = exp(x2);
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n1,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n')
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


T2 = exp(xnew); % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n1,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:

6. y[n] = 2x[n-2] + 5
Code:
[x3,n3] = sigshift(x1,n1,2); [x4,n4] = sigshift(x2,n1,2);
y1 = 2*x3 + 5; y2 = 2*x4 + 5;
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1);stem(n3,T1);title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}');xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


[x5,n5] = sigshift(xnew,n1, 2);
T2 = 2*x5 + 5; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n5,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:

7. y[n] = x[n+1] + x[n-1]


Code:
[x3,n3] = sigshift(x1,n1,-1); [x4,n4] = sigshift(x1,n1,1);
[x5,n5] = sigshift(x2,n1,-1); [x6,n6] = sigshift(x2,n1,1);
[y1,n7] = sigadd(x3,n3,x4,n4); [y2,n8] = sigadd(x5,n5,x6,n6);
[T1,n9] = sigadd(a1*y1,n7,a2*y2,n8); % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n9,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


[x7,n10] = sigshift(xnew,n1,-1); [x8,n11] = sigshift(xnew,n1,1);
[T2,n12] = sigadd(x7,n10,x8,n11); % T{a1x1[n] + a2x2[n]}
subplot(2,1,2); stem(n12,T2); title('Subplot 2: T{a1x1[n] + a2x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Plot:

8. y[n] = x[n] - x[n -1]


Code:
[x3,n3] = sigshift(x1,n1,1); [x4,n4] = sigshift(x2,n1,1);
[y1,n5] = sigadd(x1,n1,-1*x3,n3); [y2,n6] = sigadd(x2,n1,-1*x4,n4);
[T1,n7] = sigadd(a1*y1,n5,a2*y2,n6); % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n7,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


[x5,n8] = sigshift(xnew,n1,1);
[T2,n12] = sigadd(x1,n1,-1*x5,n8); % T{a1x1[n] + a2x2[n]}
subplot(2,1,2);stem(n12,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}');xlabel('n');ylabel('x[n]');
ax = gca; ax.YAxisLocation = 'origin';

Result:
9. y[n] = x[-n]
Code:
[x3,n3] = sigfold(x1,n1); [x4,n4] = sigfold(x2,n1);
[T1,n5] = sigadd(a1*x3,n3,a2*x4,n4);
subplot(2,1,1); stem(n5,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


[T2,n6] = sigfold(xnew,n1); % T{a1x1[n] + a2x2[n]}
subplot(2,1,2); stem(n6,T2); title('Subplot 2: T{a1x1[n] + a2x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:
𝜋
10. y[n] = x[n]cos 3 n

Code:
y1 = (cos((pi/3)*n1)).* x1; y2 = (cos((pi/3)*n1)).* x2;
T1 = a1*y1 + a2*y2; % a1T{x1[n]} + a2T{x2[n]}
subplot(2,1,1); stem(n1,T1); title('Subplot 1: a1T{x1[n]} + a2T{x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

xnew = a1*x1 + a2*x2;


T2 = (cos((pi/3)*n1)).* xnew ; % T{a1x1[n] + a2x2[n]}
subplot(2,1,2); stem(n1,T2);title('Subplot 2: T{a1x1[n] + a2x2[n]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

B. Generate the input vectors for each problem using the codes below. Identify if the following systems
represented by the equations are time-invariant or not. Use k as the number of letters in your first name. (10
points each).
NOTE: All codes from numbers 1 to 10 are initialized with this code that generates x1[n], n1, and k.
rng('shuffle', 'combRecursive'); x1 = randi(15,1,21); n1 = [-10:10]; k = 15;

Also, to check if the system equation is time-invariant, equality of τ1 and τ2 is checked using this code:

if T2== T1 & m1 == m2
display('The system equation is time-invariant')
else
display('The system equation is not time-invariant')
end

Solution (This applies to all of part B.):

To check if the system is time invariant, I first excited the input x[n] then shifted it by the value of k = 15.
The result is y[n,k] = T{x[n-k]} = T1. On the other hand, I changed the value of n first by nnew = n – k (n+k in
code), then excited the new equation yielding y[n,k] = y[n-k] = T2.
If T1=T2, then the system equation is time-invariant.

1. y[n] = x[n] -x[n-1]


Code:
[x2,n2] = sigshift(x1,n1,1);
[x3,n3] = sigadd(x1,n1,x2,n2);
[T1,m1] = sigshift(x3,n3,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

nnew = n1 + k;
[x4,n4] = sigshift(x1,nnew,1);
[T2,m2] = sigadd(x1,nnew,x4,n4);
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

2. y[n] = x[-n]
Code:
[x2,n2] = sigfold(x1,n1);
[T1,m1] = sigshift(x2,n2,15);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
[T2,m2] = sigfold(x1,nnew);
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

3. y[n] = nx[n]
Code:
x2 = n1.*x1;
[T1,m1] = sigshift(x2,n1,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
T2 = nnew.*x1; m2 = nnew;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:
4. y[n] = x[n2]
Discussion:
Since the x1[n] only has 21 values, k being 15, and the position of the first point on the interval (n+k)2 is
^
(0+15) 2 = 225, which is greatly beyond the 21 values of x1[n] and cannot be made in a plot. Therefore, system is
not time invariant.

5. y[n] = x2[n]
Code:
x2 = x1.^2;
[T1,m1] = sigshift(x2,n1,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
T2 = x1.^2; m2 = nnew;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:
6. y[n] = 3x[n] + 1
Code:
x2 = 3.*x1 + 1;
[T1,m1] = sigshift(x2,n1,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
T2 = 3.*x1 + 1; m2 = nnew;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

7. y[n] = ex[n]
Code:
x2 = exp(x1);
[T1,m1] = sigshift(x2,n1,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
T2 = exp(x1); m2 = nnew;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

8. y[n] = 2x[n-2] + 5
Code:
[x2,n2] = sigshift(x1,n1,2); x3 = 2.*x2 + 5;
[T1,m1] = sigshift(x3,n2,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
[x4,n4] = sigshift(x1,nnew,2);
T2 = 2.*x4 + 5; m2 = n4;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:
9. y[n] = x[n+1] + x[n-1]
Code:
[x2,n2] = sigshift(x1,n1,-1); [x3,n3] = sigshift(x1,n1,1);
[x4,n4] = sigadd(x2,n2,x3,n3);
[T1,m1] = sigshift(x4,n4,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
[x5,n5] = sigshift(x1,nnew,-1); [x6,n6] = sigshift(x1,nnew,1);
[T2,m2] = sigadd(x5,n5,x6,n6);
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

𝜋
10. y[n] = x[n]cos 3 n
Code:
x2 = x1.*(cos((pi/3)./n1));
[T1,m1] = sigshift(x2,n1,k);
subplot(2,1,1); stem(m1,T1); title('y[n,k] = T{x[n-k]}'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';
nnew = n1 + k;
T2 = x1.*(cos((pi/3)./nnew)); m2 = nnew;
subplot(2,1,2); stem(m2,T2); title('y[n,k] = y[n-k]'); xlabel('n');
ylabel('x[n]'); ax = gca; ax.YAxisLocation = 'origin';

Result:

You might also like