0% found this document useful (0 votes)
15 views

Signals and System Lab3

The document discusses using for and while loops in MATLAB to generate sequences of numbers, and the differences between the two. It also covers plotting continuous and discrete time signals in MATLAB, as well as plotting the real and imaginary parts of signals.

Uploaded by

MAHNOOR BARI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Signals and System Lab3

The document discusses using for and while loops in MATLAB to generate sequences of numbers, and the differences between the two. It also covers plotting continuous and discrete time signals in MATLAB, as well as plotting the real and imaginary parts of signals.

Uploaded by

MAHNOOR BARI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DEPARTMENT OF AVIONICS ENGINEERING

SUBJECT : SIGNALS & SYSTEMS LAB

LAB NO : 03

TITLE : Loops & Basic Signals in MATLAB

SUBMITTED TO : Ms. Maha Intakhab Alam

SUBMITTED BY : MAHNOOR

BATCH : AVIONICS 07

SECTION : B
Marks Obtained :

Remarks:

DEADLINE:

DATE OF SUBMISSION:
Working With Loops:
For Loop:
Generating numbers 1 to 10 in ascending order using for loop.
Type the following code in your editor, save and run and check the output.
It should generate numbers from 1 to 10.

Task 1. Generate numbers in descending order from 30 to 20.


INPUT:
for i=30:-1:20
disp(i)
end

OUTPUT:
Task 2. What is the use of semicolon at the end of each statement?
What happens if you put a semi colon and if you don’t?

In MATLAB, the SEMI COLON (;) at the end of an expression acts as a command separator
and presses output to the command window. Inserting a breakpoint executes the statement
without specifying an output, which is useful for suppressing large or intermediate results.
Unchecking the comma will display the output of the statement in the command window,
which can be useful for debugging when you need immediate feedback..

While loop:
Generating numbers 1 to 10 in ascending order using while loop.
Type the following code in your editor, save and run and check the output.
It should generate numbers from 1 to 10.

Task 3. Generate numbers in descending order from 30 to 20.


i=30;
while(i>19)
disp(i)
i=i-1;
end
OUTPUT:

Task 4. What is the difference between for and while loop? Why
use either one if you can generate the same output using either for
or while loop.
Both for and while loops are used in programming to iterate over a sequence of instructions
multiple times, but they have differences in syntax and usage.
For Loops:
1. Designed for scenarios where the number of iterations is known or easily determined.
2. The syntax includes initialization, conditional checking, and repeated increment/decrement
in one line.
3. Usually used to iterate over sequences such as lists, repeats, or ranges.
4. Syntax example: `for (var i = 0; i < 10; i++) { // code }`.
5. Best suited for situations where loop control variables are simple and well defined.

While Loops:
1. Suitable for scenarios where the loop continues until a certain condition is false.
2. The syntax consists of a condition that is evaluated before each iteration, and the loop
continues until the condition is no longer met.
3. Provides more flexibility because the loop condition can depend on several factors such as
boolean values or existing variables.
4. Example syntax: `while (i < 10) { // code }`.
5. Enables dynamic control flow cycling under changing conditions.
Main differences:
1. Control Flow:For loops are more structured and follow a fixed pattern of initialization,
condition checking, and iteration, while for loops offer more flexibility in defining the
conditions of the loop.
2. Readability: It is recommended for loops if the number of iterations is known or if iterating
over sequences, because the syntax is short and expressive. Loops are more suitable for
scenarios where loop control depends on dynamic conditions or existing variables.
3. Variable Creation: For loops, a new loop control variable must be created in the loop
initialization statement. Loops, on the other hand, allow existing variables to be used directly
within the loop conditions.

In summary,while both for and loops are designed to iterate over blocks of code, they differ in
syntax, usage scenarios, and readability, which is more suitable for the specific requirements
of the program.

while both loops can accomplish similar tasks, the choice between them depends on the
specific requirements of the program and the readability of the code they are designed for
different scenarios and can lead to code that is more readable and maintainable when used
appropriately.

Basic Signals in MATLAB


Continuous Time Signals
Continuous-time signals are signals that:
• are defined for every instant of time within a certain interval,
• take on values that are continuous and smoothly varying.
• These signals are usually represented mathematically by functions of time, such as sin(t)
or e^(-at), where t is time and a is a constant.
• Continuous-time signals are used to model physical phenomena that vary continuously
over time, such as sound waves or electrical signals.

Discrete Time Signals


Discrete-time signals are signals that:
• are defined only at specific instances of time, usually at uniform intervals.
• take on values that are often discrete, meaning that they come from a finite set of possible
values.
• typically represented by sequences of numbers, where each number represents the signal
value at a specific time instant.
• These signals are used in digital signal processing, where they are manipulated using
mathematical algorithms to achieve various signal processing goals, such as filtering or
compression.
Task 5. Plot x(t)= cos(t) for -roll number <= t <= roll number.
t=linspace(-2,2,26);
x=cos(t);
plot(t,x)
title("cosine function of continuous time signal")

OUTPUT:
Task 6. Plot x[n]=cos[n] for -roll number<= t <= roll number.
(Hint: stem command is used for plotting in discrete time)
INPUT:
n=linspace(-2,2,26);
x=cos(n);
stem(n,x)
title("cos function for discrete time signal")

OUTPUT:

Real and Imaginary Part of Signals


Here we’ll use the real and img command on signals to get the real and img part of a whole signal.
Consider signal y(t) = 2* e(j*pi*t + pi/3). Calculate the Time period and plot it for one period of
time.
Time period can be calculated as T = 2*π/ω. Here jωt = j πt => ω=π.
 T = 2.
Task 7. Use linspace command as t= linspace(0,T,100). And plot
the real and imaginary part of signal in MATLAB. Use hold on
command to plot in same figure.
INPUT:
T = 2;
t = linspace(0, T, 100);
y = 2 * exp(1j * pi * t + pi/3);
plot(t, imag(y));
hold on
plot(t, real(y));
title("real and imaginary part of signal")

OUTPUT:
CONCLUSION:
• From this lab report, we have learnt to use for and while loops, how to apply them in
different scenarios and how to avoid infinite loop, where while loop is used and where for
loop is used.
• We have also learnt how to plot continuous time signal and how to plot discrete time
signal on MATLAB.
• We have also learnt to plot real and imaginary part of signals on MATLAB.

You might also like