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

BSP Lab-1

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

BSP Lab-1

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

Military Institute of Science and Technology (MIST)

Department of Biomedical Engineering


_________________________________________________________________________
Sessional Title
Introductory Practice on the Fundamentals of Signal Processing in Matlab Programming
Software
Objectives:
1. To introduce with the fundamental operations with matrix in Matlab
2. To be able to solve simple practical problems using Matlab code

1. Formation of Matrix in Matlab


To create a matrix in matlab you must
a) begin with a square bracket [
b) separate elements in a row with commas or spaces
c) use a semicolon ; to separate rows
d) end the matrix with another square bracket ].
For Example:
Type in Matlab Response by Matlab
a = [1 2 3; 4 5 6; 7 8 9] a = b =
1 2 3 1 0
or 4 5 6 2 5
7 8 9 3 3
b=[1 0; 2 5; 3 3]

We can also concatenate the Matrices. Example:


Type in Matlab Response by Matlab
c = [a b] c =

1 2 3 1 0
4 5 6 2 5
7 8 9 3 3

Try c=[a; b]and comment on the result.


Be introduced with the following functions:
repmat, zeros, ones, rand, randn, eye, magic,spiral, etc.

2. Mathematical Operation of Matrix


2.1 Adding Matrix: Manipulating vectors is almost as easy as creating them. First, suppose you
would like to add 5 to each of the elements in vector 'a'. The equation for that looks like:
Type in Matlab Response by Matlab
aa = a+2 aa =

6 7 8
9 10 11
12 13 14
Try c=a+b; and comment on the result

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST
Military Institute of Science and Technology (MIST)
Department of Biomedical Engineering
_________________________________________________________________________

2.2 Matrix Subtraction: Just using the simple subtraction sign (-) you can subtract two matrices of
same length. An example is given:
Type in Matlab Response by Matlab
m=[1 2 3; 4 5 6; 7 8 9]; p =
n=[1 3 4; 4 2 3; 9 3 1];
p=m-n 0 -1 -1
0 3 3
-2 5 8
Try c=m-b; and comment on the result

2.3 Matrix Multiplication: There are two types of multiplication in Matrix- scalar multiplication and
vector multiplication. Here both example are given:
Scalar Multiplication
Type in Matlab Response by Matlab
m=[1 2 3; 4 5 6; 7 8 9]; p =
p=m*2
2 4 6
8 10 12
14 16 18
Vector Multiplication
Type in Matlab Response by Matlab
m=[1 2 3; 4 5 6; 7 8 9]; p =
n=[1 3 4; 4 2 3; 9 3 1];
p=m*n 36 16 13
78 40 37
120 64 61
Try p=m.*n; and comment on the result

2.4 Matrix Division: Matrix division is generally done for scalar division. This operation is easy. On
the other hand, vector division is associated with linear algebra. Follow class instruction to
understand these.
Scalar Division
Type in Matlab Response by Matlab
m=[1 2 3; 4 5 6; 7 8 9]; p =
p=m/2
0.5000 1.0000 1.5000
2.0000 2.5000 3.0000
3.5000 4.0000 4.5000
Vector Division
A = [1 1 3; 2 0 4; -1 6 -1]; x =
B = [2 19 8];
x = B/A 1.0000 2.0000 3.0000
Be introduced with inverse and transpose functions. Also learn about
the length and size of a matrix

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST
Military Institute of Science and Technology (MIST)
Department of Biomedical Engineering
_________________________________________________________________________

3. Subscripting
Individual elements in a matrix are denoted by a row index and a column index. To pick out any
specific vector its row and column index are necessary and vice versa. Examples are given.
Type in Matlab Response by Matlab
u = rand(1,5) u =
u(3)
[R C]=find(u,0.1763) 0.8147 0.9058 0.1270 0.9134 0.6324

???? (Try Yourself and Comment)

Also work on the following:


i) u([1 2 3])=?
ii) u(1:4)=?
iii) a(3,2)=?
iv) a(2:3,3)=?
v) a(2,:)=?
vi) a(:,3)=?
vii) [a a(a)]=?
Also look on the followings
>> q = 4:10
>> q(end)
>> q(end-4:end)
>> q = [spiral(3) [10;20;30]]
>> q(2,end-1:end)

4. Repeating works or Looping in Matlab


For repeating works, usually we use loop in programming. Here, we will be discussing about the for
loop only. An example of series summation is given here using a ‘for loop’.
>> A=[1:2:100]; %Contains 1,3,5..,99-need to sum them
>> int_sum=0;
>> for i= 1: length(A)
int_sum=int_sum+A(i);
end
%Take necessary help from the instructors to understand the code
Funcode: Try this and have fun, also try to understand the fact
N = 10;
for k = 1 : N
fprintf('%d ', 1:k);
fprintf('\n');
end

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST
Military Institute of Science and Technology (MIST)
Department of Biomedical Engineering
_________________________________________________________________________

5. Conditional Statement Scripting in Matlab


To understand the conditional statement let’s do a code first and then try to understand its
functionality:
A=[1 2 3; 4 5 6; 7 8 9];
for i=1:3
for j=1:3
if rem(A(i,j),2)==0
A(i,j)=A(i,j);
else
A(i,j)=0;
end
end
end
A
Is it interesting ?

6. Cell Arrays and Structures


Cell arrays in MATLAB are multidimensional arrays whose elements are copies of other arrays. A
cell array of empty matrices can be created with the cell function. But, more often, cell arrays are
created by enclosing a miscellaneous collection of things in curly braces, {}. The curly braces are
also used with subscripts to access the contents of various cells. For example,
>> A=[2 3 1; 3 4 5; 6 4 7];
>> C = {A sum(A) prod(prod(A))};
Observe the Value of C in Workspace. Also try C{1,1}, C{1,2},
C{1,3}.
Structures are multidimensional MATLAB arrays with elements accessed by textual field
designators. For example
>> S.name = 'Asad';
>> S.roll = 123456;
>> S.grade = 'C'
>> S.age = 32;
Like everything else in MATLAB, structures are arrays, so you can insert additional elements. In this
case, each element of the array is a structure with several fields. The fields can be added one at a
time,
>> S(2).name = 'Faizul';
>> S(2).roll = 786354;
>> S(2).grade = 'A+'
>> S(2).age = 25;

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST
Military Institute of Science and Technology (MIST)
Department of Biomedical Engineering
_________________________________________________________________________

7. Basic Graphics
The bread-and-butter of Matlab graphics is the ‘plot’ command. Earlier we produced a plot of the sine
function
x = 0:.1:2*pi;
y = sin(x);
plot(x,y)

Try these also:

a) plot(x,y,’r-.’)

b) plot(x,y,’bx’)

c) plot(x,y,’b:x’)

d) plot(x,y,x,2*y)

e) plot(x,y,x,2*y,’--’)

f) plot(x,y);
hold on
plot(5*x,5*y)
Subplotting
To plot more than one set of axes in the same window, use the subplot command. You can type
subplot (row_pos,col_pos,fig_number)

Example:

t = 0:.1:2*pi;
subplot(2,2,1)
plot(cos(t),sin(t))
subplot(2,2,2)
plot(cos(t),sin(2*t))
subplot(2,2,3)
plot(cos(t),sin(3*t))
subplot(2,2,4)
plot(cos(t),sin(4*t))

A bonus code for just fun

t = 0:.1:2*pi;
plot3(cos(3*t),sin(3*t),t)
rotate3d

Learn also about: axes labelling, legend, title, grid, box, linespace.

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST
Military Institute of Science and Technology (MIST)
Department of Biomedical Engineering
_________________________________________________________________________

Home Works:

1. Create a structure variable including all of your academic information like Name, Roll, Dept,
Level, etc.

2. Find the sum of the following series

x=12 + 32 + 52 + . . . + 252

3. Find the value of the followings using loops

i) 20!

1 15  i 2  2i  30 
ii) x  
10 i 1  i3


4. Write a script to have a triangle as follows:


1
12
123
1234
12345
123456
12345
1234
123
12
1
5. Write to script to draw sin(t), sin(2t), sin (3t), sin(4t) and sin(t)+sin(2t)+sin (3t)+sin(4t) and
present them one by one in a figure using ‘subplot’. In this figure, on the grid, set the axes
label, mention the legends.

-------------------------------------------------------------------------------------------------------------------------------------------
Prepared by: Dr. Md. Asadur Rahman, Assistant Professor, Dept of Biomedical Engineering, MIST

You might also like