Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SAS EXP-1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

Name: Branch: Lab Section:


Id No:
Execution of Execution of Inference Viva Total Marks
Examples Exercise Analysis and (10M) 50M
taught by (15M) Report
faculty (15M) submission
(10M)

Remarks:

Date: Signature of the Instructor Marks awarded

Signals and Systems(ECE2003)


Lab-1: Introduction to Matlab
Lab Report

Objectives:
1. To familiar with the Matlab environment.
2. Write the simple Matlab code to perform certain operations.

Requirements: Digital Computer with MATLAB software.

MATLAB is considered a high-level programming language because it provides


an abstract layer over the machine-level operations, making it easier for users to
write and understand code. Here are some key reasons why MATLAB is
classified as a high-level language:
1. Abstraction from Hardware

1
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

No Need for Memory Management: Unlike low-level languages like C or


Assembly, MATLAB automatically handles memory allocation and deallocation.
This means you don't need to worry about pointers, memory addresses, or
manually freeing up memory.
Platform Independence: MATLAB code can run on different operating systems
(like Windows, macOS, and Linux) without modification, thanks to its high level of
abstraction from the hardware.
2. Built-In Functions and Libraries
Extensive Functionality: MATLAB comes with a vast array of built-in functions
and toolboxes for numerical computation, data analysis, visualization, and more.
These functions simplify complex tasks like matrix operations, solving differential
equations, and statistical analysis.
Simplified Syntax: MATLAB allows you to perform complex operations with
simple, human-readable commands. For instance, performing matrix
multiplication in MATLAB is as simple as A * B, compared to a more detailed and
manual process in a low-level language.
3. Interactive Environment
Command Window for Immediate Feedback: MATLAB’s Command Window
allows you to execute commands and see results immediately, which is typical of
high-level languages designed for ease of use.
Visual Tools: MATLAB provides a user-friendly interface with tools like the
Figure Window for plotting, making it easier to visualize data without writing
extensive code.
4. Focus on Problem Solving, Not Implementation Details
Algorithm Development: MATLAB allows you to focus on solving the problem
rather than managing implementation details like memory or data types (e.g.,
floating-point vs. integer). This is a hallmark of high-level languages that prioritize
ease of development and readability.

2
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

Concise Code: High-level languages like MATLAB require fewer lines of code to
perform complex tasks compared to low-level languages. For example, solving a
system of linear equations can be done in a single line in MATLAB using the \
operator.
5. Ease of Learning and Use
Intuitive Syntax: MATLAB’s syntax is designed to be intuitive and closely
resembles mathematical notation, which makes it accessible to non-
programmers like scientists and engineers.
Rich Documentation: MATLAB provides extensive documentation, examples,
and community support, making it easier to learn and use.
Example: Matrix Multiplication in MATLAB vs. C
MATLAB:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B;

C Language:

int A[2][2] = {{1, 2}, {3, 4}};


int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = 0;
for (int k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[j][k];
}
}
}

3
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

In this example, the MATLAB code is much more concise and easier to
understand, demonstrating the high-level nature of MATLAB compared to a low-
level language like C.

MATLAB (short for MATrix LABoratory) is a high-level programming language


and interactive environment used for numerical computation, visualization, and
programming. It's widely used in academia, engineering, and science for data
analysis, algorithm development, and modeling and simulation.
How to Use MATLAB
Starting MATLAB:
 Open MATLAB by clicking its icon on your desktop or through your
operating system’s application menu.
 Once MATLAB starts, you'll see the MATLAB desktop, which includes
various windows and tools.
Basic Operations:
 Command Window: This is where you type commands. For example,
typing 2 + 2 and pressing Enter will display the result (ans = 4).
 Scripts: To write multiple lines of code, use the Editor to create a script.
Save it with a .m extension and run it by typing its name in the Command
Window or pressing the "Run" button in the Editor.
Working with Variables:
 Assign values to variables using the = operator, like x = 10.
 You can view all your current variables in the Workspace window.
 Use clear to remove variables and clc to clear the Command Window.
Creating Plots:
 MATLAB is excellent for creating visualizations. For example, plot(x, y)
will create a 2D plot of the variables x and y.
 Customize plots with titles, labels, and legends using commands like
title('Title'), xlabel('X-axis'), and legend('Label').

4
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

Various Windows of MATLAB


Command Window:This is the primary interface where you can enter
commands and see results immediately. It's a quick way to perform calculations
or test small pieces of code.
Workspace:Displays the variables you've created and their values. You can
double-click a variable to open it in the Variable Editor for more detailed
inspection.
Command History:This window shows a history of commands you’ve entered.
You can re-execute a previous command by double-clicking it or dragging it to
the Command Window.
Editor:Use this window to write, edit, and run scripts and functions. It provides
syntax highlighting, auto-indentation, and debugging tools.
Figure Window:When you create plots, they appear in the Figure Window. You
can interact with your plots here, zooming in, saving, or copying them.
Current Folder:Shows the files in the current working directory. You can
navigate to different folders and open or run files directly from this window.
Variable Editor:Allows for detailed inspection and modification of arrays and
other data structures. Open it by double-clicking a variable in the Workspace.
Toolstrip:Located at the top of the MATLAB window, the Toolstrip provides
quick access to common commands, organized into tabs like "Home," "Plots,"
"Apps," and "Editor."
Example Workflow in MATLAB
Set Up:Start MATLAB and open the Editor to create a new script.
Create Variables:
x = 0:0.1:10;
y = sin(x);
Plot Data:
plot(x, y);
title('Sine Wave');

5
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

xlabel('x');
ylabel('sin(x)');
Run Script:Save your script as sine_wave.m and run it. MATLAB will display the
sine wave plot in the Figure Window.
Inspect Results: Use the Workspace to check your variables and the Command
History to review past commands.
Computations can be carried out in one of three ways:
1. Directly from the command line,
2. by writing a script that carries out predefined instructions, or
3. by writing your own functions.
Writing your own functions is much like programming in other languages,
except that you have the full resources of MATLAB's functions at your disposal, making for
very compact code. The MATLAB-6 and above environment has several windows at your
disposal.

Command Window: The main window is the command window, where you will type
all your commands, for small programs.

1. Definition of Vectors & Matrices: MATLAB is based on matrix and vector


algebra; even scalars are treated as 1x1 matrices. Therefore, vector and matrix
operations are as simple as common calculator operations. Vectors can be defined
in two ways.
Method1: The first method is used for arbitrary elements:
>> v = [1 3 5 7]; %creates a 1x4 vector with elements 1,3, 5
and 7.

* Note that commas could have been used in place of spaces to separate the
elements. that is

>> v = [1,3,5,7];

• Additional elements can be added to the vector:

6
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

>> v(5) = 8 % yields the vector v = [1 3 5 7 8].

Previously defined vectors can be used to define a new vector. For example, with ' v '
defined above

>> a = [9 10];
>> b = [v a] % creates the vector b = [1 3 5 7 8 9 10].
Method2: The second method is used for creating vectors with equally spaced
elements:
t = 0: 0.1:10 % creates a 1x101 vector with the elements 0, .1, .2, .3,...,10.
Note that the middle number defines the increment.

If only two numbers are given, then the increment is set to a default of 1: For
example.
k = 0:10 % creates a 1x11 vector with the elements 0, 1, 2, ..., 10.
2. Matrices: Matrices are defined by entering the elements row by row
>> M = [1 2 4; 3 6 8; 2 6 5] % creates a 3X3 matrix
>> M' % Transpose of matrix M
-1
*The inverse of M is M denoted in Matlab as M^-1
>> M^-1
>> M(2,3) % displays the element of second row and third column

3. Arithmetic Operations: When applying addition, subtraction, multiplica-


tion and division between a scalar (that is a single number) and a vector we
use +, -, *, and / respectively.
% Let
>> a = [1 2 3;4 5 6;7 8 9]
>> b = [9 8 7;6 5 4;3 2 1]
% Then
>> a + b % addition of two matrices
>> a - b % Subtraction of two matrices
>> a*b % Multiplication

7
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

>> a+2 % addition of a constant to matrix ‘a ‘


>> a .*b % Element wise multiplication
4. Relations and logical operations:

5. Special Matrices
M = [ ] ; % null matrix:
M = zeros(n,m); % n x m matrix of zeros:
M = ones(n,m); % n x m matrix of ones:
M = eye(n); % n x n identity matrix:
6. Help, Document and Demos : Matlab provides excellent tutorials that are
accessible by typing >> demo
The Basic matrix operations tutorial under the Matrices tutorial, the Image
Processing and Signal Processing tutorial under Toolboxes are highly recommended.
To get information on a particular function of Matlab, we type help function_name
>> help fft % help for Fast Fourier Transform
>> help mean % help for mean or average value
Comments in Matlab must be proceeded by % .
To define a function in Matlab, we first create a function with the name of the
function. We then define the function in the file. For example, the Matlab
documentation recommends stat.m written as below for calculating mean and
standard deviation of a signal ‘ x ’.

8
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

function [mean,stdev] = stat(x)


% This comment is printed out with help stat
n = length(x); % assumes that x is a vector.
mean = sum(x) / n; % sum adds up all elements.
stdev = sqrt(sum((x - mean).^2)/n);
7. Viewing Matlab Matrices as one Dimensional Arrays : Convert multi-dimensional to
a one-dimensional array by simply using array_name (:) . For example as
previously defined matrix a=[1 2 3;4 5 6;7 8 9]
>> a(:) % results 147258369
Note that the elements in the one-dimensional array are arranged column by
column, not row by row. This is the same convention that is used for two
dimensional arrays in Fortran, and this will also be the convention that we will adopt
for representing two dimensional arrays in C++.
8. Working with Memory in Matlab: To keep track of how memory is allocated,
we use command ‘whos’
b=[34 54 56];
a=[2 3 56];
whos
9. Control Structures:

10. Generation and plotting of basic signals:


>> Plot([3 4 5 3 2 2])

9
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

To make a graph of y = sin(t) on the interval t = 0 to t = 10, we do the following:


>> t = 0:0.3:10;
>> y = sin(t);
>> plot(t,y) ;
Another example:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y); xlabel('x = 0:2\pi'); ylabel('Sine of x');
title('Plot of the Sine Function');
Multiple Graphs :
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2); plot(t,y1,t,y2); grid on

Multiple Plots
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1); plot(t,y1);
subplot(2,2,2); plot(t,y2);
11. Using Matlab Editors : Very small programs can be typed and executed in
command window. Large programs on other hand can be used the Matlab editor.
The complete program can be typed in the Matlab editor and saved as
'file_name.m', and can be retrieved when-ever
necessary. You can execute either directly from
editor window or type the file_name in the
command window and press the enter button.
Creating, Saving, and Executing a Script File
% CIRCLE - A script file to draw a pretty circle

10
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS

function [x, y] = prettycirclefn(r)


% CIRCLE - A script file to draw a pretty circle
% Input: r = specified radius
% Output: [x, y] = the x and y coordinates
angle = linspace(0, 2*pi, 360);
x = r * cos(angle); y = r * sin(angle);
plot(x,y) ; grid on ;
title(['Radius r =',num2str(r)]) ;
axis('equal') ;ylabel('y') xlabel('x') ;

11

You might also like