EEEN30160: Matlab Exercise 1
• Remember to put your Student Number on the report
• Remember to put the name of your partner in the report
• For each exercise generate a script (.m file) with all the commands, that will be uploaded
together with your report. Some exercises will specifically request additional functions to be
generated and called from the main script. Upload also the functions when requested by the
• Remember to label (both x and y axes) your graphs. In the labels put also the measurement unit
in parenthesis (e.g. seconds or samples for time axes). For arbitrary signals use (A.U.), meaning
Arbitrary Units, as measurement unit. Points will be subtracted from the score of each exercise
for any missing label.
• COMMENT YOUR CODE. A perfect code with no comments will count for half of the points
assigned to the exercise.
• Each exercise is worth the same number of points
• THE WORK IS DONE IN PAIRS BUT EACH STUDENT SHOULD SUBMIT HER/HIS OWN REPORT
Handling and plotting theoretical and real discrete-time signals and mathematical
functions in Matlab
Matlab provides a user-friendly programming environment with great flexibility, very well-suited to
modeling, analyzing physiological data and developing biomedical signal processing algorithms. In this lab
we will become familiar with handling and plotting discrete-time signals in the Matlab environment – both
theoretical signals we generate with equations, and real data.
Part I: mathematical function plotting
Say you want to generate a sinusoid of a certain frequency, and plot it. Matlab, not being a live thinking
entity, has no clue what you want to do so you need to specify EVERYTHING to it.
Let’s say you want to plot a 5-Hz sinusoid over a 2-second time frame beginning at 0. We want it to have an
amplitude of 2 and have a cosine phase (begins at its peak/maximum at time 0). We are trying to construct
a signal that is a function of time, so we should therefore define ‘time.’ And it’s a discrete-time signal so we
need a value of the signal only at discrete – usually evenly-spaced – times. Let’s choose a ‘sampling rate’ of
200 Hz, in other words, choose to evaluate the function in discrete 5-ms steps:
>> t = [0:0.005:2];
% define ‘time’ in seconds
Now that we’ve defined a discrete time-base, let’s evaluate our desired function at these times to generate
>> f = 5;
% define frequency of sinusoid as a parameter, in Hz
>> my_signal = 2*cos(2*pi*f*t);
% our cosine
Now, plot your signal against time:
It looks nice and smooth, as if it were continuous. But it’s not - it’s discrete, but has a fine enough sample
rate that it appears continuous. To see this, use the ‘stem’ function instead of ‘plot’ and zoom in to take a
closer look – it’s just a series of discrete numbers.
Problem 1: write a function that generates and plots a cosine as above, which takes the following
arguments (input parameters) and produces the following outputs:
function x = plotsinusoid(f,a,startT,endT,step)
where f is the frequency of the cosine, a is the amplitude, startT and endT the start and end times in
sec, and step gives the sample period (reciprocal of sampling rate). x is the generated cosine signal.
As a hint to see how functions work, here is a two-line m-file ‘expsig.m’ that generates (although doesn’t
plot) an exponential signal
for a series of integers n running from 0 to N-1:
… It’s that simple!
NOTE: THE M-FILE SHOULD BE SAVED UNDER THE SAME NAME AS THE FUNCTION
Use the functions xlabel and ylabel to give the axes labels – ‘Time (sec)’ and ‘Amplitude’. Run the function
from the command line with parameters f,a,startT,endT,step of your choosing (different from
above), and reproduce the command and resultant plot in your report.
IMPORTANT: make a folder on your computer to contain all the files you will make, and GO THERE in
Matlab. Before you leave SAVE everything from that folder on a memory stick or email it to yourself.
Problem 2: In signal processing we use complex exponentials to represent sinusoids because they can
represent both the phase and the amplitude of the sinusoid in their angle and modulus, respectively. Recall
Now write a new function
‘plotsinusoid2.m’ that takes the SAME
input arguments as ‘plotsinusoid.m’ but
outputs the sinusoid x in complex exponential
form instead. Also, have it produce TWO figures:
in the first (shown on right), plot the real part of
x, the imaginary part of x and the modulus of x in
the same axes. In the second plot the angle in
DEGREES with the same time-base. Label all axes
in all plots. Again reproduce the command and
plots in your report. You will find the matlab
functions exp, real, imag, abs and angle
useful. To make a new figure in matlab and
enable plotting of multiple traces in that figure,
figure; hold on
Part II: loading and plotting physiological data
Problem 3: Download the file ‘EvokedPotential.mat’ from Brightspace and put it in your working folder
(your ‘current directory’ in Matlab). This mat file contains a single vector of the same name, which is a
hypothetical electrical signal recorded from the scalp using electroencephalography (EEG), which was
evoked by a beep that was played in the subject’s ear. This subject is suspected to have multiple sclerosis
(MS), and to aid in diagnosis, you are asked to determine the post-beep time in milliseconds at which the
FIRST POSITIVE PEAK occurs. This latency tends to be longer in individuals with MS, averaging around 150
ms compared to around 120 ms in neurologically healthy people. You are told that the ‘epoch’ or time
frame of this record extends from -100 ms (i.e., 100 ms BEFORE the beep event) to +650 ms, where the 0
ms point coincides with the beep onset.
Load EvokedPotential.mat into the workspace. From the information above, determine the correct timebase that the evoked potential should be plotted against, and plot it. By zooming in the figure, determine
by eye the time, in MSEC RELATIVE TO THE BEEP, at which the first positive peak occurred. Provide the plot,
the peak latency and the code used in your report. How would you diagnose this subject, and why?
Part III: basic programming in matlab
Problem 4: The Root Mean Square of a discrete-time signal is given by
We can easily calculate the RMS of signals in MATLAB using a combination of the sum, sqrt, and ^
commands. N represents the number of samples of the signal. Please note that the command .^ applied to
a vector squares each element of the vector.
Write a script to calculate the RMS of the discrete-time signal x, defined as follows:
n = a vector of number between -10 and 10 with increments of 1.
x = n/3 + n3
There is a function that calculates the rms in matlab (rms) compare the results of your formula with that of
the native function
Problem 5: Averaging. This technique is very useful for de-noising periodic signals (of known period) or
event-locked quasi-deterministic signals when the signal and the noise are uncorrelated. Generate a 100
seconds cosine with a frequency of 5Hz, using 100 samples per second (starting from the instant 0.01). Each
second of the sinusoid signal represents one epoch. Now generate a vector of white noise, of the same
length as the cosine, using the function rand (amplitude = 3);
Plot the 12th epoch of the noisy signal.
Use a for loop to generate and plot a signal that is the average of all the epochs in the first 10 seconds of
the signals (thus the first 10 epochs). Then use another for loop to generate and plot a signal that is the
average of all the epochs in the 100 seconds of the signal. Present and comment the plots on your report.
the cosine epochs
Upload your report including all plots and WELL-COMMENTED code to blackboard
by Thursday October 3.
Purchase answer to see full