Matlab Review PDF
Matlab Review PDF
Lab Instructions
✔ The students should perform and demonstrate each lab task separately for step-wise evaluation
(please ensure that course instructor/lab engineer has signed each step after ascertaining its
functional verification)
✔ Each group shall submit one lab report on LMS within 6 days after lab is conducted. Lab report
submitted via email will not be graded.
✔ .Students are however encouraged to practice on their own in spare time for enhancing their
skills.
Lab Report Instructions
All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
✔ Lab objectives
✔ MATLAB codes
✔ Results (graphs/tables) duly commented and discussed
✔ Conclusion
jkl = 0 : 6;
Assign values from 0 to 6 with interval 1
jkl = 2 : 4 : 17;
Assign values from 2 to 17 with interval 4
jkl = 99 : -1 : 88;
Assign values from 99 to 88 with interval -1
ttt = 2 : (1/9) : 4;
Assign values from 2 to 4 with interval 1/9
tpi = pi * [ 0:0.1:2 ];
Assign values from 0 to 2*pi with interval 0.1*pi
(b) Extracting and/or inserting numbers into a vector is very easy to do. Consider the following definition
of xx:
Explain the results echoed from the last four lines of the above code.
What’s the difference between a length and a size statement for a matrix? To test this define a matrix X
with arbitrary inputs, having multiple rows and columns and test the output of length() and size() function
on it.
Ans:
Size: Returns the number of rows and columns in x
Length(x): Returns the size of the longest dimension of x
(c) Assigning selective values in a matrix differently. Comment on the result of the following
assignments:
yy = xx;
yy(4:6) = pi*(1:3);
Ans: The fourth, fifth and sixth values of yy are replaced by π, 2π and 3π respectively.
Notice the word “function” in the first line. Also, “freeq” has not been defined before being used. Finally,
the function has “xx” as an output and hence “xx” should appear in the left-hand side of at least one
assignment line within the function body. The function name is not used to hold values produced in the
function.
Write a simple MATLAB code (in the form of a function) that allows you to decompose a signal into its
even and odd parts.
Note: The function takes two inputs n, the timing index and x the values of the signal at the designated
time instants. The function outputs include the two sub-functions, x_e and x_o along with the timing
index.
function xe = even(x, n)
xe(n+5) = 1/2 * ( x(n+5) + x(5-n) );
function xo = odd(x, n)
xo(n+5) = 1/2 * ( x(n+5) - x(5-n) );
Test your function on the following signal x[n] and compute its even and odd parts.
Consider the first order system defined by the difference equation as follows (we’ll review the discussion
on how determination of order for a difference equation later):
y = zeros(1, length(x));
n = 2:length(x);
y(1) = x(1);
y(n) = a * y(n-1) + x(n);
The convolution essentially involves two operations: flipping either the input signal or the impulse
response (as in above equation) and then sliding the flipped signal.
i. Write your own convolution function, myconv.m that computes the convolution between the two
signals (or the output of passing an input signal through a system). Designate all the necessary inputs
for your function, considering that the input signal and the impulse response may start at some ‘n’
that is negative. The function output is obviously the system output along with the timing index for
the output n1, which must be set manually. Your function should work on any general signal and the
impulse response (of finite length).
x_len = length(x);
h_len = length(h);
y_len = x_len + h_len - 1;
a = x_len+1:y_len;
x(a) = 0;
a = h_len+1:y_len;
h(a) = 0;
tmp = h;
n = 1:y_len;
h(n) = tmp(y_len + 1 - n);
y(n) = 0;
yzp = hzp + xzp - 1;
for n = 1:y_len
for k = 1:n
y(n) = y(n) + x(k)*h(y_len - n + k);
end
end
y
yzp
stem(i,y)
ii. Test your function on the signal and the impulse response provided in the figures below and verify the
correctness of your function through a comparison of manual computation of the convolution for the
given signal and a plot of your function’s output.
iv. Consider now that x[n] starts from n = -1 a nd h[n] starts from -2. What will be the result of
convolution then? Plot the corresponding output signal using the stem command and proper
timing axis.
Ans:
The self defined function automatically adjusts the time axis. For the above case xzp will be equal
to 2 and hzp will be equal to 3
i. Define x[n] as a very simple signal starting at n=0 and containing a sequence of ones in its first
100 samples and a sequence of zeros in its next 200 samples. You may like to use the MATLAB
functions zeros() and ones().
>> n = 0:299;
>> stem(n, x)
,
where N represents the period of this impulse train. A particular example of this train is shown below for
N = 3 and for k =-3:3.
iii. Write a function that lets the user generate an impulse train for a given N and given range of k,
i.e. the function takes at its input N and range of k (or one positive value and define within your
function array from –k:k to get the whole range) and outputs the impulse train in an array along
with a timing index.
n = -N*k:N*k;
h = [];
for i = 1:2*k
h = [h, [1, zeros(1, N-1)]];
end
h = [h, 1];
iv. Use the function that you just defined to generate an impulse train with N = 300 and k =-2:2. Plot
the impulse train as a function of time index that would be generated.
The function in (i) has been shifted to all the impulses of the function in (iv)
vi. Repeat (iv) and (v) with N = 50 for the same input as in (i). Plot the corresponding outputs. Could
you still establish a similar relation as in (v).
The same result can be concluded as in (v)
Ans:
Time limited function can be made periodic by convolving it with a pulse train. The resulting
function will have the same period as the pulse train. The value of N should be greater or equal
to the total time of the time limited function.
viii. Load a sample audio clip (.wav file) in MATLAB and generate its periodic version (repetition)
using the process above without causing any overlap on the original audio content.
N = 5000, k = 2
After convolution