Sale!

ECE-210B Homeworks 1 to 7 solution

$150.00 $90.00

Original Work ?

Download Details:

  • Name: Homeworks-fuewf9.zip
  • Type: zip
  • Size: 589.24 KB

Category: Tags: , You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (1 vote)

ECE-210B Homework 1

In this assignment, you will utilize the basic skills you learned in the first week of class; you
will assign values to variables, use some of MATLAB’s basic functions, and perform some
simple arithmetic operations on scalars and matrices. You should also use this assignment
as a way to get comfortable with the help and doc functions. For your final submission, as
always, please suppress all outputs with semicolons. Separate each question into separate
sections. It may be good to get into the habit of beginning your scripts with clc, clear
and close all. Don’t remember what these do? Use help!
1. Create scalar variables with the following values:
• log10(26)
• 5e
j

3
• arctan(√
15) + 34


3
2 + j
1
2
Give these variables unique and reasonable names. Using these variables (not the
expressions) create a column vector with each variable as an entry.
2. You should have two complex variables from the previous question. Multiply the
two together (using the variables again) and save that as a variable. Compute the
real part, imaginary part, magnitude and angle of the resulting complex number and
create a row vector using those values as entries.
3. Now with the two vectors from the previous questions, create and save as variables
two 4×4 matrices as follows:
• Regular matrix multiplication (make sure the vectors are in the right order! I
want a 4×4 matrix, not a scalar, i.e. an outer product, not an inner product)
• Transpose (regular transpose not conjugate transpose) the column vector and
multiply it elementwise with the row vector. Then use repmat to extend this
row vector to a 4×4 matrix (use help or doc if you don’t remember how to use
repmat).
4. With these two matrices, perform the following operations (save the results as variables):
• Add the first and two times the second
• Elementwise multiply them
• Subtract two from every entry in the first matrix
• Conjugate transpose either one
1
ECE-210B Homework 1
5. Take the angle of the complex number in question 2 and convert it to degrees (check
out rad2deg). It should be an integer. Save the value as n. Then create these two
row vectors:
• A length 2000 vector with equally spaced entries from 1 to n
• A vector with entries starting at one up to n with entries spaced at intervals of
0.3 (the vector may turn out to not include n)
You will be using both linspace and the colon operator here. Make sure to use the
right one for each!

ECE-210B Homework 2

In this homework, you will review some of the topics discussed in class this week, including approximating derivatives and integrals and different matrix operations. You will also see that for loops
are usually not the best idea in MATLAB…
1. Here you will be approximating derivatives and integrals on the sine function.
• Create two vectors with entries evenly spaced between 0 and pi. One vector should be
of length 100 and the other should be of length 1000. Then apply these vectors to the
function sin(2x).
• Approximate the derivative of this function by taking differences of adjacent elements
and dividing by the space between them. Do this for both vectors.
• What function should this be? (I hope you know this…) Check this by applying the
original evenly-spaced vectors to this function, taking the elementwise difference between
the resulting vectors and the derivative vectors and computing the magnitudes of the
max differences between them. Which length produces better results?
• Now use cumsum and cumtrapz to approximate the integral of sin(2x). Do this for
both lengths as before.
• Subtract 0.5 from every element of each integral vector to center their graphs at 0. Then
compare the computed vectors with the analytic antiderivative as before. Return the
magnitude of the max error for each of the four cases.
• Plot the best approximation for the integral. Add a title to the plot.
2. Perform the following matrix operations:
• Use reshape to create the 10×10 matrix A =





1 11 … 91
2 12 … 92
.
.
.
.
.
.
.
.
.
.
.
.
10 20 … 100





.
• Flip A upside down.
• Flip the 3rd row left to right.
• Create a row vector which is the column wise sum of the entries of A.
• Create a column vector which is the row wise product of the entries of A (Check out
prod).
• Delete the 6th row.
3. Create a 300×500 matrix B where Bij =
i
3+j
3
i+j+2 . Do this, and time how long each one takes
with tic toc, using three methods:
• Using a for loop without preallocation.
• Using a for loop with preallocation.
• Using only elementwise matrix operations (meshgrid may be useful here).

ECE-210B Homework 3

In this homework, you will review logical indexing in MATLAB, a very important technique used
to efficiently index a matrix or vector. You will also have a chance to work with functions in a
separate file.
1. Roll the Dice In this part you will work with imshow and logical indexing.
• Create 100×100 matrices A, B and C of all ones.
• In matrix A, set the values of the entries aij equal to zero if p
(i − 25)2 + (j − 75)2 < 10
or p
(i − 75)2 + (j − 25)2 < 10. (Hint: Use meshgrid to create the indices, then
logical indexing on A using |, & or˜).
• In matrix B, set the values of the entries bij equal to zero if p
(i − 25)2 + (j − 25)2 < 10
or p
(i − 75)2 + (j − 75)2 < 10.
• In matrix C, set the values of the entries cij equal to zero if p
(i − 50)2 + (j − 50)2 > 10.
• Now use figure and imshow to plot:
– The complement of C
– A
– The next 3 faces of a die (so 3-5) on three separate figures. Use whatever logical
operations (&, |, or˜) are necessary to accomplish this.
2. Fun with find Write a function to return the value and index of a number in a vector
/ matrix that is closest to a desired value. The function should be called as [val, ind] =
f indClosest(x, desiredV alue). This function can be accomplished in less than five lines. You
will find abs, min and/or find useful, Hint: You may have some trouble using min when x
is a matrix. To convert the matrix to a vector, you can use y = x(:). Show that it works by
finding the value closest to 3/2 (and index of said value) in sin( linspace(0,5,100) ) + 1.
3. Calculus Nostalgia This problem will reacquaint you with the first derivative test and points
of inflection.
• Write a function, called signSwitch, in a separate file which inputs a vector v and outputs
a vector with the indices i which represent a sign change in v; i.e. report 15 if the sign
changed in v between index 14 and index 15. Do not consider going from positive or
negative to zero. We could loop through and check this condition at every point – don’t
do that. Instead think of a way to use logical indexing: One suggestion is to write
conditions on the vector and some kind of shifted version of itself. Beware however,
when you do this you will have non-overlapping points. It is up to you to figure out
what to with them. This will be a local function (see the documentation if you forgot
what this means).
• Now we will write the main function, call it whatever you’d like, which will perform all
the analysis. The function will input two vectors representing the x and y coordinates
of the graph of the function and will return a vector with the approximate indices of the
local extrema and a vector with the approximate indices of the points of inflection.
• First have the function take the first derivative (approximately, see homework 2). Then
use your local function to apply the first derivative test to see where the approximate
local minima and maxima are.
1
ECE-210B Homework 3
• Next, have the function take the second derivative. Then use your local function again
to find approximately where the points of inflection are.
• Finally, have the function plot all this information using figure then
plot(x,y,xminmax,yminmax,’ko’,xpoi,ypoi,’r*’). This will plot the function then plot
black circles on the local minima and maxima and red stars on the points of inflection.
• Apply your function to x
5 − 8x
3 + 10x + 6 sampled at 10000 points on [-3,3].

ECE-210B Homework 4

1. Professor Mintchev has just assigned you 20 tedious Gram Schmidt Orthonormalization
problems! Luckily, you are a master of MATLAB so you decide to build a function which
can handle them all for you in short time. Note: This homework has been assigned in all
three ECE-210 sections.
• Create a function called gramSchmidt. The input to the function should be a 2-D
array, each column of which is a vector in the original linearly independent set of
vectors. Implement GS to create an orthonormal set of vectors from these. Store
them as columns in an output matrix, similar to the input format. Feel free to use
the norm function if needed.
• After you’ve created this function, you’d like a way to test if it works. Create another
function called isOrthonormal which has a single 2-D array as the input. The function
should return 1 if all columns are orthonormal and 0 otherwise. Be careful with this
– direct floating point equality comparison is a bad idea. Instead apply a threshold
to the difference of the two numbers like so: if |x − xˆ| >  then … The eps function
might be useful here. You can add a nice big fudge factor to make the tolerance big
enough that it works, just don’t make it huge. (Note that there is also the matter of
spanning the same space as the original matrix, don’t worry about this condition)
• Finally, we would like to estimate another vector as a linear combination of these
orthonormal vectors. (Project the vector onto the space of the orthonormal vectors.)
Implement a function called orthoProj which takes a vector to be estimated and and
array of orthonormal columns as arguments and outputs the estimated vector.
• Test all of the above functions on some random complex vectors. (use rand to make
a random vector) First test the case where there are more elements in each vector
than the number of vectors. Then test the case where the number of vectors is equal
to the number of elements in a vector. Compare the errors.
• Uniformly sample sin(x) on [0, 2π]. Generate 5 Gaussians with the equation
1

2πσ2
exp
−(x − µ)
2
σ
2
Give each Gaussian standard deviation 1 (σ = 1) and pick the mean from a linearly
spaced vector ranging from 0 to 2π. (µ ∈ {0, π/2, π, 3π/2, 2π}) Consider using ndgrid
for compact code. Plot the Sinusoid and Gaussians on the same plot. Give axis
labels and a title. Use gramSchmidt to create an orthonormal set of vectors from the
Gaussians. Use orthoProj to estimate the sinusoid from that set of vectors. Create
a 2×1 subplot. Plot the sinusoid and the estimated sinusoid together on the upper
plot. Plot the orthonormal basis functions on the lower plot. Give all plots proper
labels and titles.

ECE-210B Homework 5

In this homework, we will go through a few advanced data structures in MATLAB, such as cell
arrays, classes, chars, as well as dataset importing in MATLAB. You will be importing in the
fisheriris dataset and encapsulating it in classes. After obtaining all the data, you will be required
to write some methods for the class you just created. Also note that some operations will require
you to do some research, but not too much!
1. Load in the fisheriris dataset with the command load fisheriris. You should obtain a 150×4
matrix called meas, as well as a 150×1 cell array called species in your workspace. This is a
very popular dataset, and you can quickly google fisheriris to see what the columns of meas
represent.
• Create a class called Flower in its own .m file. In your Flower class, you should have the
following properties – petalWidth (double), petalLength(double), sepalWidth (double),
sepalLength (double) and species (char). The ith species corresponds to the ith row of
information in meas. Note that you do not need to specify the data type of the properties
when you are declaring a class the following properties are just here to impress on you
what type of properties you would be expecting.
• Create a constructor for your class. It should take in 4 doubles and a char array corresponding, in order, to the five properties of your class.
• Now, import the entries from meas and species into MATLAB and store them in a 150×1
cell array of Flower instances. You can either use a for loop to import the entries, or
use a more elegant way to make all entries in one line (this would require some research
on object formation in MATLAB). Either way is ok. However, note that the name of
the species are stored as cell arrays; make sure to extract from the cell as a char and
remove trailing white spaces before storing them in the Flower instances. There is
a single function that will do this, which you should be able to find with a quick google
search.
• Create a method called getSWidth for the Flower class, which will return the sepal length
of the object. Use this on the 30th Flower in your cell array, and check (using ==) that
this is the correct value by looking that the corresponding entry in meas.
• Create another method called report for the Flower object, which will print out a statement on the command window and report the details about the Flower object. This
function does not need to return anything. It should print out a statement of the following form if the flower is 5.1 cm in sepal length, 3.5 cm in sepal width, 1.4 cm in petal
length and 0.2 cm in petalWidth, and the species is setosa, for example: “The length
and width of its sepal are 5.1 cm and 3.5cm respectively, while the length and width of
its petal are 1.4cm and 0.2cm respectively. It belongs to the setosa species.”

ECE-210B Homework 6

This homework deals with digital filters in a low-level sense. You are expected to
know a bit about the z-transform, but if you are not in Signals and Systems, please
contact me separately for some additional information on this homework if you need
it. Read the parts carefully and make sure to complete every part of the homework.
This homework requires you to produce a few plots; I want nice plots! Axis labels
and titles are a must.
1. For this question, you will be working with the discrete system described by
the transfer function:
H(z) =
2
5
z
2 +
1
4
z +
1
7
1
3
z
3 −
1
8
z +
3
2
• Store this transfer function as numerator and denominator polynomials.
Be VERY careful setting this up. Check the documentation for zplane
to see how discrete transfer functions are handled in MATLAB.
• Compute the poles and zeros using a specific MATLAB function. (Make
sure you use the right one for discrete signals and not the one used mostly
for continuous signals!)
• Create a poly zero plot using a different MATLAB function. You may use
either the poles and zeros themselves or the numerator and denominator
polynomials.
• Use impz to compute the impulse response of this transfer function. Compute only the first 50 points of it (there is a way to do this in the function
itself). Make a stem plot of the impulse response.
• Let x[n] = (−
4
5
)
n
. Then use filter to apply the transfer function (or
filter) to the signal. In subplots, plot the before and after.
• The above is the easiest way to apply a filter, but you also ought to be
able to do this analytically, using either convolution in MATLAB or the
product of z-transforms. Show me you know how to do this! That is,
plot the same answer achieved in another way. You don’t have to take
any inverse z-transforms to do this! Note that if you use convolution with
the impulse response, you’ll get a longer vector than when you used filter.
Therefore, only plot the first n points where n is the length of the vector
result from the previous part.
2. In this question, you will be ”designing” a bandpass filter (probably unrealistic
but it’s what I came up with). A bandpass filter is a system which only allows
a certain band of frequencies from a signal to pass.
1
ECE-210B Homework 6
• Last question you converted a transfer function in tf form to one in zpk
form. Now we will do the opposite. Compute numerator and denominator
vectors for a transfer function with k = 0.01 and these zeros and poles:
zeros : −1, 1
poles : 0.9e
j
π
2 , 0.9e
−j
π
2 , 0.95e
j

12 , 0.95e
−j

12 , 0.95e
j

12 , 0.95e
−j

12
• Create a pole-zero plot.
• Now compute the frequency response of this filter using freqz . Use n =
1024 points and return an H frequency response vector and a w frequency
vector. DO NOT use the option to plot the frequency response. You
will be manually creating the plots.
• The H vector is a complex vector. That means it has both magnitude
gain (via abs) and phase (via angle). Using subplots, plot the magnitude
and phase against the w frequency vector. A few things to note (and will
be expected in addition to proper plots):
– Plot the magnitude in dB. You can convert a gain x to dB via
20log10(x).
– Plot the phase in degrees. You will notice if you do that, the plot
will have weird sharp edges. Remove them using unwrap before
converting to degrees.
– Show units in the axis labels. (Remember the frequency vector, w,
has units radians not radians per second.)
– Remember the frequency vector, w, only goes from 0 to π. Make sure
to use xlim, xticks and xticklabels.

ECE-210B Homework 7

In this assignment, you will reinforce what we did in lecture today regarding MATLAB’s filter toolbox.
For each of the following questions, you will create a filter, create magnitude-phase
plots for the filter and apply the filter to a signal. Follow these steps:
• Generate MATLAB code for filters using the filter design toolbox in the signal
processing toolbox (filterDesigner).
• Create a filter object by calling the generated code.
• Use the DSP toolbox’s version of freqz on the filter object. Make sure to
include the sampling frequency in the function call as this is hardly mentioned
in the documentation. For example, if f ilter is a filter object, n is the number
of points (you can use 1024) and fs is the sampling frequency, run [H, f] =
freqz(f ilter, n, fs). Note I use f instead of w since by including the sampling
frequency, MATLAB scales the frequencies from [0, π] to [0, fs/2]. Hence these
frequencies have units of Hertz. Keep that in mind when including units in
your plots and setting the axis limits.
• Create magnitude-phase plots akin to homework 6 except for the difference
mentioned above regarding f.
• Apply the filter to the signal using filter.
• Lastly, plot the Fourier Transform of the final result using fft and plot. Refer
to the notes for the proper way to use fft and obtain the proper scaling.
This may seem daunting, but with properly defined functions, you may only have to
do most of the work once. However, I still want unique titles for plots (maybe pass
in a string?).
1. Generate a signal that consists of a sum of sine waves of frequencies 1 to 50
kHz. Set t to be from 0 to 2 seconds, using an interval of 0.001s.
signal =
50000
X
f=1
sin(2πf t)
2. Create a Butterworth lowpass filter with a sampling frequency of Fs = 100
kHz, a passband frequency of Fpass = 10 kHz, a stopband frequency of Fstop
= 20 kHz, a passband attenuation of Apass = 5dB, and a stopband attenuation
of Astop = 50dB.
1
ECE-210B Homework 7
3. Create a Chebychev I highpass filter with a sampling frequency of Fs = 100
kHz, a passband frequency of Fpass = 35 kHz, a stopband frequency of Fstop
= 15 kHz, a passband attenuation of Apass = 2dB, and a stopband attenuation
of Astop = 40dB.
4. Create a Chebychev II bandstop filter with a sampling frequency of Fs = 100
kHz, a passband frequency of below the frequency Fpass1 = 5 kHz and above
Fpass2 = 45 kHz, a stopband frequency of between Fstop1 = 15 kHz Fstop2 =
35kHz, a passband attenuation of Apass = 5dB, and a stopband attenuation
of Astop = 50dB.
5. Create a Elliptic bandpass filter with a sampling frequency of Fs = 100 kHz, a
stopband frequency of below the frequency Fstop1 = 15 kHz and above Fstop2
= 35 kHz, a passband frequency of between Fpass1 = 20 kHz Fpass2 = 30
kHz, a passband attenuation of Apass = 5dB, and a stopband attenuation of
Astop = 50dB.