0% found this document useful (0 votes)
7 views9 pages

Signals and Systems - Lab - Work - 2

The document discusses various MATLAB commands and their results, including the use of loops and vectorized operations. It compares the efficiency of scripts and functions in MATLAB, providing examples of each and highlighting their differences in execution. Additionally, it covers plotting continuous and discrete-time signals, demonstrating how to create and customize plots using MATLAB.

Uploaded by

Ketankeshvala
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)
7 views9 pages

Signals and Systems - Lab - Work - 2

The document discusses various MATLAB commands and their results, including the use of loops and vectorized operations. It compares the efficiency of scripts and functions in MATLAB, providing examples of each and highlighting their differences in execution. Additionally, it covers plotting continuous and discrete-time signals, demonstrating how to create and customize plots using MATLAB.

Uploaded by

Ketankeshvala
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
You are on page 1/ 9

KESHVALA KETAN U. Enrollment No.

: 240283111015
8

LAB Work 2
(A) What are the results of these sets of commands? Think them over and run them with
Matlab to see if you are right.
(1)
C = 7:2:22
Num=0;
while (all (C > 0));
C= C-3;
Num=Num+1;
end
C
Num
Code:

C = 7:2:22
Num=0;
while (all (C > 0));
C= C-3;
Num=Num+1;
end
C
Num
Command Window:-

C = -2 0 2 4 6 8 10 12
Num = 3

(2) Situations under which loops can be avoided. Does the set of Commands:

for i = 1:20
H(i) = i *5;
end

Command Window:-

Elapsed time is 0.017058 seconds.

have the same result as:

H= 1:20;
H= H*5;

Command Window:-

Signals and Systems EC-LDCE 14


KESHVALA KETAN U. Enrollment No. : 240283111015
8

Elapsed time is 0.001227 seconds.

Does the set of commands:

tic
for n = 1:100000
x(n) = sin(n*pi/10);
end
toc

Command Window:-

Elapsed time is 0.026934 seconds.

have the same result as:

tic
n = 1:100000;
x = sin(n*pi/10);
toc

Command Window:-

Elapsed time is 0.005434 seconds.

(B) Compare a script and a function

1) Write a script: In the main menu of Matlab, select file -> new -> M-file A new
window will pop up. Input the following commands:

x = 1:5;
y = 6:10;
g = x+y;

Command Window:-

g =
7 9 11 13 15

and then save the file as myprogram.m under the default path matlab/work

2) Write a function:
Create a new m file following the procedure of above.
Type in the commands:

function g = myfunction(x,y)
g = x + y;

Signals and Systems EC-LDCE 15


KESHVALA KETAN U. Enrollment No. : 240283111015
8

and then save it as myfunction.m

Command Window:-

ans =
7 9 11 13 15

3) Compare their usage

(1) run the commands one by one:


myprogram

x
y
g
z = myprogram (error?)

Command Window:-

Error in myprogram (line 4)


z = myprogram
unrecognized function or variable ‘myprogram’.

(2) Run command clear to remove all variables from memory

Clear;

(3) Run the commands one by one:

x = 1:5;
y = 6:10; (error?)
z = myfunction(x,y)
g
a = 1:10;
b = 2:11;
myfunction(a,b)

Command Window:-

z =
7 9 11 13 15
Unrecognized function or variable 'g'.

(a) Plotting Continuous-Time Signals


For the following:

Signals and Systems EC-LDCE 16


KESHVALA KETAN U. Enrollment No. : 240283111015
8

Run the following three lines and explain why the plots are different.

t = 0:2*pi; plot(t,sin(t))

t = 0:0.2:2*pi; plot(t,sin(t))

t = 0:0.02:2*pi; plot(t,sin(t))

For the last graph, add a title and axis labels with:

title(‘Sinusoidal Signal’)

xlabel(’t (Seconds)’)

ylabel(’y(t)’)
t = 0:2*pi; plot(t,sin(t))

t = 0:0.2:2*pi; plot(t,sin(t))

t = 0:0.02:2*pi; plot(t,sin(t))

Signals and Systems EC-LDCE 17


KESHVALA KETAN U. Enrollment No. : 240283111015
8

Change the axis with:

axis([0 2*pi -1.2 1.2])

Put two plots on the same axis:

t = 0:0.2:2*pi; plot(t,sin(t),t,sin(2*t))

Produce a plot without connecting the points:

Signals and Systems EC-LDCE 18


KESHVALA KETAN U. Enrollment No. : 240283111015
8

t = 0:0.2:2*pi; plot(t,sin(t),’.’)

Try the following command:

t = 0:0.2:2*pi; plot(t,sin(t),t,sin(t),’r.’)

What does the r do?


"r--" is a line specification. Each specification can include characters for the line colour, style, and
marker. A marker is a symbol that appears at each plotted data point, such as a +, o, or *. For
example, " g:*" requests a dotted green line with * markers.

(b) Plotting Discrete-Time Signals

Use stem to plot the discrete-time step-function:


n = -10:10;
f = n >= 0;
stem(n,f)

Make stem plots of the following signals.


Decide for yourself what the range of n should be.

Signals and Systems EC-LDCE 19


KESHVALA KETAN U. Enrollment No. : 240283111015
8

f(n) = u(n) − u(n − 4)

n = -10:10
f = (n >= 0) – (n >= 4);
stem(n,f);
xlabel(“time”)
ylabel(“magnitude”)
title(‘f(n) = u(n) – u(n – 4)’)

g(n) = n · u(n) − 2 (n − 4) · u(n − 4) + (n − 8) · u(n − 8)

n = -10:10
g = (n.* (n >= 0)) – (2 * (n – 4) .*(n >= 4)) + ((n – 8) .* (n >= 8));
stem(n,g);
xlabel(“time”)
ylabel(“magnitude”)
title(‘g(n) = n.u(n)–2(n-4)u(n-4) + (n-8)u(n–8)’)

x(n) = δ(n) − 2 δ(n − 4)

n = -10:10
x = (n == 0) – 2*(n == 4);
Signals and Systems EC-LDCE 20
KESHVALA KETAN U. Enrollment No. : 240283111015
8

stem(n,x);
xlabel(“time”)
ylabel(“magnitude”)
title(‘x(n) = δ(n) – 2 δ(n – 4)’)

y(n) = (0.9)n (u(n) − u(n − 20))

n = -10:10
y = (0.9 .^ n) .* ((n >= 0) – (n >= 20));
stem(n,y);
xlabel(“time”)
ylabel(“magnitude”)
title(‘(0.9)n(u(n) – u(n – 20))’)

v(n) = cos(0.12 πn) u(n)

n = -10:10
v = cos(0.12 * pi * n) .* (n >=0);
stem(n,v);
xlabel(“time”)
ylabel(“magnitude”)
title(‘v(n) = cos(0.12 πn) u(n)’)

Signals and Systems EC-LDCE 21


KESHVALA KETAN U. Enrollment No. : 240283111015
8

Signals and Systems EC-LDCE 22

You might also like