0% found this document useful (0 votes)
2 views52 pages

MATLAB Programing Lab Manual

The document is a MATLAB Programming Lab Manual from Pragati Engineering College's Department of ECE, detailing procedures and experiments for familiarization with MATLAB, including matrix operations, plotting, and basic programming concepts. It covers various experiments focusing on signal processing, including the exploration of even and odd symmetries in signals, and the effects of signal parameter transformations. Each experiment includes objectives, procedures, and conclusions derived from the programming exercises.

Uploaded by

ramyaramesh8888
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views52 pages

MATLAB Programing Lab Manual

The document is a MATLAB Programming Lab Manual from Pragati Engineering College's Department of ECE, detailing procedures and experiments for familiarization with MATLAB, including matrix operations, plotting, and basic programming concepts. It covers various experiments focusing on signal processing, including the exploration of even and odd symmetries in signals, and the effects of signal parameter transformations. Each experiment includes objectives, procedures, and conclusions derived from the programming exercises.

Uploaded by

ramyaramesh8888
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pragati Engineering College

Dept. of ECE
MATLAB Programing Lab Manual

Software Used- MATLAB R2013b.

Common Procedure:-
 Open MATLAB
 Open new M-file
 Type the program
 Save in current directory
 Compile and Run the program
 For the output see command window\ Figure window
Experiment-1
Objective-

Familiarization with MATLAB

i. Matrix Operations & Plotting using MATLAB

ii. Relational Operators, Loops & Functions using MATLAB

Software Used- MATLAB R2013b.

Common Procedure:-
 Open MATLAB
 Open new M-file
 Type the program
 Save in current directory
 Compile and Run the program
 For the output see command window\ Figure window
Familiarization with MATLAB

1. Scripts
• Script: overview

Scripts are

• collection of commands executed in sequence

• written in the MATLAB editor

• saved as MATLAB files (.m extension)

• To create an MATLAB file from command-line >> edit ('I:\Pragati Engineering


College\SS\SS 2021-22\MATLAB\ex1.m')

or click for new script and save as pragati.


• Scripts: The Editor

• Some Notes on Script

• COMMENT!

• Anything following a % is seen as a comment

• Comment thoroughly to avoid wasting time later

• Note that scripts are somewhat static, since there is no input and no explicit output

• All variables created and modified in a script exist in the workspace even after it has
stopped running

Display a string or array

• Syntax disp(x)
• Example-

• x = 'Hello' ; y = [2 3 4 5]; z = rand(5,2); disp(x),disp(y),disp(z)

• Hello

• 2 3 4 5

• 0.8003 0.9595

0.1419 0.6557

0.4218 0.0357

0.9157 0.8491

0.7922 0.9340

2. making Variables
• Naming Variables

• Variable names

 first character must be a LETTER

 after that, any combination of letters, numbers and _.

 CASE SENSITIVE! (x1is different from X1)

• >> x1 = [2 7 8 6;1 2 0 5]

• >> X1

• Undefined function or variable 'X1'.

• Did you mean:

• >> x1

• i, j, ans, inf, -inf, NAN can’t be used as the variables.

• A variable can be a numerical value or a function

• >> x = 10; x1 = 2*x + 3

• x1 =
• 23

• Size of the Variable


• Size of the variable
• >> x =[1 2 3 4;9 0 8 0;1 1 2 3]; size(x)
• ans =

3 4

• Matrix
• concatenating vectors or matrices
• a = [2 1;1 0];b = [2 3 4;5 1 1];c = [2 4 0 1 1;2 0 1 1 1;1 5 2 3 1;4 2 5 1 1];d = [1;0;1;2;3;4];
• x =[[[a,b];c],d];

• x=

2 12 3 41
𝐝
𝐚 1 05 1 10

2 4 0 1 11
𝐛
2 0 1 1 1 2

1 5 2 3 13
𝐜
4 2 5 1 14

• Save Variables

• >> save [Link] ; % saved in the current directory

• >> clear

• >> load(‘[Link]')

• Name Size Bytes Class Attributes

• a 2x2 32 double

• b 2x3 48 double
• c 4x5 160 double

• d 6x1 48 double

• x 6x6 288 double

 Create a New Variable and append it to mat file

• >> e = [1 2 3 4 5 6];

• >>save(‘[Link]','e','-append');

• >>clear

• >> load(‘[Link]')

• >>whos

• Name Size Bytes Class Attributes

• a 2x2 32 double

• b 2x3 48 double

• c 4x5 160 double

• d 6x1 48 double

• e 1x6 48 double

• x 6x6 288 double

 long, short Format, and clock

• >> format long

• >> pi

• ans =

• 3.141592653589793

• >> format short (% default)

• >> pi

• ans =
• 3.1416

• >>start_time = clock & end_time = clock

shows [year, month, date, hour, minute, second ]

3. Manipulating Variables

 Arithmetic Operations

Syntax (rarely used) Alternative Meaning

plus(a,b) a+b adds matrices

minus(a,b) a-b Subtraction

mtimes(a,b) a*b Matrix Multiplication

times(a,b) a.*b Element-wise multiplication

rdivide(a,b) a./b Right array division (a divided b


element wise)

ldivide(b,a) b.\a = Left array division(a divided b


element wise)
a./b

Power(a,b) a.^b Element-wise power

mpower(a,b) a^b Matrix power

b= cumprod(a) Cumulative product (ex)

syntax Meaning

cumsum(a) Cumulative sum


diff (x) Differences and Approximate
derivatives

movsum Moving sum

prod Product of array elements

Sum Sum of array elements

ceil Round toward positive infinite

floor Round toward negative infinity

idivid Integer division with rounding option

mod Remainder after division

rem Remainder after division

round Round to nearest decimal or integer

4. Basic Plot

 2-D Plot
• >> x = linspace(0,2*pi,100);
• >> y = sin(x); plot(y) or plot(x,y)
• >> x = linspace(0,2*pi,25);
• >> y = sin(x);
• >>figure % new figure window
• >>stairs(x,y)
 Plot Multiple Lines
• x = linspace(0,2*pi,100);
• y1 = sin(x);
• y2 = sin(x-pi/4);
• figure plot(x,y1,x,y2) or plot(x,y1), hold on, plot(x,y2)
 Specify Line Style
Specify Line Style
• x = linspace(0,2*pi,100);
• y = sin(x);
• figure plot(x,y,'--')
Specify Different Line Styles for Multiple Lines
• x = linspace(0,2*pi,100);
• y1 = sin(x);
• y2 = sin(x-pi/4);
• figure plot(x,y1,'--',x,y2,':')
Specify Line Style and Color
• x = linspace(0,2*pi,100);
• y1 = sin(x);
• y2 = sin(x-pi/4);
• figure plot(x,y1,'--g',x,y2,':r')
Specify Line Style, Color, and Markers
• x = linspace(0,2*pi,25);
• y1 = sin(x);
• y2 = sin(x-pi/4);
• Figure,
• plot(x,y1,'--go',x,y2,':r*')
Plot Only Data Points
• x = linspace(0,2*pi,25);
• y = sin(x);
• Figure,
• plot(x,y,'*')
 Add Title, Axis Labels, and Legend to Graph
• x = linspace(-2*pi,2*pi,100);
• y1 = sin(x);
• y2 = cos(x);
• figure,
• plot(x,y1,x,y2)
• title('Graph of Sine and Cosine Between -2\pi and 2\pi')
• xlabel('-2\pi < x < 2\pi')
• ylabel(‘Amplitude')

 Add Legend
• legend('y = sin(x)','y = cos(x)')
• Specify Legend Location
• legend('y = sin(x)','y = cos(x)','Location','southwest')
• Specify Axis Limits
• x = linspace(-10,10,200);
• y = sin(4*x)./exp(x);
• plot(x,y) xlim([0 10]) ylim([-0.4 0.8])
5. Relational Operators

Symbol Function Equivalent Description

< lt Less than

<= le Less than or equal to

> gt Greater than

>= ge Greater than or equal to

== eq Equal to

~= ne Not equal to

6. if, elseif, and else for Conditional Assignment

Syntax

ifexpression

statements

elseifexpression

statements

else

statements

end
• Syntax

forindex = values

statements

end

7. Programme

clear all

close all

clc

dbtypess_lab_loop.m

%outstanding dues of the students in Rupees

% for 4th year students if the dues is nill then hall ticket will be issued

% for 3rd year students if the dues is less then Rs. 20,000/- hall ticket will be issued

% for 2nd year students if the dues is less then Rs. 30,000/- hall ticket will be issued

s = 6; % number of students

s_hall_ticket = zeros(1,s);

s1_year = 4; s1_fee = 2000; % year & due

s2_year = 4; s2_fee =0;

s3_year = 3; s3_fee = 5412;

s4_year = 3; s4_fee = 1000;


s5_year = 2; s5_fee = 30000;

s6_year = 2; s6_fee = 30001;

year = [s1_year s2_year s3_year s4_year s5_year s6_year];

fee = [s1_fee s2_fee s3_fee s4_fee s5_fee s6_fee];

for i = 1:s

if year(i)==4 && fee(i)==0

s_hall_ticket(i) = 1;

elseif year(i)==3 && fee(i)<20000

s_hall_ticket(i) = 1

elseif year(i)==2 && fee(i)<30000

s_hall_ticket(i) = 1;

else

end

end

[1:s;s_hall_ticket] % result

Conclusion-

1. Different matrix operations are studied.


2. Basic plotting is studied.
3. Small programe using loop is implemented.
Experiment-2
Objective-

To explore the commutation of even and odd symmetries in a signal with algebraic operations.

Software used-

Procedure-

Theory-

A signal 𝑥(𝑡) is known as odd signal if 𝑥(−𝑡) = −𝑥(𝑡) and denoted as 𝑥𝑜 (𝑡).

The signal 𝑥(𝑡) is known as even signal if 𝑥(−𝑡) = 𝑥(𝑡) and denoted as 𝑥𝑒 (𝑡).

Any signal 𝑥(𝑡)is the sum of its odd and even components𝑥𝑜 (𝑡)and 𝑥𝑒 (𝑡).

𝑥(𝑡) = 𝑥𝑜 (𝑡) + 𝑥𝑒 (𝑡)

Where

1
𝑥𝑜 (𝑡) = [𝑥(𝑡) − 𝑥(−𝑡)]
2

and

1
𝑥𝑒 (𝑡) = [𝑥(𝑡) + 𝑥(−𝑡)]
2

Program-

clearall
clc

%exp_2.m

t = -1:0.0001:1;
a = -1; % a is the exponential factor
x = exp(a*t)+exp(2*t)+cos(10*pi*t+pi/3)+sin(20*pi*t);
subplot(211);
plot(t,x),grid,
xlabel('Time'),ylabel('Magnitude'),title('x(t)'),
t1 = -t;
x1 = exp(a*t1)+exp(2*t1)+cos(10*pi*t1+pi/3)+sin(20*pi*t1);
subplot(212),
plot(t,x1),grid,
xlabel('Time'),ylabel('Magnitude'),title('x(-t)'),
x_odd = 0.5*(x-x1);
x_even = 0.5*(x+x1);
figure,
subplot(211),
plot(t,x_even),grid,
xlabel('Time'),ylabel('Magnitude'),title('Even function of x(t)'),
subplot(212),
plot(t,x_odd),grid,
xlabel('Time'),ylabel('Magnitude'),title('Odd function of x(t)'),
x_reconstruct = x_even+x_odd;
figure,
subplot(211)
plot(t,x),grid,
xlabel('Time'),ylabel('Magnitude'),title('x(t)'),
subplot(212)
plot(t,x_reconstruct),grid,
xlabel('Time'),ylabel('Magnitude'),
title('Reconstruction of x(t) from its Even and odd constituents'),
Conclusion-

From the result of the program we observed that the signal is the sum of its even and odd
components.
Experiment-3
Objective-

To explore the effect of transformation of signal parameters (amplitude-scaling, time-scaling and time-
shifting).

Software used-

Procedure-

Theory-

Time Shifting-

𝑥(𝑡)is the given signal when shifted right or left by time 𝑇 is denoted as

𝑥(𝑡 − 𝑇) → 𝑥(𝑡)shifted by right by time 𝑇.

𝑥(𝑡 + 𝑇) → 𝑥(𝑡)shifted by left by time 𝑇.

Time Scaling-

For the signal 𝑥 (𝑡), the independent variable time can be scale down (compress) or scale up (expand) by
a factor 𝑎 depending on whether 𝑎 > 1 or 0 < 𝑎 < 1 and denoted as:

𝑥 (𝑎𝑡) → 𝑥(𝑡)is compressed by factor 𝑎 for 𝑎 > 1

𝑥 (𝑎𝑡) → 𝑥(𝑡)is expand by factor 𝑎 for 0 < 𝑎 < 1.

Amplitude Scaling-

The amplitude of the signal 𝑥(𝑡) is scaled up or scaled down by a factor 𝑎 depending on whether𝑎 >
1or 0 < 𝑎 < 1.

Program-

Clear all
clc
t = -0.3:0.001:0.3;
x = exp(-t)+exp(2*t)+cos(2*5*pi*t)+sin(2*10*pi*t);
plot(t,x,'-'),
holdon,
t_shift_left = t-500*0.001;
plot(t_shift_left,x,'--'),hold on,
t_shift_right = t+500*0.001;
plot(t_shift_right,x,':');grid,
axis([-1 1 0 5]),
xlabel('Time'),
ylabel('Magnitude'),
title('signal x(t) and its time shift')
legend('x(t),','x(t) left shift','x(t) right shift'),

% Time scaling

figure,

x = exp(-t)+exp(2*t)+cos(2*5*pi*t)+sin(2*10*pi*t);
a = 2; a1 = 1/a;
% plot x(at)
t1 = a1*t; %-0.5*a1:0.002*a1:0.5*a1;
x_compress = exp(-t)+exp(2*t)+cos(2*5*pi*t)+sin(2*10*pi*t);

% plot x(t/a)
t2 = a*t;
x_expand = exp(-t)+exp(2*t)+cos(2*5*pi*t)+sin(2*10*pi*t);
plot(t,x,'k',t1,x_compress,'r',t2,x_expand,'g'),grid,
axis([-0.7 0.7 -0.6 4.5]),
legend('x(t)','x(at)','x(t/a)')
xlabel('Time'),
ylabel('Magnitude'),
title('signal x(t),x(at),x(t/a), a=2')

% Amplitude Scaling

x = exp(-t)+exp(2*t)+cos(2*5*pi*t)+sin(2*10*pi*t);
amp = 2; amp1 = 1/amp;
x1 = amp*x;
x2 = amp1*x;
figure,
plot(t,x,'k'),grid,
holdon,
plot(t,x1,'b'),
holdon,
plot(t,x2,'g'),

legend('x(t)','amp*x(t)','(1/amp)*x(t)')
xlabel('Time'),
ylabel('Magnitude'),
title('signal x(t),amp*x(t)x(at),(1/amp)x(t),amp=2')
Conclusion- Time-shifting, Time-scaling, and amplitude scaling properties are examined through
MATLAB program.
Experiment-4
Objective-

To identify a given system as linear or nonlinear.

Software used-

Procedure-

Theory-

A system is said to be linear if it satisfy the superposition theorem.

𝑥(𝑡) 𝑆𝑦𝑠𝑡𝑒𝑚 𝑦(𝑡)

Two different systems 𝑆1 and 𝑆2 are given as:

𝑦1 (𝑡) = 𝑆1 [𝑥(𝑡)] = 2𝑥(𝑡) + 𝑥(𝑡 − 1) &

𝑦2 (𝑡) = 𝑆2 [𝑥(𝑡)] = 𝑥 2 (𝑡)

Consider two different signals 𝑥1 (𝑡)&𝑥2 (𝑡)and their linear combination as:

𝑥1 (𝑡) = 𝑒 −𝑡

𝑥2 (𝑡) = 𝑒 2𝑡

𝑥3 (𝑡) = 10𝑥1 (𝑡) + 𝑥2 (𝑡) = 10𝑒 −𝑡 + 𝑒 2𝑡

The output of different systems 𝑆1 and 𝑆2 for different inputs 𝑥1 (𝑡), 𝑥2 (𝑡), and 𝑥3 (𝑡) are:

𝑦11 (𝑡) = 𝑆1 [𝑥1 (𝑡)] = 2𝑥1 (𝑡) + 𝑥1 (𝑡 − 1) = 2𝑒 −𝑡 + 𝑒 −𝑡+1

𝑦12 (𝑡) = 𝑆1 [𝑥2 (𝑡)] = 2𝑥2 (𝑡) + 𝑥2 (𝑡 − 1) = 2𝑒 2𝑡 + 𝑒 2(𝑡−1)


𝑦13 (𝑡) = 𝑆1 [𝑥3 (𝑡)] = 2𝑥3 (𝑡) + 𝑥3 (𝑡 − 1) = 2(10𝑒 −𝑡 + 𝑒 2𝑡 ) + 10𝑒 −𝑡+1 + 𝑒 2(𝑡−1)

For linear system 𝑦13 = 10𝑦11 + 𝑦12 . Otherwise non-linear.

𝑦21 (𝑡) = 𝑆2 [𝑥1 (𝑡)] = 𝑒 −2𝑡

𝑦22 (𝑡) = 𝑆2 [𝑥2 (𝑡)] = 𝑒 4𝑡

2
𝑦23 (𝑡) = 𝑆2 [𝑥3 (𝑡)] = 𝑥32 (𝑡) = (10𝑥1 (𝑡) + 𝑥2 (𝑡)) = (10𝑒 −𝑡 + 𝑒 2𝑡 )2

For linear system𝑦23 = 10𝑦21 + 𝑦22 . Otherwise non-linear.

Program-

clearall
clc

t = -2:0.0001:2;
t1 = -2:0.0001:3; %
zeros_add = zeros(1,1/0.0001);
% input signals and plot
a1 = 10; a2 = 1;
signal1 = exp(-t);
signal2 = exp(2*t);
x1 = [signal1,zeros_add];
x2 = [signal2,zeros_add];
x3 = [a1*x1 + a2*x2];
x1_shift_right = [zeros_add,signal1];
x2_shift_right = [zeros_add,signal2];
subplot(321)
plot(t1,x1),title('x1(t)'),grid % x1(t)
subplot(322)
plot(t1,x2),title('x2(t)'),grid % x2(t)
subplot(323),
plot(t1,x1_shift_right),grid % x1(t-1)
title('x1(t-1)'),
subplot(324),
plot(t1,x2_shift_right),grid
title('x2(t-1)')
subplot(325)
plot(t1,x3),grid,title('10x1(t)+x2(t)') % plot of linear sum of inputs x1(t)
& x2(t), 10x1(t)+x2(t)
% plot of output of the system S1 due to inputs x1(t) & x2(t) and its linear
sum 10x1(t)+x2(t)
figure,
y11 =2*x1 + x1_shift_right;
subplot(221)
plot(t1,y11),grid
title('y11')

y12 =2*x2 + x2_shift_right;


subplot(223)
plot(t1,y12),grid
title('y12')

y13 = 20*x1 + 2*x2 + 10*x1_shift_right + x2_shift_right; %


subplot(222)
plot(t1,y13),grid
title('y13')

y13_as_sum = 10*y11 + y12;


subplot(224)
plot(t1,y13_as_sum)
title('10y11+y12'),grid

% plot of output of the system S2 due to inputs x1(t) & x2(t) and its linear
sum 10x1(t)+x2(t)

figure,
y21 = x1.^2;
subplot(221)
plot(t1,y21)
title('y21'),grid

y22 = x2.^2;
subplot(222)
plot(t1,y22)
title('y22'),grid

y23 = (10*x1+x2).^2;
subplot(223)
plot(t1,y23)
title('y23'),grid

y23_as_sum = 10*y21 + y22;


subplot(224)
plot(t1,y23_as_sum)
title('10y21 + y22'),grid
Conclusion-

𝑦13 = 10𝑦11 + 𝑦12 . Hence the system s1 is linear.

𝑦23 ≠ 10𝑦21 + 𝑦22 . Hence the system s2 is non-linear.


Experiment-5
Objective-

Explore the time-variance and time-invariance property of a system

Software used-

Procedure-

Theory-

Two different systems 𝑆1 and 𝑆2 are given as:

𝑦1 (𝑡) = 𝑆1 [𝑥(𝑡)] = 2𝑥(𝑡)&

𝑦2 (𝑡) = 𝑆2 [𝑥(𝑡)] = 𝑡𝑥(𝑡)

The input signal (𝑡) = 𝑒 −𝑡 .

For system 𝑆1

𝑦1 (𝑡) = 𝑆1 [𝑥(𝑡)] = 2𝑥(𝑡) = 2𝑒 −𝑡

𝑦1 (𝑡, 𝑇) = 𝑆1 [𝑥(𝑡 − 𝑇)] = 2𝑥(𝑡 − 𝑇) = 2𝑒 −𝑡+𝑇

For 𝑇 = 2,

𝑦1 (𝑡, 2) = 2𝑥(𝑡 − 2) = 2𝑒 −𝑡+2

𝑦1 (𝑡 − 2) = 2𝑥(𝑡 − 2) = 2𝑒 −𝑡+2 = 𝑦1 (𝑡, 2)

For system 𝑆2

𝑦2 (𝑡) = 𝑆2 [𝑥(𝑡)] = 𝑡𝑥(𝑡) = 𝑡𝑒 −𝑡

For shift time 𝑇 = 2,

𝑦2 (𝑡, 2) = 𝑆2 [𝑥(𝑡 − 2)] = 𝑡𝑒 −𝑡+2

𝑦2 (𝑡 − 2) = (𝑡 − 2)𝑒 −𝑡+2 ≠ 𝑦2 (𝑡, 2)


Program-

Clear all
clc

s_d = 0.0001; % s_d is sample distance


t = -3:s_d:3;
t1 = -6:s_d:6; % t1 id for more duration
T = 2;
x = exp(-0.25*t); % signal x(t)
% plot x(t)
x1 = [zeros(1,length(-6:s_d:-3)-1),x,zeros(1,length(3:s_d:6)-1)]; % Input
signal x(t) for the duration t1 = -6 to 6.
x_shift_right = [zeros(1,length(-6:s_d:-1)-1),x,zeros(1,length(5:s_d:6)-1)];
% Input signal right shifted by time T i.e. x(t-T)
x_shift_left = [zeros(1,length(-6:s_d:-5)-1),x,zeros(1,length(1:s_d:6)-1)];
% Input signal left sifted by time T i.e. x(t+T)
subplot(311),
plot(t1,x1),grid,%axis([-6 6 -5 60]),
title('x(t)')
subplot(312)
plot(t1,x_shift_right),grid
ylabel('Magnitude')
title('x(t-2)')
subplot(313)
plot(t1,x_shift_left),grid
xlabel('Time')
title('x(t+2)')

% Output of the system S1 when input is shifted by time T.


figure,
subplot(211)
y1_input_shift_right = 2*x_shift_right;
plot(t1,y1_input_shift_right),grid,
ylabel('Magnitude')
title('y_1(t,T) = s_1[x(t-T)]'),
subplot(212)
y1_output_shift_right = 2*x_shift_right;
plot(t1,y1_output_shift_right),grid,
xlabel('Time'),ylabel('Magnitude')
title('y_1(t-T)'),

% Output of the system S2 when input is shifted by time T.


figure,
subplot(211)
y2_input_shift_right = 2*t1.*x_shift_right;
plot(t1,y1_input_shift_right),grid,
ylabel('Magnitude')
title('y_2(t,T) = s_2[x(t-T)]'),
subplot(212)
y2_output_shift_right = 2*(t1-2).*x_shift_right;
plot(t1,y2_output_shift_right),grid,
xlabel('Time'),ylabel('Magnitude')
title('y_2(t-T)'),
Conclusion-

For system 𝑆1

𝑦1 (𝑡 − 2) = 𝑦1 (𝑡, 2)

Therefore it is time-invariant.

For system 𝑆2

𝑦1 (𝑡 − 2) ≠ 𝑦2 (𝑡, 2)

Therefore it is time-variant.
Experiment-6
Objective-

Explore the causality and non-causality of a system.

Software used-

Procedure-

Theory-

Program-

Conclusion-
Experiment-7
Objective-

Generation of Signals & Signal Operations Synthesis of signals using Fourier Series.

Software used-

Procedure-

Theory-

Consider two periodic signals 𝑥(𝑡) and 𝑦(𝑡).

𝜋
𝑥 (𝑡) = 1 + sin(𝜔0 𝑡) + 2𝑐𝑜𝑠(𝜔0 𝑡) + 𝑐𝑜𝑠 (2𝜔0 𝑡 + ) of time-period 𝑇0 = 0.2
4

𝜋 𝜋
= 1 + sin(𝜔0 𝑡) + 2𝑐𝑜𝑠(𝜔0 𝑡) + cos ( ) 𝑐𝑜𝑠(2𝜔0 𝑡) − sin ( ) 𝑠𝑖𝑛(2𝜔0 𝑡)
4 4

= 1 + 2𝑐𝑜𝑠(𝜔0 𝑡) + 0.7071 𝑐𝑜𝑠(2𝜔0 𝑡) + sin(𝜔0 𝑡) − 0.7071 𝑠𝑖𝑛(2𝜔0 𝑡) (𝐴)

∞ ∞

= 𝑎0 + ∑ 𝑎𝑛 cos(𝑛𝜔0 𝑡) + ∑ 𝑏𝑛 sin(𝑛𝜔0 𝑡) (𝐵)


𝑛=1 𝑛=1

Comparing equation (𝐴) & (𝐵) we get


𝑎0 = 1, 𝑎1 = 2, 𝑎2 = 0.7071, 𝑏1 = 1, 𝑏2 = −0.7071

𝑦(𝑡) is a rectangular periodic pulse of fundamental time-period 𝑇0 = 4 as shown in the figure.

As 𝑦(𝑡) is an even function the coefficient 𝑏𝑛 = 0

2𝜋 𝜋
Here 𝜔0 = =
𝑇0 2

1
2
𝑎0 = ∫ 𝑥(𝑡)𝑑𝑡 = 1
𝑇0
0

1 2
4 𝜋 𝑛 = 1,5,9, …
𝑎𝑛 = ∫ 𝑥(𝑡)𝑐𝑜𝑠 (𝑛 𝑡) 𝑑𝑡 = { 𝑛𝜋
𝑇0 2 2
0 − 𝑛 = 3,7,11, …
𝑛𝜋

𝑦(𝑡) = 1 + ∑ 𝑎𝑛 cos(𝑛𝜔0 𝑡)
𝑛=1

2 2 2
= 1+ cos(𝜔0 𝑡) − cos(3𝜔0 𝑡) + cos(5𝜔0 𝑡) + ⋯
𝜋 3𝜋 5𝜋

Program-

clear all
clc

% plot of x(t)= 1 + sin(w0t) + 2cos(w0t) + cos(w0t+pi/4)

f0 = 5;
t1 = -0.5:0.0001:0.5;
subplot(311)
x = 1+sin(2*pi*f0*t1)+2*cos(2*pi*f0*t1)+cos(4*pi*f0*t1+pi/4);
plot(t1,x),grid
title('x(t)')

% plot of Rectangular Pulse


t = -4.5:0.001:6.5;
t2 = -5.5:0.001:5.5;
y = 1+square(2*pi*0.25*t,50);
subplot(312)
plot(t2,y,'r'),
axis([-6 6 -0.5 2.5]),grid
title('Rectangular Pulse Train y(t)')
y = 1+square(2*pi*0.25*t,50);
subplot(313)
plot(t2,y,'r'),
axis([-2 2 -0.5 2.5]),grid
xlabel('Time')
title('Rectangular Pulse')

% reconstructted x(t) from its FS factors.

w0 = 2*pi*f0;
x_fs = 1+2*cos(w0*t1)+0.7071*cos(2*w0*t1)+sin(w0*t1)-0.7071*sin(2*w0*t1);
figure,
plot(t1,x_fs),grid
title('x(t) reconstructed from Fourier Series Expansion')

%%% MATLAB script to demonstrate the


%%% Gibb's phenomenon..
t3=linspace(-2,2,2000);
u=linspace(-2,2,2000);
k=2;
sq=[zeros(1,500),k*ones(1,1000),zeros(1,500)];
N=[1,3,7,19,49,70];
figure,
for n=1:6;
an = [];
for m=1:N(n)
an=[an,2*k*sin(m*pi/2)/(m*pi)];
end;
fN=k/2;
for m=1:N(n)
fN= fN + an(m)*cos(m*pi*t3/2);
end;
nq=int2str(N(n));
subplot(3,2,n),plot(u,sq,'r','LineWidth',2);hold on;
plot(t3,fN,'LineWidth',2); hold off; axis([-2 2 -0.5 2.5]);grid;
xlabel('Time'), ylabel('y_N(t)');title(['N= ',nq]);
end;
Observation-

Fig-1

Fig-2
Reconstruction of Rectangular pulse from its Fourier series components

For different N values

Fig-3

Conclusion-

Fig-1 (a) and Fig-2 are exactly same i.e. the Fourier series expansion of 𝑥(𝑡) exactly same as the
signal. This is due to the smooth change of the curve.

Fig-3 shows that for rectangular pulse the Fourier series expansion does not produce the exact
signal. Due to the abrupt change of the signal the partial sum of the Fourier series in the vicinity
of discontinuities produce ripples and that ripples does not change even if more harmonics are
[Link] is Sum of large number of harmonics make the oscillation proportionally faster
and transition sharpness is more.
Experiment-8
Objective-

Convolution of discrete-Time Signals without using the build-in function conv.

Software used-

Procedure-

Theory-

𝑧(𝑛) = 𝑥 (𝑛) ∗ 𝑦(𝑛) ∑ 𝑥(𝑚 )𝑦(𝑛 − 𝑚)


𝑛=−∞

Program-

clear all
clc

dbtype exp_8.m

% convolution of two signals

x = input('Enter x: ')
y = input('Enter y: ')
x_l = length(x);
y_l = length(y);
x1 = [x,zeros(1,y_l)];
y1 = [y,zeros(1,x_l)];
for i=1:x_l+y_l-1
z(i)=0;
for j=1:x_l
if(i-j+1>0)
z(i)=z(i)+x1(j)*y1(i-j+1);
else
end
end
end
subplot(211)
stem(z);
ylabel('z[n]');
title('Convolution of Two Signals without conv function');

subplot(212)
stem(conv(x,y))
xlabel('----->n');
title('Convolution of Two Signals using conv function');
Conclusion-

The convolution of two discrete signal was calculated using the convolution sum and compared
with the build-in function conv. Both are exactly same.
Experiment-9
Objective-

Demonstrate the convolution and correlation of two discrete-time signals.

Software used-

Procedure-

Theory

For the sequences x and y the linear convolution is

𝑧(𝑛) = ∑ 𝑥 (𝑘)𝑦(𝑛 − 𝑘)
𝑘=−∞

Length of 𝑧 = length of x + length of y -1.

Circular convolution-

𝑧(𝑛) = 𝐼𝐷𝐹𝑇{𝑋(𝜔)𝑌(𝜔)}

Where

𝑋(𝜔) = 𝐷𝐹𝑇{𝑥(𝑛)} 𝑎𝑛𝑑

𝑌(𝜔) = 𝐷𝐹𝑇{𝑦(𝑛)}

Correlation-

Correlation gives the similarity between a signal and the time delayed version of the same or other
signal. Thus correlation is known as autocorrelation or cross-correlation.

For two different sequences 𝑥 (𝑛) 𝑎𝑛𝑑 𝑦(𝑛) the cross-correlation is

𝑧𝑥𝑦 (𝑙 ) = ∑ 𝑥 (𝑛)𝑦(𝑛 − 𝑙) 𝑤ℎ𝑒𝑟𝑒 𝑙 𝑖𝑠 𝑡ℎ𝑒 𝑡𝑖𝑚𝑒 − 𝑠ℎ𝑖𝑓𝑡 𝑜𝑟 𝑙𝑎𝑔


𝑛=−∞

For autocorrelation of the sequence 𝑥(𝑛)


𝑧𝑥𝑥 (𝑙 ) = ∑ 𝑥 (𝑛)𝑥(𝑛 − 𝑙)
𝑛=−∞

Program-

clear all
clc

% Linear Convolution, Circular Convolution & correlation

x = input('Enter x = ');
y = input('Enter y = ');

subplot(221)
stem(x,'filled')
title('Sequence x')
subplot(223)
stem(y,'filled')
title('Sequence y')
% Linear Convolution
subplot(222)
linear_convolution = conv(x,y);
stem(linear_convolution,'filled')
title('Linear Convolution of x & y')

% Circular Convolution
l = length(x)+length(y)-1;
xpad = [x,zeros(1,l-length(x))];
ypad = [y,zeros(1,l-length(y))];
circular_convolution = ifft(fft(xpad).*fft(ypad));
subplot(224)
stem(circular_convolution,'filled')
title('Circular Convolution of x & y')

% Cross Correlation

n1 = 21;
n = (0:n1-1)';

a = 0.4;
b = 0.7;
c = 0.9;

xabc = [a.^n b.^n c.^n];


figure,
stem3(n,1:3,xabc','filled')
ax = gca;
[Link] = 1:3;
view(37.5,30)

[cr,lgs] = xcorr(xabc,'coeff');
figure,
for row = 1:3
for col = 1:3
nm = 3*(row-1)+col;
subplot(3,3,nm)
stem(lgs,cr(:,nm),'.')
title(sprintf('c_{%d%d}',row,col))
ylim([0 1])
end
end

Figure 1: Sequences x, y, Linear and Circular Convolution

Figure 2: Discrete-time Sequences 1, 2, & 3 are used for correlations


Figure 3: Correlations between the sequences 1,2, & 3

Conclusion-

1. The circular convolution is same as the linear convolution if length of the sequences are
made equal to the length of the linear convolution by zero padding before applying DFT.
2. Correlation is maximum at 𝑙 = 0 i.e. at 0 lag.
Experiment-10
Objective-

Demonstrate the sampling in frequency domain (Discrete Fourier Transform).

Software used-

Procedure-

Theory-

Program-

Conclusion-
Experiment-11
Objective-

Demonstrate the time domain sampling of band limited signals (Nyquist theorem).

Software used-

Procedure-

Theory-

Nyquist sampling theorem states that the minimum sampling rate 𝑓𝑠 of a band-limited signal is twice of
the maximum frequency 𝑓𝑚𝑎𝑥 so that the original signal can be reconstructed from its samples i.e.

𝑓𝑠 ≥ 𝑓𝑚𝑎𝑥

The signals considered here are:

𝑥1 (𝑡) = 𝑠𝑖𝑛(50𝜋𝑡) + 𝑠𝑖𝑛(100𝜋𝑡), where maximum frequency is 50Hz

𝑥2 (𝑡) = 𝑠𝑖𝑛(100𝜋𝑡) + 𝑐𝑜𝑠(200𝜋𝑡) + 𝑐𝑜𝑠(400𝜋𝑡), where maximum frequency is 200Hz

Program-

clear all
clc

dbtype exp_11.m

sample_distance = 0.1;
N = [(1/sample_distance)*(5:5:50)];
for n=1:10
tn = 0:(sample_distance/(n*10)):0.1;
xn1 = sin(50*pi*tn)+cos(100*pi*tn);
subplot(5,2,n)
plot(tn,xn1)
nq = int2str(N(n));
title(['sampling rate = ',nq])
axis([0 0.1 -2.5 2.5])
end
title('sin(50*pi*tn)+cos(100*pi*tn)')
figure,
for n=1:10
tn = 0:(sample_distance/(n*10)):0.1;
xn = sin(100*pi*tn)+cos(200*pi*tn)+cos(400*pi*tn);
subplot(5,2,n)
plot(tn,xn)
nq = int2str(N(n));
title(['sampling rate = ',nq])
axis([0 0.1 -2.5 2.5])
end
title('sin(100*pi*tn)+cos(200*pi*tn)+cos(400*pi*tn)')

figure,
t = 0:0.0001:0.1;
x = sin(100*pi*t)+cos(200*pi*t)+cos(400*pi*t);
plot(t,x);
title('sampling rate = 10000')
axis([0 0.1 -2.5 2.5])

[audio,fs] = audioread('H:\Pragati Engineering College\SS\SS 2021-


22\MATLAB\Achyutam_bxs7w-[Link]');
sound(audio,fs)

Fig 1: 𝑥(𝑡) = 𝑠𝑖𝑛(50𝜋𝑡) + 𝑠𝑖𝑛(100𝜋𝑡). Reconstruction of 𝑥(𝑡) at different sampling


rate.
Fig 2: 𝑥(𝑡) = 𝑠𝑖𝑛(100𝜋𝑡) + 𝑐𝑜𝑠(200𝜋𝑡) + 𝑐𝑜𝑠(400𝜋𝑡). Reconstruction of 𝑥(𝑡) at
different sampling rate.

Fig 3: 𝑥(𝑡) = 𝑠𝑖𝑛(100𝜋𝑡) + 𝑐𝑜𝑠(200𝜋𝑡) + 𝑐𝑜𝑠(400𝜋𝑡). Reconstruction of 𝑥(𝑡) at the sampling


rate 10000.
Conclusion-

From Fig 1, it is observed that at sampling rate of 250 or the signal with maximum
frequency 50Hz can be reconstructed whereas fig 2 shows that sampling rate of 500 is not
enough to reconstruct the signal of maximum frequency of 200Hz. Fig 3 shows the exact
reconstruction of the signal.

The audio signal when under sampled gives noise.


Experiment-12
Objective-

Study of Laplace Transforms using MATLAB Procedure.

Software used-

Procedure-

Theory-

The Laplace transform and its inverse are defined as:

𝑋(𝑠) = 𝐿[𝑥(𝑡)] = ∫ 𝑥 (𝑡)𝑒 −𝑠𝑡 𝑑𝑡


−∞

𝑐+𝑗∞
1
𝑥 (𝑡) = ∫ 𝑋(𝑆)𝑒 𝑠𝑡 𝑑𝑠
2𝜋𝑗
𝑐−𝑗∞

where c is a constant chosen to ensure the convergence of 𝑋(𝑠).

The following time-domain signals and its Laplace transform are executed through MATLAB program.

𝑢(𝑡) 1
𝑠

𝑡𝑢(𝑡) 1
𝑠2

𝑡 𝑛 𝑢(𝑡) 𝑛!
𝑠 𝑛+1

𝑒 𝜆𝑡 𝑢(𝑡) 1
𝑠−𝜆

𝑡𝑒 𝜆𝑡 𝑢(𝑡) 1
(𝑠 − 𝜆 ) 2
𝑡 𝑛 𝑒 𝜆𝑡 𝑢(𝑡) 𝑛!
(𝑠 − 𝜆)𝑛+1

𝑐𝑜𝑠(𝑏𝑡)𝑢(𝑡) 𝑠
𝑠 2 + 𝑏2

𝑠𝑖𝑛(𝑏𝑡)𝑢(𝑡) 𝑏
𝑠2 + 𝑏2

𝑒 −𝑎𝑡 𝑐𝑜𝑠(𝑏𝑡)𝑢(𝑡) 𝑠+𝑎


(𝑠 + 𝑎 )2 + 𝑏 2

𝑒 −𝑎𝑡 𝑠𝑖𝑛(𝑏𝑡)𝑢(𝑡) 𝑏
(𝑠 + 𝑎 )2 + 𝑏 2

If laplace cannot find an explicit representation of the transform, it returns an unevaluated call.

ilaplace returns the original expression

Program-

clear all
clc

syms t s
lamda = input('Enter lamda: ');
y1 = t*heaviside(t);
z1 = laplace(y1);
simplify(z1);
pretty(ans)
%fplot(abs(z1))

y2 = (t^lamda)*heaviside(t);
z2 = laplace(y2);
simplify(z2);
pretty(ans)

y3 = exp(lamda*t)*heaviside(t);
z3 = laplace(y3)
simplify(z3);
pretty(ans)

y4 = t*(exp(lamda*t))*heaviside(t);
z4 = laplace(y4)
simplify(z4);
pretty(ans)
n5 = input('Enter n5: ');
y5 = (t^n5)*(exp(lamda*t))*heaviside(t);
z5 = laplace(y5)
simplify(z5);
pretty(ans)

% If laplace cannot find an explicit representation of the transform, it


returns an unevaluated call:

n_5 = input('Enter n5 as -ve value: ');


y_5 = (t^n_5)*(exp(lamda*t))*heaviside(t);
z_5 = laplace(y_5,t,s)
simplify(z_5);
pretty(ans)
% ilaplace returns the original expression:
i_5 = ilaplace(z_5,s,t)
simplify(i_5);
pretty(ans)

b6 = input('Enter b6: ');


y6 = cos(b6*t)*heaviside(t);
z6 = laplace(y6)
simplify(z6);
pretty(ans)

b7 = input('Enter b7: ');


y7 = sin(b7*t)*heaviside(t);
z7 = laplace(y7)
simplify(z7);
pretty(ans)

a8 = input('Enter a8: ');


b8 = input('Enter b8: ');
y8 = exp(-a8*t)*cos(b8*t)*heaviside(t);
z8 = laplace(y8)
simplify(z8);
pretty(ans)

a9 = input('Enter a9: ');


b9 = input('Enter b9: ');
y9 = exp(-a9*t)*sin(b9*t)*heaviside(t);
z9 = laplace(y9)
simplify(z9);
pretty(ans)

y_9 = ilaplace(z9,s,t)
simplify(y_9);
pretty(ans)
Conclusion-

The function ‘laplace’ and ‘ilaplace’ are used to find the Laplace transform and its inverse for
different time-domain functions.

Experiment-13
Objective-

Study of Z Transforms using MATLAB.

Software used-

Procedure-

Theory-

Program-
Conclusion-

You might also like