代写 R data structure math matlab graph Laboratory Tasks

Laboratory Tasks
MSc Introductory Module (Part I)
Peter Janˇcoviˇc September 29, 2019
Laboratory Instructions
You should work in groups of two (however, there is no problem if anyone wants to work alone). Each group should record the outcomes of their work in one lab-report and store any required program and sound files.
Tha lab-report can be written in LaTeX or MS Word. The front page of the report should contain the IDs of both persons working in the group. The report should be written using font size of 11pt but Matlab code listings included in the report should have the font size of 9pt.
Submission of the lab-report (as well as all Matlab codes and any other required files) is through Canvas – under the ‘MSc Introductory Module for Computer Engineering’, find the assignment ‘Lab-Report Submission’. Please submit .pdf (or .docx) file for the report, .m files for MATLAB codes, and .wav format sound files. Make sure you attach all the files required and make your submission. Each group should make a single submission.
The usual penalty of 5% per day will apply to all late submissions.
Plagiarism
Plagiarism will not be tolerated. It is the act of a Student claiming as their own, intentionally or by omission, work which was not done by that Student. Plagia- rism also includes a Student deliberately claiming to have done work submitted by the Student for assessment which was never undertaken by that Student, in- cluding self-plagiarism and the other breaches. Sanctions of a plagiarism include the Student failing the Programme of study.
1

1 1.1
a)
b)

Programming in Matlab
Loops, arrays and indexing (6 points)
Consider a row vector a defined as a = [1, 4, 1, 3, 2, 5, 0]. Construct a row vector b that contains the indices of elements of a which are larger than the mean of a and a row vector c that contains the values of elements from a which are larger than the mean of a.
Perform this exercise first by using a ‘for’ loop and then by using logical operators and ‘find’ function without any use of ‘for’ loop.
Consider a matrix R defined as follows:
345236 R=12 95 23 0 29 39.
57 64 72 41 8 91 47 28 31 62 84 37
Constructs a matrix S (of the same size as matrix R) that keeps the values from R that are larger than 5 and smaller than 60 and replaces all the other values with −1. Perform this task first by using two ‘for’ loops and then without any ‘for’ loop.
So, the resulting matrix S should be:
−1−1−1−1−1 6 S=12 −1 23 −1 29 39.
57 −1 −1 41 8 −1 47 28 31 −1 −1 37
File operations and structures (5 points)
1.2
The text file ‘textFileEx3.txt’ (provided at Canvas) contains formatted data as given below – there first line is a header information, the following lines give the useful information to store, and the last line with a dot indicates the end of the file.
Name
NESTLE SWATCH ZURICHFINANCE SWISSCOM
UBS .
Price
50.62 364.20 48.24 350.90 11.07
Volume 1137511 51947 329296 61944 4916261
Write a Matlab program which reads the formatted data from the text file and stores these into an appropriate data structure. You are NOT ALLOWED to use Matlab functions ‘textread’ and ‘textscan’.
2

1.3 Functions and user input (9 points)
Write a text-based menu-driven program (not GUI) to perform calculations of the constant π. Many mathematicians have found ways to approximate π. For example, Machin’s formula is:
π 􏰃1􏰄 􏰃1􏰄 4 = 4 arctan 5 − arctan 239
Leibniz found that π can be approximated by: π=4−4+4−4+4− 4 +…
1 3 5 7 9 11
This is called a sum of a series. There are six terms shown in this series. The
first term is 4, the second term is −4/3, the third term is 4/5, and so forth. The text-based menu-driven program should have the following choices (selected
by a user by pressing a specified letter, ‘a’, ‘b’, ‘c’, or ‘d’):
(a) Print the result from Machin’s formula.
(b) Print the approximation using Leibniz’ formula, allowing the user to spec- ify how many terms to use. Also plot a figure showing the error as a function of the number of terms used.
(c) Print the approximation using Leibniz’ formula, looping until an approx- imation with an error smaller than specified by the user is found.
(d) Exit the program.
Your Matlab program to perform Leibniz formula calculations (option (b) and (c)) should be in a form of a function. This function should accept input param- eters (the required number of terms or the required error value) and it should return the calculated approximation, the error of the approximation and the number of terms used.
For the option (b) above – provide in the report the outcomes of your program when using 100 terms and plot a figure showing the error as a function of the number of terms used.
For the option (c) above – provide in the report the outcomes of your program when the required error is set to 0.001 and state how many terms was needed to obtain this approximation.
Deliverables:
For each of the above tasks, include required outputs and the listing of your Matlab programmes in the lab-report. Also attach all your Matlab programmes with your Canvas submission.
3

2 Digital FIR Filters (15 points)
The objective of this exercise is to gain a better understanding of the finite impulse response (FIR) digital filters. The exercise is to be performed using the wav-file ‘sineWave1000Hz+whiteNoiseHPF Fs20kHz Oct2019.wav’, which contains a sine-wave signal of frequency 1000 Hz corrupted by a noise. The sampling frequency is Fs = 20 kHz.
Your task is to write an m-file that implements FIR filtering using two different types of filters as given below. Consider that the FIR filter is defined by the difference equation: y(n) = b0x(n) + b1x(n − 1) + … + bKx(n − K) wherethefiltercoefficientsb0,b1,…,bK shouldbesettovaluesasgivenbelow. Assume that values of samples in the input signal x(n) for time t ≤ 0 are zero.
• Filter 1: this is a 2nd order FIR filter with filter coefficients b0 = 1 and b1 = −1.
• Filter 2: this filter is defined by its impulse response
h(n) = {h(0), h(1), . . .} =
= {−0.12, −0.24, −0.06, 0.8, 1.35, 0.8, −0.06, −0.24, −0.12}.
To perform this task, you are NOT ALLOWED to use Matlab functions: ‘filter’ and ‘conv’. You should implement the equation relating y(n) and x(n) as given above.
You can read sample values of an audio signal stored in ‘.wav’ format into a 1-D array using the function ‘audioread’. You can save a 1-D array into an audio ‘.wav’ format using the function ‘audiowrite’.
You can listen to the content of a 1-D array (containing audio samples) by typing in Matlab command window ‘p=audioplayer(sigWav,Fs); play(p);’ (with- out the quotes!) where ‘sigWav’ is a 1-D array containing the audio samples.
After you have performed the above filtering operations, calculate the mag- nitude frequency characteristic of each filter by using the discrete Fourier trans- form (DFT). To do this, first, find the impulse response of the filter. Then, append the impulse response vector with zeros in order the full length is 256. Perform the DFT of this zero-expanded impulse response and take the absolute value to get the magnitude frequency characteristic of the filter. To get the magnitude characteristic in dB, you need to apply 20 · log10.
Note that the length of the zero-expanded impulse response determines the number of frequency points the entire frequency range from 0 to Fs is divided into (which is here 256). Based on this, the frequency index on the x-axis can be converted into frequency in Hz.
Deliverables:
i) Plot the waveforms of the input and output signals from each filter.
ii) Submit the output .wav file produced by each filter and your Matlab program. Also include the listing of your Matlab program in the lab- report.
4

iii) Plot the magnitude frequency characteristics of each filter and indicate on the x-axis the point corresponding to the frequency of 1000 Hz. State whether the filter is a low-pass, band-pass or high-pass filter.
iv) Comment, with detailed justification, on the strength of the sine-wave and noise in the output signal obtained by each filter.
5

3 Frequency analysis 3.1 Spectrogram (15 points)
Create a program in Matlab that loads an audio signal in ‘.wav’ format and calculates and plots spectrogram of the signal.
The input to the function should be the name of the .wav file and the pa- rameters needed for calculating the spectrogram, i.e., frame length, frame shift, and window type. The window type should be either Rectangular or Hamming window. The output should be the calculated spectrogram (as a 2D array).
You can read sample values of an audio signal stored in ‘.wav’ format into a 1-D array using the function ‘audioread’. You can use Matlab function ‘fft’ to perform the DFT calculations, ‘abs’ for absolute value, ‘log10’ function for logarithm, and the ‘imagesc’ function to plot a 2-D array (spectrogram). You are NOT ALLOWED to use any other ready-made functions available in Matlab or elsewhere (such as ‘specgram’).
Deliverables:
i) Submit your developed Matlab program and include the program listing in the lab-report.
ii) Using your developed program, analyse spectrograms of the sound files sound flute labMScIntro.wav and sound bird labMScIntro.wav. Dis- play the waveform and the spectrogram of each sound when using the frame length of 10 ms and 60 ms (in both cases use the frame shift of 5 ms and the Hamming analysis window). Label the axes in your figures. Explain in details the effect of different frame size on spectrogram analysis of each sound. Justify your answers.
3.2
Fourier transform (9 points)
Let x(t) be the signal depicted in Figure 1.
Figure 1: Signal x(t).
6

Deliverables:
• Determine the Fourier transform X(f) of the signal x(t) depicted in Fig- ure 1. Perform the calculations manually. Show all your workings.
3.3 Discrete Fourier transform calculations (6 points)
Let f be the discrete 8 point function given by: f(0) = 2,f(1) = 1,f(2) = 1,f(3) = 1,f(4) = 0,f(5) = 1,f(6) = 1,f(7) = 1.
Deliverables:
• Calculate all 8 terms F(0),F(1),F(2),F(3),F(4),F(5),F(6),F(7) of the discrete Fourier transform (DFT) F of f. Perform the calculations man- ually using the DFT equations given in lectures. Show fully all your workings. Explain the meaning of the obtained result.
7

4 Linear algebra 4.1 Vectors (5 points)
7 5 Suppose v = −3, and w =  3 .
 0   − 8  21
Deliverables:
• Calculate v · w
• Calculate ||v|| and ||w||
• Calculate the value of the angle between v and w. Perform all calculations by hand. Show all your workings.
4.2 Linear independence (5 points)
3 5 6 Let u =  1 , v = −2, w = −3.
−7 0 7 Deliverables:
• Show whether u, v, w are linearly independent or not? Perform calcula- tions manually. Show all your workings.
8

5 Probability
5.1 Independence (4 points)
Suppose that we have a coin which comes up heads with probability p, and tails with probability q. Now suppose that this coin is tossed twice. Using a frequency interpretation of probability, it is reasonable to assign to the outcome (H, H) the probability p2, to the outcome (H, T ) the probability pq, and so on.
Deliverables:


5.2
Let E be the event that “heads turns up on the first toss” and F the event that “tails turns up on the second toss”. Are these two events independent? Show your workings.
Let now I be the event “heads on the first toss” and J the event “two heads turn up”. Are these two events independent? Show your workings.
Probability mass function (9 points)
Consider four independent rolls of a 6-sided die. Let X be the number of 1s and let Y be the number of 2s obtained.
Deliverables:
• What is the joint probability mass function of X and Y ? Show your workings.
5.3 Gaussian mixture model (12 points)
Suppose that g is a Gaussian mixture model defined by g(x) = 0.3g1(x) + 0.7g2(x)
where
g1 is a Gaussian PDF with mean m1 = 6 and variance v1 = 4, and g2 is a Gaussian PDF with mean m2 = −1 and variance v2 = 1.
Deliverables:
• Calculate manually g(3). Show all your workings.
• Carefully sketch by hand a graph of g1, g2 and g (indicating values at
several points on the graph).
• Suppose r is a random variable governed by g. Show that P(4 ≤ r ≤ 8)
is approximately equal to 0.2. END
9