Sunteți pe pagina 1din 5

ISLAMIC UNIVERSITY OF TECHNOLOGY (IUT)

ORGANIZATION OF THE ISLAMIC COOPERATION (OIC)


Digital Signals Processing Lab. (EEE 4750)
Lab. 1 Matlab functions, filtering and aliasing

PART-1: Familiarization with MATLAB


1. MATLAB Interface:

2. Basic MATLAB Commands:

o clear all: clears workspace of all variables


o close all : closes all the figures and file windows
o % : used for Comments
o help : when used with command gives its syntax
o MATLAB Demos accessed from the start icon at lower bottom.

Modified by: Irteza Enan Kabir


Page 1
3. Variables:
 Create a variable:
Write the following commands in the Command window,

x=99 + 4 i % j can be used too. Complex numbers are natural for MATLAB variables
myString=‘hello world’
myArray = [ 1 2; 3 4]

 Variable Names:
o First character must be a LETTER.
o After that, any combination of letters, numbers and _ could be given.
o CASE SENSITIVE! (var1 is different from Var1).

 Built-in variables:
1. i and j can be used to indicate complex numbers.
2. pi has the value 3.1415926…
3. Ans stores the last unassigned value (like on a calculator).

 Brackets, comma, semi-colon, and colon:


1. Only first ( ) and third [ ] brackets are used in MATLAB. Third bracket [ ] is used
only when we want to define/allocate/assign something. For everything else,
which can be categorized as going into something, we use first bracket ( ) .
2. Comma represents going to the next element or operation in the same line,
whereas semi-colon refers to going to next line (example: element in next line of
array, going to next line without showing result)
3. Colon relates beginning and end of elements. Try 1:4 and 1:1/2:4 in your
command prompt.

4. Matrix and Array:


1. Defining/Assigning, thus use third bracket : a=[1 2 3;3 4 5;4 5 6]
or [ 1, 2,3 ; 3, 4,5;4,5,6] .

2. Indexing or going into and bringing back elements for use, thus use first bracket:
c = a (2,1)
d = sin (a(2,1))
% Remember RC convention or Row-Column convention signifying row to be
represented first, then column.

3. Select only certain elements


C=a(2:3,1:2) % 2nd and 3rd row and 1st and 2nd column
D= a(1:3, 2:3)

Modified by: Irteza Enan Kabir


Page 2
4. Initializing of arrays of Ones and Zeros. (Important in binary-valued arrays useful
particularly in telecommunication related simulation)
X= ones (20,3)
Y=zeros (2, 20)
size (Y) % Understand difference between size and length
length (&)
Try [ 0:1:length(y)]

5. Structure Array: A structure is a data type that groups related data using data
containers called fields. Each field can contain data of any type or size.
Example:
patient(1).name = 'John Doe';
patient(1).billing = 127.00;
patient(1).test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205];
patient

5. Creating functions:
function y = discrete_sin (n, Fs, Fo)
y = sin (2*pi*Fo/Fs*n);
% t = n * Ts = n / Fs

% Do you have a result if you give a single value of ‘n’?


% Demonstration of for loop and if statement

stem( n,y)

end

NOTE: Save this function for later part of this lab

6. MATLAB Sound
 Check out the MATLAB demo by typing on command prompt: xpsound
 Make your own sound !
t = 1 : 0.01:5 ;
Fo=3;
Fs= 100;
A=400;
y = A* cos (2*pi*Fo*t);
sound (y)

% Can you hear a sound? Oops! No speaker in lab. Try at home.

Modified by: Irteza Enan Kabir


Page 3
7. Some useful matlab built-in function
 repmat  sort
 reshape  cumsum
 linspace  randi
 isequal  mean
 circshift  median
 flip

Assignment:
1) Use audioread and audiorecorder functions (get help from MATLAB help),
and try to find digital versions of “Hello !” and “Olleh !” in your OWN voice
by next class!. No need to write it in report!

2) Write briefly with example the contribution of the above functions

PART-2: Aliasing(Basics)
Aliasing occurs when a system is measured at an insufficient sampling rate. It is
perhaps best explained through example.

Let's say I have a ball that has three positions: 0 (middle), +1 (up), and -1 (down).
Let's say that ball starts at 0 at time=0, goes to +1, then to 0, then to -1, then back
to 0, and each of these 4 movements takes 1 second. Since it takes 4 seconds to
complete this cycle, the frequency of this motion would be (1 cycle)/(4 seconds) =
.25 Hz.

Now let's say you want to measure this frequency, but you only look at the ball
once every 5 seconds. At t=0, you see it at 0, at t=5 you see it at 1, at t=10 you see
it at 0, at t=15 you see it at -1, and finally at t=20 you see it at zero after what
appears to be a complete cycle. You will then say that the frequency appears to be
(1 cycle)/(20 seconds) = .05 Hz. But this is wrong!!! Why? Because you weren't
looking often enough, and you missed a bunch of the movement, resulting in you
'measuring' a totally different frequency. That's aliasing.

Modified by: Irteza Enan Kabir


Page 4
EXAMPLE:

function y = discrete_sin (n, Fs, Fo)


y = sin (2*pi*Fo/Fs*n);
% t = n * Ts = n / Fs

% Do you have a result if you give a single value of ‘n’?


% Demonstration of for loop and if statement

for i = 1:3
if ((Fs < 2 * Fo) && i~=3)
disp('you cannot make me do this! ' )
elseif ((Fs < 2 * Fo) && i==3)
disp('Fine, I give you aliasing ' ) % broadly discussed in later part
plot ( n/Fs, y)
else
plot ( n/Fs, y)
end
end

PART-3: For students

Basic filters (Mean and Median)

 In help bar, type “mean” and see how it works.


 Do same for “median”

LAB TASK
Create a 5 by 5 random matrix(use rand function). Multiply the whole matrix by 10. Do
the mean of the first 2 columns and median for the next 3 columns, and save it in a 1 by
5 matrix . Try to use all the things you learned in this lab and finish the code in ONE
LINE ONLY

Modified by: Irteza Enan Kabir


Page 5

S-ar putea să vă placă și