0% found this document useful (0 votes)
304 views5 pages

Review of MATLAB

This document provides instructions for Programming Exercise 01 in the course CoE 121: Introduction to Digital Signal Processing. The exercise focuses on MATLAB review and covers topics like matrix manipulations, operations, and signal generation. Students are asked to complete coding tasks to demonstrate their understanding of these topics and submit their code and figures by the deadline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
304 views5 pages

Review of MATLAB

This document provides instructions for Programming Exercise 01 in the course CoE 121: Introduction to Digital Signal Processing. The exercise focuses on MATLAB review and covers topics like matrix manipulations, operations, and signal generation. Students are asked to complete coding tasks to demonstrate their understanding of these topics and submit their code and figures by the deadline.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CoE 121: Introduction to Digital Signal Processing

Electrical and Electronics Engineering Institute


University of the Philippines, Diliman

Programming Exercise 01: MATLAB Review

INSTRUCTIONS
• This is an individual programming exercise. Any clarifications or questions must be
directed to the instructor or student assistant.
• Save your code in an M-file with the file name CoE121_PE01_<LastName>. Submit
your code together with the required figures and functions via the submission bin in
UVLe.
• The deadline of this exercise is on January 21, 11:55 PM. A 10-point deduction per
day will be incurred for late submissions.

MATRIX MANIPULATIONS AND SIGNAL GENERATION


A. (20 POINTS) Basic Data Types: Matrices and Arrays
1. One way to generate a matrix or an array is to specify its elements row-wise. A
semicolon and a space (or a comma) are used to separate rows and columns,
respectively. Example: to generate the matrix A, the command to use is

>> A = [1 1/2 3; 6 10 1; 0 3 2];

1 1 / 2 3  4 9 − 3
0 − 8 10 
A = 6 10 1  B= C = 13 0 7 
 
5 3 1   
0 3 2   1 2 11 

(2 POINTS) Write in the blank below the commands to generate matrices B and
C.

>> ____________________________;

>> ____________________________;

2. Each element of a given matrix can be accessed by specifying its indices. A(row,
column) will give you the element at the given row and column. For example, use
the command below to access the value of the matrix at the 2nd row and 2nd
column.

>> MidValue = A(2,2)

(1 POINT EACH) What are the values of the following?

>> A(1,3) + C(2,1) = ______ >> A(2,3) * B(2,[1 3]) = ______________

>> B(2,3) – C(3,1) = ______ >> B(2,[1 3]) * B([1 2],3) = __________

1 3]
(2 POINTS) How would you generate the matrix 𝐷 = [ from matrix A? Write
0 2
the command (just one) below.

>> ____________________________;

3. To access entire rows or columns of matrices, use the ‘:’ operator. As an


example, the command below is used to access the 2nd row of matrix C.

>> row2 = C(2,:)

CoE 121 Programming Exercise 01: MATLAB Review


2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
The ‘:’ operator can also be used in creating an arithmetic series. If you want to
generate an array of sample indices of a discrete-time signal, the command to
employ is

>> n = [-5:1:5]

Note: The increment can be omitted if its value is one.

(1 POINT EACH) What are the values of the following?

>> A(3,:) + C(1,:) = _________ >> B(:,1) – C(2:3,2) = __________

(2 POINTS) Create a vector of time samples from t = 0 to 10 sec with 0.25 sec
interval.

>> ____________________________;

1 1/2 3
(2 POINTS) How would you generate the matrix 𝐸 = [ 5 3 1] from matrices A,
13 0 7
B and C? Write the command (just one) below.

>> ____________________________;

4. For large matrices, the find() command is useful in determining the row and
column of a certain value of the matrix. (type ‘help find’ at the MATLAB prompt
for more details)

To find the indices of matrix C that are zero, enter the command

>> [RowInd, ColInd] = find(C==0)

0 9 0
(4 POINTS) Without using loops, produce the matrix 𝐹 = [13 0 0 ] from matrix
0 0 11
C only. Write in the blanks below the commands to produce F.

>> ____________________________;

>> ____________________________;

B. (12 POINTS) Matrix Operations


1. The addition and subtraction operations when applied to matrices are performed
element-by-element. This is not the case for matrix multiplication. To perform
element-by-element multiplication of matrices, use the ‘.*’ operator. Type ‘help
ops’ for more commands.

(1 POINT EACH) What are the values of the following?

>> A.*C = _____________ >> B(1:2,1:2)./C([1 3],[1 3]) = _____________

(4 POINTS) Generate a 1 × 2500 vector of random numbers using the function


rand(). Extract the 1st, 2nd, 4th, 8th, 16th, …, 2048th elements and assign it as
vector E. Do not use loops. Write the answer in the space provided.

>> ____________________________;

>> ____________________________;

2. To get the inverse of a matrix, use the inv() command. The inverse of matrix A is

CoE 121 Programming Exercise 01: MATLAB Review


2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman

>> inv(A)

Solve the following system of equations using MATLAB and give the values of the
unknowns in the space provided below.

(4 POINTS) What are the commands that you use in solving the unknowns?

>> ____________________________;

>> ____________________________;

>> ____________________________;

3. (2 POINTS) Using the vector 𝑟 = [0.5 + 𝑒 −𝑗(𝜋/3) , 𝑒 −𝑗(𝜋/6) , 2], determine the difference
between the transpose() command and the ‘ operator. Write your answer in the
space provided below.
_________________________________________________________________

C. (8 POINTS) Signal Generation


1. To generate a sinusoidal signal of a given frequency, several parameters need to
be defined. We have to determine the amplitude, frequency, and phase of the
sinusoid; in addition, the sampling rate should be defined. The sin() function is
used to generate a sinusoidal signal.

Generate 40 samples of a sinusoidal signal with a frequency of 200 Hz, amplitude


of 2 and a phase shift of ϖ/2 with a sampling frequency of 4000 Hz. In MATLAB,
this translates to the following.

>> fs = 4000; % defines the sampling rate


>> n = [0:39]’; % generates the number of samples
>> fx = 200; % defines the frequency
>> amp = 2; % defines the amplitude
>> phase = pi/2; % defines the phase
>> y = amp*sin(2*pi*fx*n/fs+phase) % generates the sinusoid

2. To view the signals, use the available plotting and viewing functions in MATLAB.
Some examples of these functions are the figure(), plot(), and subplot(). To view
the different samples of the sinusoid, use the stem() command.

>> figure; stem(y);

Use the sample index variable n as an additional parameter in the stem()


function.

>> figure; stem(n,y);

(2 POINTS) Which of the two figures shows the correct sketch of the signal? Why?
_________________________________________________________________

CoE 121 Programming Exercise 01: MATLAB Review


2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
To change the x-axis to display the time instead of n-samples, define a time
variable t, and use this as an additional parameter to the plot() function.

>> t = n/fs; % defines the time axis


>> figure; plot(t,y); % plot the sinusoid

Use the xlabel(), ylabel() and title() commands to add labels to your plot.

>> xlabel(‘time in seconds’);


>> ylabel(‘amplitude in volts’);
>> title(‘plot of a sinusoidal signal’);

(2 POINTS) Save the figure as <StudentNumber>_sinusoid.jpg

The plot() command allows you to vary the way the signal is plotted. If you want
to change the color or the plot, just add the appropriate parameters to the plot
command. For more details, use ‘help plot’ command.

>> figure; plot(t,y,’rx:’);

To overlap the points in a single window, use the hold() command.

>> figure; plot(t,y); hold on; stem(t,y,’r’);

To close a figure, use the close() command. This is to avoid too much cluttering
of figures in the desktop. To close all figures, just type

>> close all;

To make multiple plots on a single figure window, use the subplot() command.
For example, to plot on different rows or columns, specify the row and column in
the subplot() command. The following examples will help illustrate.

>> figure; subplot(2,1,1); plot(t,y);


>> subplot(2,1,2); stem(t,y,’r’);
>> figure; subplot(1,2,1); plot(t,y,’r’);
>> subplot(1,2,2); stem(t,y,’g+:’);
>> figure; subplot(2,2,1); plot(t,y);
>> subplot(2,2,2); stem(n,y,’r’);
>> subplot(2,2,3); plot(t,y,’r’);
>> subplot(2,2,4); stem(n,y,’m’);

(2 POINTS) Save the last figure as <StudentNumber>_splot.jpg

3. Noise signals with uniform and Gaussian distribution are generated using the
rand() and randn() commands, respectively. The randn() function by default will
generate random numbers with zero mean and unit variance. To change the
variance, just multiply the output of randn() with the square root of the desired
variance. To vary the mean, add/subtract the desired mean.

To generate a noise signal with a variance of 2 and a mean of 1

>> tempsig = randn(1,100); % the initial noise signal


>> noise = sqrt(2)*tempsig + 1; % change the mean and variance
>> figure; plot(noise);

(2 POINTS) Save the figure as <StudentNumber>_noise.jpg

CoE 121 Programming Exercise 01: MATLAB Review


2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
D. (10 POINTS) Multitone Sinusoidal Signals
1. The sin() function can be used to generate sinusoids of different frequencies
represented as matrices. Each column (or row) of the matrix represents a
sinusoid of a given frequency. The technique in doing this is to generate a matrix
of different frequency samples and apply the sin() function to that matrix.

Example: Generate the tones of the octave with the following frequencies {262,
294, 330, 349.7, 392.6, 440.6, 494.5, 524} and the duration of each sinusoid is
approximately 1 second. Play each tone in succession using a simple for loop.

>> fs = 8000;
>> freq = [262, 294, 330, 349.7, 392.6, 440.6, 494.5, 524];
>> amp = ones(1,8);
>> phase = zeros(1,8);
>> t = [0:7999]’/8000;
>> octave_sig =
(ones(length(t),1)*amp).*sin(2*pi*t*freq+ones(length(t),1)*phase)

Use sound() function to hear the sound.

>> for i = 1:length(freq)


temp_sound = octave_sig(:,i);
sound(temp_sound,fs);
pause(0.5);
end

To generate a composite sinusoidal signal, sum the samples across each row (or
column). Type ‘help sum’ for more options.

>> temp_sound = sum(octave_sig,2);


>> sound(temp_sound,fs);

2. A Dual Tone Multi-Frequency (DTMF) standard is extensively used in telephone


systems. This identifies which number is pressed/dialled. The table below shows
the frequency combination for each button on a telephone handset. The
telephony system detects the presence of the various combinations of these
frequency components. For example, if a 1209 Hz sinusoid and a 697 Hz sinusoid
are detected from the signal, then the base station would conclude that ‘1’ was
pressed/dialled. Set the sampling frequency to 8000 Hz.
Freq (Hz) 1209 1336 1477 1633
697 1 2 3 A
770 4 5 6 B
852 7 8 9 C
941 * 0 # D

(10 POINTS) Generate the DTMF of the last 7 digits of your student number. Each
digit must have a duration of 1 second, followed by a half-second silence. Save
the signals as <StudentNumber>_dtmf.wav.

To save a sound signal, enter this command

>> wavwrite(<your_sound>,fs, ‘<StudentNumber>_dtmf.wav’);

Some notes:
• To save all the commands you entered, you can use the command ‘diary <filename>’
• If you need any help on any of the MATLAB commands, just type ‘help command’
from the MATLAB command prompt. If you want to search for any command that
contains a certain keyword, use the ‘lookfor’ command.

CoE 121 Programming Exercise 01: MATLAB Review


2nd Semester 2018-2019

You might also like