You are on page 1of 24

14/02/04 CS6242 Matlab 1

University of Manchester
CS4291 Mobile Computing 2006
Introduction to MATLAB

MATLAB is a high level computing language for matrix
operations & complex arithmetic.
Widely used for studying signal processing & comms.
No need to declare variables in advance.
Each variable may be array with real or complex elements.
No distinction between reals & integers
14/02/04 CS6242 Matlab 2
Examples:

A = [ 1 2.8 3.2 4
5 6 7.7 8 ] ;

X = [ 1 2 3 4] ;

Y = [ 1
2
3
4 ] ;

Z = 3 ;

W = 4 + 5.5j ; % Comment
14/02/04 CS6242 Matlab 3
Solved using MATLAB as follows:-

A = [ 2 3 4
3 -2 1
4 3 -2 ] ;
b = [ 1
2
3 ] ;
x = inv(A)*B % or X = A \ B
(
(
(

=
(
(
(

(
(
(

3
2
1
.
2 3 4
1 2 3
4 3 2
3
2
1
x
x
x
A . x = b
Simultaneous equations:

2 x
1
+ 3 x
2
+ 4 x
3
= 1
3 x
1
- 2 x
2
+ x
3
= 2
4 x
1
+ 3 x
2
- 2 x
3
= 3
14/02/04 CS6242 Matlab 4
Signals:
Stored as row or column vectors, i.e. n x 1 or 1 x n matrices.
Represent windowed signals.
To produce row vector x containing sine-wave segment:
for n = 1:100
x(n) = sin(n*pi/10)
end;
May be plotted by the following command,
plot(1:100, x);
To produce column vector x containing sine-wave segment
for n = [1:100]
x(n) = sin(n*pi/10)
end;
MATLAB seems not to mind.


14/02/04 CS6242 Matlab 5
Another (preferred) way of defining column vector x:

n=[1:100] ; % Set n as 100x1 col vector with entries 1,2,...,100
x(n) = sin(n*pi/10) % For each entry of n, calculate a value.

Whereas x = [ 1.1, 2, 3.3, 4, 5 ] defines x to be row vector,
x = [ 1.1; 2; 3.3; 4; 5 ]
defines x to be a column vector. Easier to write than:
x = [ 1.1
2
3.3
4
5 ]


14/02/04 CS6242 Matlab 6
x. is the transpose of x. (Note the .' symbols).
So, if x is col vector, x. will be a row vector.
Note dot apostrophe (.) rather than simply apostrophe ().
Omitting the dot will give complex conjugate transpose.
Index of the first element of a row or column vector is always one.
Sometimes inconvenient since a signal starts at time zero.
Frequency spectrum normally starts at frequency zero.
To avoid confusion, consider adopting following statements:
for n = 0 : 1023
x(1+n) = sin(n*pi/10)
end;

Somewhat inefficient, but preserves natural defn of n as time-index.

>

14/02/04 CS6242 Matlab 7
Run MATLAB by clicking on icon & type in statements.
If in doubt about any command, e.g. plot type:
help plot <return
More convenient to have statements in script file e.g. myprog.m.
Just a text file often referred to as an "M-file".
To execute the statements in "myprog.m" just type
"myprog"
MATLAB has a text editor.
Paths must be set to a "work" directory
where the "M-files" will be stored.
14/02/04 CS6242 Matlab 8
Signals and systems (Signal Processing) toolbox:

Set of functions for:
(i) carrying out operations on signal segments stored in vectors
(ii) evaluating the effect of these operations, and
(iii) computing parameters of digital filters etc.

Examples are:
X = fft(x) ;
If x contains N time-domain samples we get complex vector X of N
samples of frequency spectrum of x.
plot ( abs(X[1:N / 2] )
displays 'magnitude spectrum' of x from frequency zero to half the
sampling frequency.
x = ifft(X); % Inverse DFT
14/02/04 CS6242 Matlab 9
y = filter(a, b, x) ; % Pass x thro digital filter to produce y.
Coefficients in vectors a and b.
For an FIR filter, b=1.
The filters transfer function is:
a(1) + a(2)z
-1
+ a(3)z
-2
+ ...
H(z) =
1 + b(2)z
-1
+ b(3)z
-2
+ ...

freqz(a,b); % Display gain & phase responses of digital filters:-
% Note the 'zoom' function.

[H,w] = freqz(a,b; %Calculates freq response

[a,b] = butter(n, fhsc); % Calculate a & b for nth order low-pass
filter with cut-off fhsc times half the sampling freq..
14/02/04 CS6242 Matlab 10
For cut-off f
C
Hz set fhse = 2f
C
/ qifs where qifs = sampling freq.
Note that filter will produce delay as well as the filtering effect.

[a,b] = butter(n, fhse, high); % High-pass.
[a,b] = butter(n, [fhslow fhsup] ); % Band-pass.
[a,b] = butter(n, [fhslow fhsup],stop); % Band-stop

[a,b] = cheby1(n, Rp,fhsc ); % Low-pass Chebychev type 1.
[a,b] = cheby2(n, Rs,fhsc ); % Low-pass Chebychev type 1.

a = fir1(n, fcr); % Get 'a' coeffs of FIR low-pass filter order n.
% Cut-off is fcr times half sampling freq.

a = fir1(n, (flhslow, fihsup] ); % FIR band-pass filter design.
% Note that filter will delay by n/2 samples.
Many more exist.

14/02/04 CS6242 Matlab 11
Processing "quasi-continuous" signals stored in disk files.

Wish to process signals as though they were continuous analogue.
May also wish to generate our own test signals.
Such signals must be sampled to be processed by MATLAB.
Infinite sampling rate is the ideal, but we settle for a sampling rate that
is about 10 or 20 X highest bandwidth.
Signals sampled at this quasi-infinite sampling rate (call it qifs say),
could be called "quasi-continuous" signals.

Say we wish to simulate the processing of analogue signals whose
frequencies are in the range zero to 1000 Hz.



14/02/04 CS6242 Matlab 12
Lets choose 10,000 Hz for qif
S
.
To convert between time (in seconds) & its sample number n
qt
:
t = (n
qt
)/ qifs and n
qt
= t . qifs.
Now n
qt
is sample number starting at zero.
To convert this to a MATLAB array index, add one.
Assume we wish to generate 100ms or ten cycles of a sine-wave,
whose frequency is 100 Hz, and store this in a disk file.
We need to store 1000 MATLAB samples.
We must also decide on the structure of the disk-file.
14/02/04 CS6242 Matlab 13

Saving/loading arrays to/from disk files

To save an array "b" in a disk file called "bits.dat":
save bits.dat b -ascii
Omitting "-ascii" creates a more compact "non-ascii" binary file.
To read contents of ascii file "bits.dat" into an array called "bits":
load bits.dat -ascii

14/02/04 CS6242 Matlab 14
The sine-wave generator now becomes the simple m-file:

% SINEGEN.M
clear all;
qifs = 10000; % quasi-infinite sampling rate
for nqt = 0 : 9999
t = (nqt ) / qifs ; % true time of nth sample
Outdata(nqt+1) = 10000* sin( 2*pi * 100 * t ) %100 Hz sine.
end;
plot(outdata);
save outdata.dat -ascii
14/02/04 CS6242 Matlab 15
To load back the 100Hz sine wave from disk file & 'amplitude
modulate' it onto a 1000 Hz carrier:

% AMGEN.m
clear all;
qifs = 10000;
load outdata.dat indata -ascii
for nqt = 0 : 9999
t = (nqt ) / qifs % convert sample number to true time
moddata(nqt+1)=(3000 +indata(nqt+1) )*cos ( 2*pi*1000*t) ;
end;
plot (moddata);
save moddata.dat -ascii;


14/02/04 CS6242 Matlab 16
% SINGEN.m : Generate sine-wave whose frequency sweeps from a
specified lower to a specified upper frequency.
clear;
qifs = 280; % qi sampling frequency
freq_lower = 5; freq_upper = 30; % frequencies in Hz
time_span=1; % in seconds
nqt_span = round(time_span*qifs) % no. of qt samples
freq_span = freq_upper freq_lower ;
phase_inst = 0; % inst_phase in radians,
for nqt = 0 : nqt_span-1
t = (nqt)/qifs; % true time
t_inc = 1 / qifs; % time increments
freq_inst = freq_lower + freq_span * t /time_span; % freq (Hz)
phase_inst = phase_inst + freq_inst * 2 * pi *t_inc; % hard.
x(nqt+1) = sin(phase_inst);
end;
plot (x);
14/02/04 CS6242 Matlab 17
Simple "rules of thumb" when generating sine-waves etc.

To convert "time" in seconds to "sample index" multiply by qifs .
To convert sample index to time in seconds divide by qifs .
To convert a sample index to a MATLAB array index add one.
The formula to store a sine-wave of frequency f Hz in an array is
x(n+1) = cos ( 2*pi*f * (n / qifs ) ).
This stores the sample at t=0 in x(1) and so on.


To spectrally analyse a signal in array x : plot (fft(x));
There are various refinements possible.

14/02/04 CS6242 Matlab 18
Filtering signals
There are 2 types of digital filter: FIR & IIR. Transfer function:

a(1) + a(2)z
-1
+a(3)z
-2
+ ... + a(N)z
-N

H(z) =
b(1)+ b(2)z
-1
+ b(3)z
-2
+ ...+ b(M)z
-M


The vector a = [a(1), a(2), , a(N) ] & b = [b(1), b(2), , b(M)]
determine the effect of the filterl.
For FIR , b is 1, and the a vector is often long.
For IIR, a & b generally have same length, much lower than FIR.
MATLAB removes the need to worry too much about filtering.
It designs the digital filters & can then apply them to a signal easily.

14/02/04 CS6242 Matlab 19
To illustrate for IIR :

To design a 4
th
order "Butt type" IIR low-pass filter with fc = 30 Hz.

qifs = 280;
[a,b] = butter(4, 30/(qifs/2) ) ; %Lowpass Butt.

To implement it by applying it to signal in array x :

y = filter(a,b,x);

14/02/04 CS6242 Matlab 20
To illustrate for FIR:

Design & implement 128
th
order FIR band-pass digital filter with cut-off
frequencies 20 MHz & 36 MHz Apply it to signal in array x.

%To design:

qifs = 280;
a = fir1(128, [20 28] / (qifs/2) ) ;
freqz ( a , 1) ; % plot gain & phase (the b array is 1 for FIR filter)

% To implement:

y = filter(a, 1, x ); % Remember the delay of 64 samples.
14/02/04 CS6242 Matlab 21
Pseudo-random signal generation:
"rand" generates uniform PDF random numbers between 0 & 1.
Subtract 0.5 from each sample for zero mean & variance = 1/12.
To produce array of 100 samples of uniform PDF process with zero
mean & unit variance:.
x = (rand(100) - 0.5) * SQRT(12);

"randn" generates Gaussian PDF random numbers with zero mean
& unit variance. To generate 100 samples of such a process
x = randn(100);
White PSD over frequency range 0 to qifs / 2 Hz.
1-sided PSD for these samples is: 1 / ( qif s/ 2) Watts/Hz.
To produce 100 samples of AWGN (white between 0 & qifs/2) of
1-sided PSD N
0
Watts/Hz :
x = randn(100) * sqrt ( N
0
* qifs/2 );
14/02/04 CS6242 Matlab 22
Complementary error function
Q(z) is probability that a given sample of a zero mean unit variance
(o
2
=1) random variable with Gaussian PDF will be greater than z.
Q(z) is also the probability that sample < z.

If variance o
2
=1, probability of the sample being > z is Q(z/ o).
Same probability of sample being < z.
Matlab does not have Q(z) but has erfc instead.

To deduce Q(z) from the erfc function is easy:

Q(z) = 0.5 erfc (z / 1.414 );

Q(z) used for plotting bit-error probabilities against E
b
/N
0
( a
signal to noise ratio) for different digital modulation schemes.
14/02/04 CS6242 Matlab 23
1.1. With qifs=280 MHz, display 10 cycles of a 10 MHz sine-
wave with additive Gaussian noise, which is white over the
frequency range 0 to 140 MHz, such that SNR is 20 dB.
Estimate N
0
for the noise.
NB. (i) power of a sine-wave of amplitude A is A
2
/2.
(ii) power of noise with variance o
2
is o
2
.
(iii) SNR in dB is 10 log
10
(signal-power / noise power)
(iv) randn generates random samples of zero mean
unit variance Gaussian process with 1-sided PSD: N
0
= 1/(qifs/2)
Watts/Hz.

1.2. Display a frequency spectrum of the noisy sine-wave.

14/02/04 CS6242 Matlab 24
1.3 Generate a sine wave whose frequency sweeps linearly from
5 MHz to 30 MHz in 1 micro-second. Spectrally analyse this using
"fft" and save the 280 samples in a file.

1.4. With qifs=280 MHz, design a 6
th
Butterworth type low-pass
filter with cut-off frequency 10 MHz and examine its gain-response.
Apply the swept sine-wave signal produced earlier to a MATLAB
implementation of the filter and display the output obtained. Note
the delay between input and output.

1.5. With qifs = 280 MHz, design a 100 th order FIR filter band-
pass with cut-off frequencies at 10 MHz and 20 MHz. Display its
gain and phase response, and pass the swept sine wave though it.
Display the response and note the delay.

You might also like