EE-410L Communication Systems Lab · Lab 1 · read before the labAll labsGo to the lab →

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.

01

MATLAB and Simulink

MATLAB

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.

Simulink

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.

Script

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.
02

Vectors and the colon operator

Colonn = 0:N-10, 1, 2, … N−1 (N values)
With a stept = 0:0.1:40401 values from 0 to 40
Element by elementx = exp(-0.1*t) .* sin(0.6*t).* multiplies sample by sample

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.

Semicolons. A line that ends with ; runs quietly. Without it MATLAB prints the result in the Command Window, which for a vector of 256 samples fills the screen. The % sign starts a comment.
03

Sampling a sine

Sampled sinex(n) = A sin(2π (f/fs) n + θ)n is the sample number
Samples per cyclefs / f8000 / 1000 = 8 in Exercise 1
Cycles in the recordf N / fs1000 × 64 / 8000 = 8

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.

04

The basic signals

Unit impulseδ(n) = 1 at n = 0, else 0the sample that reveals a system
Unit stepu(n) = 1 for n ≥ 0, else 0a sudden switch on
Link between themδ(n) = u(n) − u(n−1)assignment 4(a)

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.

05

Spectrum by the FFT

DFTX(k) = Σ x(n) e−j2πkn/Nk = 0 … N−1, computed by fft(x)
Frequency of bin kfk = k fs / Nbin spacing fs/N = 31.25 Hz in 1.5
Peak of a sine|X| = N/2 → 20 log(N/2) dBN = 256: 42.1 dB

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.

The Fourier transform. X(f) = ∫ x(t) e−j2πft dt writes a signal as a sum of complex exponentials. The DFT is its sampled version for a record of N samples, and the FFT is the algorithm that computes it.
06

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.

Frequencies in Simulink. The Sine Wave block takes its frequency in rad/s. For 50 Hz type 2*pi*50. The default of 1 rad/s is a period of 6.28 s. Simulink also has no impulse block: a narrow pulse of area 1 made from two steps stands in for it.
07

In the virtual lab

Scripts (.m)
Open the file→Move the sliders→Run→Read the figure→Add to report
the sliders are the boxed numbers in the code; Copy code gives the code with your values for real MATLAB
Models (.slx)
Open the model→Click a block→Type the parameters→OK→Run→Read the Scope
parameters are typed as in Simulink (2*pi*50, 1/0.01); the Stop Time box sets how long the simulation runs

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.

08

What you should see

FileSettingsResult
ex1_sineN = 64, f = 1 kHz, fs = 8 kHz8 cycles, 8 samples each
ex2_expt = 0:0.1:40, e−0.1tfalls from 1 to 0.0183 at 40 ms
ex3_impulse, ex4_stepN = 64, N = 40impulse at n = 0; step from 0 to 1 at n = 0
ex5_fftN = 256, f = 1 kHz, fs = 8 kHzone line at 1000 Hz, 42.1 dB; bin spacing 31.25 Hz
example1.slxexternal initial condition 1, stop time 10et, about 22026 at t = 10 s
example2.slxSine Wave 1 rad/s, initial condition −1−cos t, from −1
Assignment 5cos 30 Hz, fs = 128 Hz, N = 256one line at 30 Hz (bin 60)
09

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.

I am ready, go to the lab →