1. MATLAB
Start MATLAB and follow along with the commands by entering them yourself in either the Command Window or in a Live Script. This section is not part of your report submission, it is just to get you familiar with the tools you need for the next section.
1.1. Complex numbers
MATLAB handles complex numbers natively and needs no special treatment.
>> sqrt(-1) ans = 0.0000 + 1.0000i
1.1.1. Rectangular form
The default display format is to show complex numbers in rectangular form (Real + j Imaginary).
Also by default, both i and j are defined like variables to represent the imaginary number:
>> j ans = 0.0000 + 1.0000i
>> i ans = 0.0000 + 1.0000i
If you define new variables with the same names i or j (such as for loop counters), then the variables will no longer represent complex numbers. This can be a nasty source of subtle bugs! |
A better way to enter a complex number into MATLAB is the form:
>> z = 2.9 + 7.4j z = 2.9000 + 7.4000i
where the imaginary part of your number is denoted by a suffix j
or i
.
Note that MATLAB understands j
, but will prefer to display with the i
suffix.
It is sometimes useful, especially when programming, to create a complex number (or vector!) from separate real and imaginary parts like:
>> z = complex(a, b)
Once you have a complex-valued variable, you may wish to extract only the real or imaginary part. Also easy:
>> real(z) >> imag(z)
1.1.2. Polar form
The polar form of a complex number is (magnitude, angle), or written as:
MATLAB does all calculations in radians. There are versions of functions that use degrees for angles, but only use those if you are clear about the consequences for introducing subtle bugs. |
>> x = 3 + 4j >> y = 2 * exp(1j * pi/3) >> z = sqrt(2) * exp(1j * pi/4)
Above are a few example variables to define that we’ll use below.
The magnitude of these numbers is computed with the abs()
function:
>> abs(x)
which is the same as using the Pythagorean theorem:
>> sqrt(real(x)^2 + imag(x)^2)
The angle of a number is extracted by:
>> angle(y) >> angle(z)
Convert these from radians to degrees:
>> rad2deg(angle(y)) >> rad2deg(angle(z))
or
>> atan2(imag(y), real(y))
Why atan2(im, re) ? Because the single-argument function atan(a) can’t distinguish between quadrants I and III, or quadrants II and IV.
|
All of these conversion functions will accept single numbers and arrays of numbers.
1.2. Creating vectors
Frequently, we have an expression that is a function of some varying term, such as frequency in hertz or radians/second. As you saw from the last lab, it is useful to plot the value of the expression as the variable changes.
For frequency response, it is common to plot using a logarithmic frequency scale. This is where each 10× frequency range is the same graphical width.
The logspace()
function easily generates log-spaced vectors:
>> w = logspace(0, 3, 100) ; % notice the semicolon
generates 100 values from \(10^0\) to \(10^3\). See this by making a plot:
>> plot(w, 'x') % mark points with an 'x'
Since we didn’t specify both x and y vectors, MATLAB just uses indexes for the horizontal axis and the values of the vector elements for the vertical axis. Notice how the plot looks like an exponential --- this is what log-spaced values look like on a linear scale.
Next, use log-spacing for the vertical axis:
>> semilogy(w, 'x')
…back to a straight line. Notice the vertical axis scale!
What if you want log-spaced values that don’t start or end at exactly a power of 10? First compute the log10 of your number and use the result in for the argument. >> logspace(log10(3), log10(42), 4) |
2. Frequency response
2.1. RC low-pass
Compute the transfer function \(H(s)\) of Figure 1 and use the same R and C values as you did from the end of the last lab.
Replace every instance of \(s\) with \(j\omega\) to give \(H(j\omega)\). It will look something like:
The denominator is a single complex number
-
Create a frequency vector (ω) that spans the same range as your simulations from last time. Convert from hertz to rad/s.
>> w = logspace(...);
-
Compute your transfer function at these frequency values:
>> Hw = 1 / (1 + 1j*R*C*w);
Oops! MATLAB is trying to do a matrix division operation and not element-by-element division.
The dotted forms of binary operators (.*
, ./
, and .^
) do an elementwise operation as you might expect.
The non-dotted forms are defined in linear algebra terms.
>> Hw = 1 ./ (1 + 1j*R*C*w);
Plot the magnitude of this vector against the frequency:
>> plot(w, abs(Hw)) >> xlabel('Frequency $\omega$ rad/s') >> ylabel('|H(jw)|')
This plot will look more like your PSpice plots if you use logarithmic scales for both axes:
>> loglog(w, abs(Hw))
Cleanup your plot so the axes are reasonable (and ~match PSpice and Waveforms). Include axis labels because no one likes to read un-labeled plots.
2.2. RLC low-pass filter
-
R = 1 kΩ
-
L = 1 mH
-
C = 3.3 nF
Find the transfer function of this filter, \(H(s)\), substitute \(s \rightarrow j\omega\), and plot the magnitude of the frequency response of this filter from 100 Hz to 10 MHz.
Re-compute and re-plot for two more resistor values of 100 Ω and 10 kΩ.
Add appropriate axis labels and titles to these plots.
>> title('Some title')
2.3. RLC band-pass filter
Same element values:
-
R = 1 kΩ
-
L = 1 mH
-
C = 3.3 nF
The natural frequency of this and the previous circuit is:
which is about 87 kHz in this case.
Plot the magnitude of the frequency response of this filter over the same frequency range using linear scale for the magnitude and logarithmic for frequency: semilogx()
.
Then plot using loglog()
.
You did add axis labels? |
The peak of the response will be around the natural frequency ωn and the name “band-pass” should make sense.
3. Submission
Create a MATLAB Live Script to hold your work from Section 2. Make sure that the document has a “readable” quality to it so that someone not immediately familiar with the lab procedure is able to follow what you did and the resulting … results.
Export (SaveAs) as a PDF to upload to Blackboard.
4. References
- Ulaby book
-
-
Section 7-2 is a nice review of complex algebra.
-
Section 9-4 covers passive filters of the sort that we’ve been using lately. Plots from Section 2.2 will look like Figure 9-20 on p.529 (pdf.543). Notice how nearly all of the frequency response plots have log-log axes.
-
- Math is Fun: Complex Numbers
-
A basic introduction and review of complex numbers and the mechanics of algebraic manipulation.
- A Real-Imaginative Guide to Complex Numbers
-
A rather interesting read that starts from first principles and reasons through to visualizing complex numbers to arbitrary powers as rotation and spirals in 3 dimensions. As implied by the site, with a bent towards how complex numbers are used in digital signal processing.