A signal is a list of numbers. MATLAB is where you play with it.
Ten minutes of reading. Move the sliders, then the code of the lab will read like plain sentences.
MATLAB and Simulink
MATrix LABoratory: a language where every value is a matrix. A signal is a row vector of samples, so one line of code works on the whole signal at once.
A graphical tool inside MATLAB. You draw a block diagram (sources, integrators, gains, scopes) and its built-in solver works out every signal against time.
A .m file is a list of commands that runs from top to bottom, like typing them in the Command Window one after another.
The MATLAB desktop
- Current Folder: the files you can run. A script is run by its name or with Run in the Editor.
- Editor: where scripts are written. Command Window: where commands are typed and results printed.
- Workspace: every variable that exists, with its size. Figure windows open for every plot.
Commands of this lab
- plot(x,y) joins points with lines; stem(n,x) draws one line per sample, the right picture for discrete signals.
- grid, title, xlabel, ylabel, axis([xmin xmax ymin ymax]) dress up the figure.
- zeros(1,N) and ones(1,N) make vectors; fft(x) gives the spectrum; abs and log10 work element by element.
Vectors and the colon operator
MATLAB counts from 1, not 0. The first sample of x is x(1). In Exercise 3 the vector n runs from −N/2, so the sample at n = 0 is x((N/2)+1). Getting this wrong by one is the most common bug in signal code.
A plain * between two vectors is a matrix product and fails (or gives a surprise). For signals you almost always want the element by element operators .*, ./ and .^. Functions such as sin, exp and abs already work on each element.
Sampling a sine
Only the ratio f/fs matters to the samples. When f goes above fs/2 (the Nyquist frequency) the samples are exactly those of a lower frequency, fs − f: the signal is aliased. At exactly f = fs/2 a sine with zero phase is sampled at its zero crossings and vanishes.
The basic signals
A shift moves a signal: δ(n−k) is an impulse at n = k, u(n−k) a step that starts at n = k. Two shifted steps subtracted, u(n−a) − u(n−b), give a rectangular pulse of b − a samples. A sample outside the vector n simply does not exist, so δ(n−20) on n = −20 to 19 shows nothing.
Spectrum by the FFT
The FFT is a fast way to compute the DFT: about N log2N operations instead of N². Only the first N/2 bins are plotted because a real signal gives a mirror image above fs/2. When f is not a whole number of bins the energy spreads over the nearby bins (spectral leakage) and the peak drops by up to 4 dB.
How Simulink solves a model
A Simulink diagram is a set of equations. The Integrator block holds a state: its output is the integral of its input plus an initial condition. The solver steps through time and, at each step, works out every block in signal order, then updates the states. That is why a line may go back from the output of an integrator to its own input: dx/dt = x has the answer x(t) = x(0) et.
The initial condition can be typed in the block (internal) or brought in on a second port, x0 (external), as in Example 1. A loop without an integrator in it cannot be solved step by step and gives an algebraic loop error.
In the virtual lab
In guided mode each step ticks itself when it is really done: the right file open, the right values set, the simulation run, the value read and typed. Free mode leaves you alone with every file. The report keeps each figure with the values that made it.
What you should see
| File | Settings | Result |
|---|---|---|
| ex1_sine | N = 64, f = 1 kHz, fs = 8 kHz | 8 cycles, 8 samples each |
| ex2_exp | t = 0:0.1:40, e−0.1t | falls from 1 to 0.0183 at 40 ms |
| ex3_impulse, ex4_step | N = 64, N = 40 | impulse at n = 0; step from 0 to 1 at n = 0 |
| ex5_fft | N = 256, f = 1 kHz, fs = 8 kHz | one line at 1000 Hz, 42.1 dB; bin spacing 31.25 Hz |
| example1.slx | external initial condition 1, stop time 10 | et, about 22026 at t = 10 s |
| example2.slx | Sine Wave 1 rad/s, initial condition −1 | −cos t, from −1 |
| Assignment 5 | cos 30 Hz, fs = 128 Hz, N = 256 | one line at 30 Hz (bin 60) |
Check yourself
Q1What is MATLAB?
MATLAB (MATrix LABoratory) is a high level language and environment for technical computing. It joins computation, plotting and programming, and every value in it is a matrix, which makes it natural for signals stored as vectors of samples.
Q2Define the unit impulse function.
δ(n) is 1 at n = 0 and 0 at every other n. In continuous time δ(t) is zero everywhere except t = 0, with an area of 1. Feeding an impulse into a system gives its impulse response.
Q3Define the unit step function.
u(n) is 0 for n < 0 and 1 for n ≥ 0. It models a sudden switch on. The impulse is the difference of two steps: δ(n) = u(n) − u(n−1).
Q4What is the Fourier transform?
A way of writing a signal as a sum (integral) of complex exponentials of every frequency. It moves a signal from the time domain to the frequency domain and shows which frequencies it contains and how strong each is.
Q5Write down the mathematical expression for the Fourier transform.
X(f) = ∫ x(t) e^(−j2πft) dt over all t, and the inverse x(t) = ∫ X(f) e^(j2πft) df. For N samples the DFT is X(k) = Σ x(n) e^(−j2πkn/N), n = 0 … N−1.
Q6Why is plot(n,x) of an impulse a triangle and not a single line?
plot joins consecutive points with straight lines, so it draws lines from the zero samples up to the 1 and back. stem(n,x) draws each sample on its own and shows the true discrete signal.
Q7A 1 kHz sine is sampled at 8 kHz with N = 256. In which FFT bin is the peak and how high is it?
Bin spacing is 8000/256 = 31.25 Hz, so 1000 Hz is bin 32 (index 33 in MATLAB). The magnitude is N/2 = 128, which is 42.1 dB.
Q8Why does the Sine Wave block of Simulink need 2*pi*50 for a 50 Hz wave?
Its frequency field is in radians per second. One hertz is 2π rad/s, so 50 Hz is 2π × 50 ≈ 314.16 rad/s.