Sns Lab Manuals: 18-Ee-128 Musaib Ahmed

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 104

SnS Lab Manuals

18-EE-128
MUSAIB AHMED

Sub. To: Sir Hammad


LAB 01
Task 1: Evaluate the following MATLAB expressions by hand and use MATLAB to check
the answers:
a. 2/2*3
b. 6-2/5+7^2-1
c. 10/2\5-3+2*4
d. 3^2/4
e. 3^2^2

Code:
a = 2/2*3 b = 6-2/5+7^2-1 c =
10/2\5-3+2*4 d = 3^2/4 e = 3^2^2

Output:
>>task1 a
=
3b
=
53.6000 c
=
6d
=
2.2500 e
=
81
>>

Task 2: Let X = [2 5 1 6]
a) Add 16 to each element
b) Add 3 to just odd index elements
c) Compute the square root of each element

d) Compute the square of each element Code:


x = [2 5 1 6]
a = x + 16 y
= x(1)+3; z
= x(3)+3;
y=[y 5 z 6]
c = sqrt(x) d
= x.*2

Output:
>> task2 x
=
2 5 1 6a
=
18 21 17 22 b
=
5 5 4 6c
=
1.4142 2.2361 1.0000 2.4495 d=
4 25 1 36

>>

Task 3: Consider the two polynomials p(s) = s^2+2s+1 and q(s) = s+1. Using MATLAB
compute:

a) p(s) * q(s)
b) Roots of p(s) and q(s)
c) p(-1) and q(6) Code:

P = [1 2 1]
Q = [1 1] R = conv
(P,Q) roots (P) roots (Q)
X = polyval (P,-1)

Y = polyval (Q,6) Output:


>> task3 P
=

1 2 1Q
=
1 1

R=
1 3 3 1
ans =

-1
-1 ans
=
-1

X=

0
Y=
7

>>

Task 4(a): Use MATLAB Command to make partial fractions of following:


a) F(x) = (-4x+8) / (x^2+6x+8)

Code:

A = [-4 8]
B = [1 6 8] [R,P,K] = residue

(A,B) Output:

>> task4
A=
-4 8
B=

1 6 8
R=
-12

8
P=
-4

-2
K=
[]

Task 4(b): Use MATLAB Command to make partial fractions of following:


b) F(x) = (x^2+2x+3) / (x+1)^3 Code:

A = [1 2 3]
B = [1 3 3 1]

[R,P,K] = residue(A,B) Output:

>> task4b
A=

1 2 3
B=
1 3 3 1

R=
1.0000
0.0000

2.0000
P=
-1.0000

-1.0000
-1.0000
K=

[]
>>
Lab 2
Lab Task 1
1. Plot three phase voltage of magnitude 440v and 120-degree phase difference in 2D as well as
in 3D.

a) Use distinct colors (R, Y, B), X-label, Y-label, title, legend for each.

b) All plots overlapping in 1 fig.

CODE

OUTPUT
c) different window for each plot
CODE

OUTPUT
2. Plot the function: y = sin(1/x) for time interval 0-2 (Hint: use
symbolic variable and respective command)
CODE

OUTPUT

3. Plot Complex number Z = 4+5j in complex plan CODE:


OUPUT:

Lab Task 2:

I. Take matrix A of an order 3x3 and differentiate b/w A^2, A.^2, A*A
II. Define a rectangular matrix of order 5x3 having random values b/w 0 and 8
III. For vector [1.92 , .05 ,-2.43 , -.02 , .09 , .85 , .06 , -.05 , .3214 ,3, 25 , 1 ,5 ,10 , .0125
,100]
IV. Replace any value b/w 1 and 4 with 0
V. Multiply negative values with -1
VI. Add 100 for the number that larger than or equal 5
CODE:

OUTPUT:
Lab Task 3:

V1=[1 2 3 4 5 6 7 8 9 0]

V2=[0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 3.6 4.2 1.7 0.9]

V3=[4 4 4 4 3 3 2 2 2 1]

I. Derive a new vector V7 from V3, with its 7th element changed to 1.4 II.
Derive a new vector V6 from V1, with its 9th element omitted.
III. Derive a new vector V8 from V2, whose elements are prime no. indexed
values of V2

CODE

OUTPUT
LAB 3
TASK 1
i) Create a matrix of 3 by 3 with all ones, then Create matrix of 8 by 1 with all zeros,
finally Create a matrix of 5 by 2 with all elements equal to 0.37.
CODE

OUTPUT
ii) Create vector “s “containing elements of ‘x’ sorted in an ascending order. Clear x
from the workspace. From s create back a vector with the same elements and in the
same order as in x.
CODE

OUTPUT

iii) Given
the same matrix m = [1 2 3; 2 1 5; 4 6 4; 2 3 2], create matrix n with rows sorted in
a descending order of elements in the second column
CODE

OUTPUT
TASK 2
i) Given matrix A = [0, 2, 1; 3, 1, 0; 4, 6, 4; 2, 0, 2], create
a matrix with 1’s at locations where a has zeros and 0’s
elsewhere. Create a matrix containing all 0’s except the
maximum elements in each row of ‘A’.
CODE

OUTPUT
ii) Make an n × n random matrix a) calculates its inverse
b) Multiply the two back together
c) calculates the residual (difference from the desired n × n identity matrix eye(n))
CODE

OUTPUT

iii) Given a vector x = [3, 1, 4] and integer number n = 5, create matrix y containing
ntimes x(1) in 1st Row , n-times x(2) in 2nd Row and so on….then covert this matrix into
row vector V.
CODE

OUTPUT
TASK 3

CODE(i)

OUTPUT(i)

CODE(ii)

OUTPUT(ii)
TASK 4

CODE

OUTPUT OF PART(i)
OUTPUT OF PART(ii)

OUTPUT OF PART(iii)
Lab 4
Task 1:

i) Use Cramer’s Rule to solve the linear system Ax = b if

Make a function which uses “disp” command to give answer of x1, x2.

CODE and OUTPUT


ii) Now make a universal function which prompts user to enter matrix A & B of 2x2 and
2x1 respectively from user and returns A1 and A2 using both “disp” & “ fprintf ”
command. (Hint: A1 is the determinant of A but 1st column replaced by matrix “b”).
what is the difference b/w these two commands?
CODE

OUTPUT
TASK 2

PART A
PART B
PART C
Task 3
1) Write a Function statistics which used “nested” functions to prompts user to enter any
rectangular matrix and calculates its i) Mean, ii) Median and iii) standard deviation.
When you run program, it should be of the form… >> statistics Please enter the
matrix: ….

For Given Matrix the “mean is: ….”

“median is: …. “

“standard deviation is: …”

2) Create a function in MathScript that convert a given temperature from degrees


Celsius to degrees Fahrenheit. Take 10 different values from user and display it in
three different ways….1) Display 2) fprintf 3) plot “ Challenge” can you print a table
of given data?
3) Write a function that prints out the binary equivalent of a number prompt from user.
Lab 5
Task 1

i) write a function “sum m_to_n” using for loop that calculate the sum of integers from
m to n prompted by user. Display it as: m=5, n=8, Result is 5+6+7+8=26.
You cannot use built in function “sum”.
CODE and OUTPUT
ii) write a function “min_max_val” to find the minimum & maximum value in a
vector prompted by user. You can only use “if” & “for”. Output should be as:
“The minimum so far is the x element in the vector & maximum is y element
in the vector”, where x & y are the index of minimum and maximum value
respectively.
NOTE: you can’t use built in function “min” or “max”
CODE and OUTPUT
Task 2

CODE and OUTPUT of part(i)


CODE and OUTPUT of part(ii)
Task 3

Part(ii)
Part(iii)
LAB 6 Part (a)
Task 1:
Consider the continuous time signal x(t) = Ceβt, 0 ≤ t ≤ 14. Plot this signal with step size of
0.01 for the following values of C and β.
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)

i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1
iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = ------------------------------------------------------------------------------------------------ 43
vii. C = 0.5, β = -1 viii. C = 2, β = -----------------------------------------------------------------------43
ix. C = 1, β = -0 --------------------------------------------------------------------------------------------- 43

x. C = 1, β = -2

Write down your observations about the results?


OUTPUT:

Task 2:
Consider the discrete time signal x(n) = Ceβn, 0 ≤ n ≤ 14. Plot this signal for the following
values of C and β.
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)
i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1
iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = -1
vii. C = 0.5, β = -1
viii. C = 2, β = -1
ix. C = 1, β = -0.5
x. C = 1, β = -2

Write down your observations about the results?


OUTPUT:

OBSERVATIONS:
Task 3:
Consider the continuous time signal x(t) = Cejβt, 0 ≤ t ≤ 14. Plot this signal with step size of
0.01 for the following values of C and β.
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)

i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1 iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = -1
vii. C = 0.5, β = -1
viii. C = 2, β = -1 ix. C = 1, β = -0.5
x. C = 1, β = -2

Write down your observations about the results?


OUTPUT:

OBSERVATIONS:
Task 4: Consider the discrete time signal x(n) = Cejβn, 0 ≤ n ≤ 14. Plot this signal for the
following values of C and β.
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)

i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1 iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = -1
vii. C = 0.5, β = -1
viii. C = 2, β = -1 ix. C = 1, β = -0.5
x. C = 1, β = -2 Write down your
observations about the results?
OUTPUT:

OBSERVATIONS:
Task 5: Consider the continuous time signal x(t) = Ce (γ + jβ)t, 0 ≤ t ≤ 20. Plot this signal
with step size of 0.01 for the following values of C and β. (Take γ = 0.2)
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)

i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1 iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = -1
vii. C = 0.5, β = -1
viii. C = 2, β = -1 ix. C = 1, β = -0.5
x. C = 1, β = -2

Write down your observations about the results?


OUTPUT:

OBSERVATIONS:
Task 6: Consider the discrete time signal x(n) = Ce (γ + jβ)n, 0 ≤ n ≤ 20. Plot this signal for
the following values of C and β. (Take γ = 0.2)
(Paste single Output figure use subplot command with proper title,xlabel,ylabel. Use the
command “suptitle” for whole figure title)
i. C = 1, β = 1
ii. C = 0.5, β = 1
iii. C = 2, β = 1 iv. C = 1, β = 0.5
v. C = 1, β = 2
vi. C = 1, β = -1
vii. C = 0.5, β = -1
viii. C = 2, β = -1 ix. C = 1, β = -0.5
x. C = 1, β = -2

Write down your observations about the results?

CODE:
OUTPUT :

OBSERVATIONS:
LAB 6 PART b

Task 7:
Generate the following Continuous time signals in MATLAB using the built-in functions
“sawtooth” and “square”. (Mention Time period in each plot and keep in mind the amplitude
value of any signal should not be less than zero at any instant. Paste single Output figure use
subplot command with proper title,xlabel,ylabel)

i. Periodic Triangular wave. ii.


Periodic Sawtooth wave. iii.
Periodic Square wave.

OUTPUT:
Task 8:
Write a MATLAB code for this expression. Output should contain single figure with three plots
x1(n) in subplot(2,2,1) plots x2(n) in subplot(2,2,2) plots y(n) in subplot(2,2,[3 4]). Label the
axis and title every subplot.(Use the command “suptitle” for whole figure title) y(n) = x1(n)
+ x2(n), wherex1(n) = 2δ(n-3) & x2(n) = -δ(n+1)
CODE

Task 9:
Plot(2D & 3D both) the following signals in MATLAB. Determine whether or not the
following signals are periodic. If a signal is periodic, specify its fundamental period. Explain
the results accordingly. (Paste single Output figure for each part use subplot command with
proper title,xlabel,ylabel)
(Exercise Problem 1.9)
i. x1(t) = jej10t
ii. x2(t) = je(-1+j)t
iii. x3(n) = ej7лn iv. x4(n) = 3ej3л(n+1/2)/5
OUTPUT:
Task 10:
Consider the discrete time signal x(n) = u(n), -6 ≤ n ≤ 7.
Write a MATLAB code and verify that a signal is composed of two parts Even & Odd .
Explains the steps involved in it. (Paste single Output figure use subplot command with proper
title,xlabel,ylabel)
CODE:
Task 1:
Compute the convolution (by using built-in function “conv” and theoretically verify your
answer) for the following Discrete time signals. Output should contain single figure with three
plots x(n) in subplot(2,2,1) plots h(n) in subplot(2,2,2) plots y(n), the convolved output, in
subplot(2,2,[3 4]). Label the axis and title each subplot. (Use the command “suptitle” for whole
figure title)

i. x[n] = u[n] – u[n-5] & h[n] = 0.7n, (0 ≤ n ≤ 5).


. ii. x[n] = δ[n] + 2δ(n-1) & h[n] = 2δ[n+1] +
2δ[n-1].

Write down your observations about the results with theoretical verification?

CODE part(i)
OUTPUT part(i)

CODE part(ii)
OUTPUT part(ii)
Task 2:
Suppose that the impulse responses of the sub-systems S1 and S2, that are connected as shown
in figure below, are h1[n] = [2 3 4 5 7], 0 ≤ n ≤ 4; h2[n] = [-2 3 -4 0 5], 0 ≤ n ≤ 4, respectively.
Perform the following tasks. (use the built-in function “conv” if needed)

i. Compute impulse response of the overall system.


ii. Compute the response of the overall system to the given input signal x[n] = u[n] –
u[n5].
iii. Plot all graphs z[n], v[n], s[n], p[n], r[n], k[n], y[n]. (given x[n] = u[n] – u[n-5])
iv. Determine if the overall system is BIBO stable or not.

CODE
OUTPUT
Task 3:
Define Correlation coefficient, Auto-correlation, Cross-correlation. Write down their
mathematical expressions. Write an expression that relates convolution with correlation.
Perform the following tasks and explain each result from its output plot.
i. x[n] = [1 2 3 4 5], Plot the Auto-correlation.
ii. x[n] = [1 2 3 4 5], y[n] = [1 2 3 4 5], Plot the Cross-correlation. iii. x[n] = [1
2 3 4 5], y[n] = [1 2 3 0 6], Plot the Cross-correlation.
iv. x[n] = [1 2 3 4 5], y[n] = [-1 -2 -3 -4 -5], Plot the Cross-correlation.

Use the following built-in functions (“xcorr” & “stem”).


CODE
OUTPUT
Optional Task (5 absolute bonus marks)
Write a function that will perform convolution without using the ‘conv’ command. Try to
minimize the use of other built-in functions as well.
There is an old saying that only sure things in life are death and taxes. We can add one more
certainty to that list: if you write a program of any significant size it will not work for the first
time you will try it. Errors in programs are known as bugs & the process of locating and
eliminating them is known as debugging. (MATLAB Debugging tool is very useful for this
task implementation)
CODE:
OUTPUT:
Lab Manual 08

Tasks 1: Consider the following periodic signals with T = 1, wo = . Plot this signal in time
domain. Determine its Fourier series coefficient using analysis equation and verify your
result from MATLAB (magnitude and phase plot of a k). Now use the calculated Fourier
series coefficient and apply Synthesis equation compare the synthesized signal with original
signal plot: -
i. x1(t) = cos(wot)
CODE
t0 = 0; T = 1;
w=2*pi/T; syms t; x =
cos(w*t);
subplot(2,2,1)
ezplot(x,[t0 t0+T]);
title('Original Signal')
k = 10;
a = analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis
equation')
suptitle({'Cos(w*t)'})
PLOT
ii. x2(t) = sin(wot) (Book example 3.3)
CODE
t0 = 0; T = 1; w=2*pi/T; syms
t; x = sin(w*t); subplot(2,2,1)
ezplot(x,[t0 t0+T]);
title('Original Signal') k =
10; a =
analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis
equation')
suptitle({'Sin(w*t)'})

iii. x3(t) = 1 + cos(wot) + cos(2wot) + cos(3wot) (Book example 3.2)

2|Page Signal&SystemLabManual
CODE
t0 = 0; T
= 1;
w=2*pi/T;
syms t;
x = 1+((1/2)*cos(w*t))+cos(2*w*t)+(2/3)*cos(w*t);
subplot(2,2,1) ezplot(x,[t0 t0+T]);
title('Original Signal') k = 10; a =
analysiss(T,x,k,t0,t); subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|'); subplot(2,2,3)
stem([-k:k],angle(a)); title('\angle a_k'); xx =
synthesis(k,a,T,t0,t); subplot(2,2,4)
ezplot(xx,[t0 T]) title('By synthesis
equation')
suptitle({'1+((1/2)*cos(w*t))+cos(2*w*t)+(2/3)*cos(w*t)'})

iv. x4(t) = 1 + sin(wot) + 2cos(wot) + cos(2wot + ) (Book example 3.4)

3|Page Signal&SystemLabManual
CODE
t0 = 0; T
= 1;
w=2*pi/T;
syms t;
x = 1 + sin(w*t) + 2*cos(w*t)+cos(2*w*t +pi/4 );
subplot(2,2,1) ezplot(x,[t0 t0+T]);
title('Original Signal') k = 10;
a = analysiss(T,x,k,t0,t); subplot(2,2,2) stem([-k:k],abs(a));
title('|a_k|'); subplot(2,2,3) stem([-k:k],angle(a)); title('\angle
a_k'); xx = synthesis(k,a,T,t0,t); subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis equation') suptitle({'1 + sin(w*t) +
2*cos(w*t)+cos(2*w*t +pi/4 )'})
PLOT

4|Page Signal&SystemLabManual
Tasks 2: Take T = 4, and pass this command for input signal, x= heaviside(t-(T/2)). Plot this
signal in time domain. Calculate wo and comment on signal shape. Determine its Fourier
series coefficient using analysis equation and verify your result from MATLAB (magnitude
and phase plot of ak). Now use the calculated Fourier series coefficient and apply Synthesis
equation compare the synthesized signal with original signal plot. Increase the range(value)
of k and write down your observations. (Book example 3.5)

CTFS (K=20) CODE


t0 = 0; T = 4;
w=2*pi/T; syms t; x=
heaviside(t-(T/2));
subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal')
k = 20;
a = analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis
equation')
suptitle({'CTFS'})
CTFS (K=20) PLOT

CTFS (K=59) CODE

5|Page Signal&SystemLabManual
t0 = 0; T = 4;
w=2*pi/T; syms t; x=
heaviside(t-(T/2));
subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal')
k = 59;
a = analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis
equation') suptitle({'CTFS'})

Tasks 3: Take T = 3, and input signal x(t) = e-t. Plot this signal in time domain. Calculate wo
and comment on signal shape. Determine its Fourier series coefficient using analysis
equation in MATLAB (magnitude and phase plot of ak). Now use the calculated Fourier
series coefficient and apply Synthesis equation compare the synthesized signal with original
signal plot. Increase the range(value) of k and write down your observations.

6|Page Signal&SystemLabManual
CTFS (FOR K=5) CODE
t0 = 0; T = 3;
w=2*pi/T; syms t; x
= exp(-t);
subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal')
k = 5;
a = analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0 T])
title('By synthesis equation')
suptitle({'CTFS k=5'})

CTFS (FOR K=59) CODE


t0 = 0; T = 3; w=2*pi/T; syms
t; x = exp(-t); subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal') k = 59; a
= analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-k:k],abs(a));
title('|a_k|'); subplot(2,2,3)
stem([-k:k],angle(a));
title('\angle a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0 T])
title('By synthesis equation')
suptitle({'CTFS k=59'})

7|Page Signal&SystemLabManual
Tasks 4: Consider the following three continuous time periodic signals with T = 1 that
is wo = 4 . Plot this signal in time domain. Determine its Fourier series coefficient using
analysis equation and verify your result from MATLAB (magnitude and phase plot of
ak). (Book problem 3.25)

x(t) = cos(wot)
y(t) = sin(wot)
z(t) = x(t)y(t)

CODE
t0 = 0; T = 4; w=2*pi/T; syms t; x=
cos(w*t); subplot(2,2,1)
ezplot(x,[t0 t0+T]) title('Original
Signal') k = 20; a =
analysiss(T,x,k,t0,t); subplot(2,2,2)
stem([-k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-k:k],angle(a));
title('\angle a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0 T])

8|Page Signal&SystemLabManual
title('By synthesis equation')
suptitle({'x(t)=Cos(w*t)'})
figure t0 = 0; T = 4;
w=2*pi/T; syms t; x=
sin(w*t); subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal') k =
20; a =
analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a)); title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a)); title('\angle
a_k'); xx =
synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0
T]) title('By synthesis
equation') suptitle({
,'y(t)=Sin(w*t)'})
figure
t0 = 0;
T = 4;
w=2*pi/T;
syms t;
x= sin(w*t)*cos(w*t);
subplot(2,2,1)
ezplot(x,[t0 t0+T])
title('Original Signal') k
= 20;
a = analysiss(T,x,k,t0,t);
subplot(2,2,2) stem([-
k:k],abs(a));
title('|a_k|');
subplot(2,2,3) stem([-
k:k],angle(a));
title('\angle a_k');

xx = synthesis(k,a,T,t0,t);
subplot(2,2,4) ezplot(xx,[t0 T])
title('By synthesis equation')
suptitle({'z(t)=X(t)*Y(t)'})

CTFS (X) PLOT:

9|Page Signal&SystemLabManual
CTFS (Y) PLOT:

CTFS (Z) PLOT:

10 | P a g e Signal&SystemLabManual
11 | P a g e Signal&SystemLabManual
Lab Manual 09

Lab Tasks:

Tasks 1: Implement the eq 3.94 (from Oppenheim book) in MATLAB and paste your code
here.

function x=synthesis(k,ak,N,n)
wo=2*pi/N; xx=ak*exp(j*k*wo*n)
x=symsum(xx,k,-N,N) end

Tasks 2: Implement the eq 3.95 (from Oppenheim book) in MATLAB and paste your code
here.

function ak=analysiss(k,x,N,n)
wo=2*pi/N; y=x*exp(-j*k*wo*n);
yy=symssum(y,n,-N,N);
ak=1/N*yy; end

Tasks 3: Explain briefly each parameter involved in eq 3.94 and eq 3.95


The given equations have the same role for Discrete-Time Periodic Signals just like the equations. (3.38)
and (3.39) Oppenheim book have for Continuous-Time Periodic Signals, where equation (3.94) is the
synthesis equation for the Discrete Time Periodic signal and equation (3.95) is the Analysis Equation for the
Discrete Time Periodic Signal..
As in continuous time, the discrete-time Fourier series coefficients ak are often referred to as the spectral
coefficients of x[n]. These coefficients specify a decomposition of x[ n] into a sum of N harmonically
related complex exponentials. Referring to equation (3.88) from the Oppenheim book, we will see that if
we take k in the range from 0 to N - 1, we have
Similarly, if k ranges from 1 to N, we obtain

so, from equation (3.86) from the Oppenheim book, 0[n] = N[n], and upon comparing equations (3.96) and
(3.97) both from Oppenheim book, we can say that a0 = aN. Similarly, by letting k range over any set of
N consecutive integers and using equation (3.86) Oppenheim book, we can conclude that

Now, if we consider more than N sequential values of k, the values ak repeat periodically with period N.
Since there are only N distinct complex exponentials that are periodic with period N, the Discrete-Time
Fourier series representation is a finite series with N terms. Therefore, if we fix the N consecutive values of
k over which we define the Fourier series in equations (3.94) Oppenheim book, we will obtain a set of
exactly N Fourier coefficients from equation (3.95). On the other hand, at times it will be convenient to
use different sets of N values of k, and consequently,
Lab Manual 10

Tasks 1: Consider the following three continuous time periodic signals with T = 1 i-e wo =𝟐𝝅/𝑻
. Plot this signal in time domain. Perform frequency domain analysis on z(t) using fft command
(1) x(t) = cos(wot)

CODE
w0=2*pi; t=0:0.01:1; x=cos(w0*t);
subplot(2,1,1)
plot(t,x,'r','linewidth',2)
xlabel('Time') ; ylabel('x(t)=
Cos(w0*t)') ; suptitle({ })
title('Time Domain Representation');
w=-length(t)/2:length(t)/2-1;
y=fftshift(fft(x)/length(x));
subplot(2,1,2)
plot(w+1/2,abs(y),'linewidth',2)
axis([-10 10 0 1])
xlabel('Frequency(Hz)')
ylabel('Magnitude Response')
title('Frequency Domain Representation')

PLOT
(2) y(t) = sin(wot)
CODE
w0=2*pi; t=0:0.01:1; x=sin(w0*t);
subplot(2,1,1)
plot(t,x,'r','linewidth',2)
xlabel('Time') ; ylabel('y(t)=
Sin(w0*t)') ; suptitle({ })
title('Time Domain Representation');
w=-length(t)/2:length(t)/2-1;
y=fftshift(fft(x)/length(x));
subplot(2,1,2)
plot(w+1/2,abs(y),'linewidth',2)
axis([-10 10 0 1])
xlabel('Frequency(Hz)')
ylabel('Magnitude Response')
title('Frequency Domain Representation')

PLOT

(3) z(t) = x(t)y(t)


CODE
w0=2*pi; t=0:0.01:1;
x=sin(w0*t).*cos(w0*t);
subplot(2,1,1)
plot(t,x,'r','linewidth',2)
xlabel('Time') ; ylabel('z(t)=
Sin(w0*t),*Cos(w0*t)') ; suptitle({
}) title('Time Domain
Representation'); w=-
length(t)/2:length(t)/2-1;
y=fftshift(fft(x)/length(x));
subplot(2,1,2)
plot(w+1/2,abs(y),'linewidth',2)
axis([-10 10 0 1])
xlabel('Frequency(Hz)')
ylabel('Magnitude Response')
title('Frequency Domain Representation')

PLOT

Tasks 2: Write the use of following commands?

• fftshift
• fft
• abs
• length

• fftshift

It is useful for visualizing a Fourier transform with the zero-frequency component in the middle
of the spectrum. For vectors, fftshift(X) swaps the left and right halves of X . For matrices,
fftshift(X) swaps quadrants one and three of X with quadrants two and four

• fft
A common use of Fourier transforms is to find the frequency components of a signal
buried in a noisy time domain signal. Consider data sampled at 1000 Hz. Form a signal
containing a 50 Hz sinusoid of amplitude 0.7 and 120 Hz sinusoid of amplitude 1 and
corrupt it with some zero-mean random noise. It is difficult to identify the frequency
components by looking at the original signal. Converting to the frequency domain, the
discrete Fourier transform of the noisy signal y is found by taking the fast Fourier
transform (FFT).
• Abs
Absolute value and complex. Y = abs(X) returns the absolute value of each
magnitude abs(X) returns the complex magnitude
element in array X. If X is complex,
• Length
L = length(X) returns the length of the largest array dimension in X. For vectors, the
length is simply the number of elements. For arrays with more dimensions, the length
is max(size(X)). The length of an empty array is zero.
Lab Manual 11

Lab Tasks

Task 1:
i) Evaluate in the range 0 ≤ ω ≤ π the following DTFT:

CODE
clf; %Clear all figures clear
all; %Clear all variables
close all; %Close all figures
% Compute the frequency samples of the DTFT w
= 0:0.01:pi;
num = [0.7 -0.5 0.3 1]; %Nominator coefficients den
= [1 0.3 -0.5 0.7]; %Denominator coefficients h =
freqz(num, den, w); %Compute DTFT
% Plot the DTFT subplot(2,1,1);
plot(w/pi,real(h)); grid;
suptitle({ }) title('Real part of
H(e^{j\omega})') xlabel('\omega
/\pi'); ylabel('Amplitude');
figure(1); subplot(2,1,2);
plot(w/pi,imag(h)); grid;
title('Imaginary part of H(e^{j\omega})')
xlabel('\omega /\pi'); ylabel('Amplitude');
figure(2); subplot(2,1,1);
plot(w/pi,abs(h)); grid; suptitle({ })
title('Magnitude Spectrum |H(e^{j\omega})|')
xlabel('\omega /\pi'); ylabel('Amplitude');
subplot(2,1,2); plot(w/pi,angle(h)); grid;
title('Phase Spectrum arg[H(e^{j\omega})]') xlabel('\omega
/\pi');
ylabel('Phase,radians');

OUTPUT
Task 2:
i) Generate a linearly increasing chirp signal of your own choice and plot its magnitude
and phase response. Your observations?
CODE
t= 0:0.001:5; freqz(
chirp(t,rand),t);
title( )

ii) What is the difference b/w MATLAB commands “freqs” and “freqz” Freqz:
[h,w] = freqz(b,a,n);

returns the n-point frequency response vector, h, and the corresponding angular frequency vector,
w, for the digital filter with numerator and denominator polynomial coefficients stored in b and
a, respectively
Freqs:
Freqs returns the complex frequency response H(jω) (Laplace transform) of an analog filter

given the numerator and denominator coefficients in vectors b and a. h = freqs(b,a,w) returns
the complex frequency response of the analog filter specified by coefficient vectors b and a
freqs evaluates the frequency response along the imaginary axis in the complex plane at the
angular frequencies in rad/s specified in real vector w, where w is a vector containing more
than one frequency.
Task 3:

i) Add three cosine signals of frequencies 2, 5 & 10 Hz and name that signal as T. Could you
find these frequencies by frequency domain analysis of T? Now add some random noise
in T and observe the Magnitude and Phase Spectrums

CODE
t= 0:0.01: 2*pi;
T= cos(2*t)+ cos(5*t)+ cos(10*t);
figure(1);
freqz(T,t);
suptitle({ })
title('Without Noise')
figure(2); x=
10*rand(1,629);
T0= T+x;
freqz(T0,t);
suptitle({ })
title('With Noise')
ii) if x[n]= [1 1 0 1] is the input to a system and output is y[n]= [5 4 3 2 1 0] .Perform the
frequency domain analysis of this system.

CODE
x= [1 1 0 1]; y= [5 4
3 2 1 0]; h=
deconv(y,x);
freqs(h,0:length(h-1))
suptitle({ })
Lab Manual 12

Task 1:-
Given below is code your task is to write down this code in MATLAB observe each parameter of this
code and comment on it briefly.

% Record your voice for 5 seconds. fs=1000; recObj =


audiorecorder(fs,16,1);
disp('Start speaking.') recordblocking(recObj, 5); disp('End of Recording.'); play(recObj);
% Play back the recording. myRecording = getaudiodata(recObj); % Store data in
double-precision array.
plot(myRecording) % Plot the waveform.
PLOT

COMMENTS
Following are the few comments about the code
(1) audiorecorder(fs,16,1)
Its is used to record audio data from input device such as microphone for processing in
MATLAB where
• Sampling frequency Fs = 8000 hertz
• Bits per sample nBits = 8
• Number of channels nChannels = 1
(2) disp('Start speaking.')
It displays ‘start speaking’ in command window
(3) disp('End of Recording.');
It displays ‘End of Recording ‘ in the command window
(4) recordblocking(recObj, 5);
recordblocking(recorderObj, length) records audio from an input device, such as a microphone connected to your
system, for the number of seconds specified by length. The recordblocking method does not return control until
recording completes

(5) play(recObj);
It plays back the recorded signal

(6) plot(myRecording)
It plots the graph of our recording in matlab

LESSON LEARNED
In this lab we learnt about the following things
We learnt about the creating of different signals giving different sampling frequencies
We learnt about aliasing and effect of underdamped and overdamped
We learnt about different audio commands in MATLAB like soundsc,audiorecord etc

2|Page Signal&SystemLabManual
Lab Manual 13
Task 1: Write a MATLAB code that
• Reads a colored image
• Convert it into gray scale image using rbg2gray ().
• Generates the flipped image of original image using flipud built in command.
• Your results should look like the picture below.
• Crop a part of image using imcrop built in command.
• Convert Gray image to binary using built in command im2bw.
• Rotate an image using built in command imrotate.

CODE
img = imread('download.jpg');
Ir = flipdim(img,2);
gr=rgb2gray(img);
up=flipud(img);
crp=imcrop(up,[35 0 55 50]);
bw= im2bw(crp);
subplot(4,2,[1 2]);
imshow(img); suptitle({ })
title('image')
subplot(4,2,3); imshow(gr);
title('gray')
subplot(4,2,4); imshow(up);
title('up-side-down')
subplot(4,2,5); imshow(crp);
title('crop')
subplot(4,2,6); imshow(bw);
title('black&white')
subplot(4,2,7); imshow(Ir);
title('Mirror')
OUTPUT

 Imread command is selecting a image from the same folder we are saving the particular
matlab code
 Imshow command is used for showing that image
 Flipud command is used to flip the image upside down
 Imcrop command is used to crop that image
 Rgb2gray command is used to convert that image in to gray color
 Im2bw command is used to show that image into black and white colors only
Task 2: Use functions like zeros and ones and create a check pattern.
CODE

a=[ones(1,200);zeros(1,200)]; for
i=0:59
a=[a;ones(1,200);zeros(1,200)]; end
imshow(a); suptitle({ ,'Check
mate'});
OUTPUT
Task 3:
Draw the below model in Simulink and display the spectrum analyzer output. Set the
spectrum analyzer block in order to get the correct output.

You might also like