advancedDSP 1
advancedDSP 1
Communication
in
MATLAB
Communication ToolBox
Communication
Simulink
block-Set
Communication Toolbox
Communication -Simulink
Information
source
Transmitter
Channel
Receiver
Information
sink
3
MATLAB functions
The MATLAB functions are in
sub-directory comm
help comm
..
help amod
help qam
eyescat
randint
randbit
symerr
rate
Gaussian
Uniform
Rayleigh
Poisson
Rice
6
wgn
y1 = wgn(50,1,2)
sum(y1.^2)/length(y1)
y4 = wgn(200,1,2,60,linear)
y4 = wgn(20000,1,2,60)
sum(y4.^2)/length(y4)
y2 = wgn(50,1,2,60,'complex','linear');
or
y3 = wgn(50,1,2,60,'linear','complex');8
randsrc
The
function generates random matrices whose entries
are chosen independently from an alphabet that is specify, with a
distribution that you specify.
A special case generates bipolar matrices.
For example, to generates a 5-by-4 matrix whose entries are
independently chosen and uniformly distributed in the set {1,3,5}.
a=
randsrc
(5,4,[1,3,5])
a=
3
If we want
b = randsrc(5,4,[1,3,5; .5,.25,.25])
b=
3
50%
10
randint(5,4,[2,10])
c=
2
5 10
7 10
10
4 10
randsrc(5,4,[2])
randint(5,4,2)
ans =
ans =
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
0
0
0
0
0
1
0
0
0
1
1
0
0
1
0
1
1
1
1
0
11
randerr (5,4)
f=
f=
12
13
Example
generating an equiprobable binary 100-element column vector
using any of the commands below.
The three commands produce different numerical outputs, but use
the same distribution.
binarymatrix1 = randsrc(100,1,[0 1] ); % Possible values are 0,1.
Third argument
14
Error Rates
Comparing messages before and after transmission
for evaluating the quality of a communication system
design (or the evaluate algorithm performances).
Counting bit errors is different from counting symbol errors.
In either the bit-or symbol-counting case,
Bits
The
Symbols
The
16
17
code = encode(msg,n,k,'hamming');
codenoisy = rem(code+(rand(n*200,1)>.95),2); % Add noise.
% Decode and correct some errors.
newmsg = decode(codenoisy,n,k,'hamming');
% Compute and display symbol error rates.
[codenum,coderate] = symerr(code,codenoisy);
[msgnum,msgrate] = symerr(msg,newmsg);
disp(['Error rate in the received code: ',num2str(coderate)])
disp(['Error rate after decoding: ',num2str(msgrate)])
18
In the example above, the symbol errors and bit errors are the
same because each symbol is a bit. The commands below
illustrate the difference between symbol errors and bit errors in
other situations.
a = [1 2 3]'; b = [1 4 4]';
format rat % Display fractions instead of decimals.
[snum,srate] = symerr(a,b)
snum =
2
srate =
2/3
19
bnum is five because the second entries differ in two bits and the
third entries differ in three bits. brate is 5/9 since the total number
of bits is nine. The total number of bits is, by definition, the
number of entries in a or b times the maximum number of bits
among all entries of a and b
20
Dugma2.m
Display devices
Provides display devices that make it easier to
analyze the performance of communication system
Display devices
Eye-Pattern
diagram
Scatter
plot
Error Rate
computation
21
Eye Diagrams
An eye diagram is a tool for studying the effects of intersymbol
interference and other channel impairments in digital transmission.
To construct an eye diagram, plot the received signal against time
on a fixed-interval axis. At the end of the fixed time interval, wrap
around to the beginning of the time axis.
One way to use an eye diagram is to look for the place where the
"eye" is most widely opened, and use that point as the decision point
when sampling to recover a digital message.
22
PLOTSTRING can be any of the strings used in the PLOT function value for OFFSET is 0.
.
eye_diagram.m23
Scatter Plots
A scatter plot of a signal shows the signal's value at a given decision point. In the
best case. The decision point should be at the time when the eye of the signal's eye
diagram is the most widely open.
To produce a scatter plot from a signal,
use the scatterplot function.
SCATTERPLOT(X, N, OFFSET, PLOTSTRING)
generates a scatter plot of X. X can be a real or complex vector, or
a two-column matrix with real signal in the first column and
imaginary signal in the second column.
SCATTERPLOT(X, N) generates a scatter plot of X with decimation factor N.
Every Nth point in X is plotted, starting with the first value. The default
for N is 1.
OFFSET is the number of samples skipped at the beginning of X before
plotting. The default value for OFFSET is zero.
PLOTSTRING can be any of the strings used in the PLOT function. The default value for
PLOTSTRING is 'b.'.
24
scatterplots.m
Signal Generator
Random Signal Generator
Source Coding
commands
Error-Control Coding
Lower-Level Functions
for ECC & GF
Modulation
&
Demodulation
25
Source Coding
Part 2
26
Source Coding
Part 2
source encoding
source decoding
Mapping process
Input
Common
value
28
Partitions
A quantization partition defines several contiguous, nonoverlapping ranges of
values within the set of real numbers.
For example, if the partition separates the real number line into the four sets:
1.{x : x 0}
2.{x : 0 x 1}
3.{x : 1 x 3}
4.{x : 3 x}
2
0
3
1
4
3
29
Codebooks
A codebook tells the quantizer which common value to assign to inputs that
fall into each range of the partition. Represent a codebook as a vector whose
length is the same as the number of partition intervals. For example, the
vector
codebook = [-1, 0.5, 2, 3];
is one possible codebook for the partition [0,1,3].
3
2
0.5
-1
1
2
0
3
1
4
3
30
31
Source encoding
Partition(1)
Signal
indx
Partition(2)
+
Quantization decode
Partition(N)
quant
Codebook(indx)
+distor
(.)2
Distortion
computation
1/M
+
Memory
32
Quantizing a Signal
How to use partition and codebook parameters in
the quantiz function ?
quantiz
Produce a quantization index and a quantized output value
Syntax
index = quantiz(sig,partition);
[index,quants] = quantiz(sig,partition,codebook);
[index,quants,distor] = quantiz(sig,partition,codebook);
33
]( 0.5 ](
0
1
]( 3
3
34
Dugma5.m
sig = sin(t);
partition = [-1:.2:1];
codebook = [-1.2:.2:1];
35
Dugma6.m
partition = [0,1,3];
codebook = [-1, 0.5, 2, 3];
samp = [-2.4, -1, -.2, 0, .2, 1, 1.2, 1.9, 2, 2.9, 3, 3.5, 5];
[index,quantized] = quantiz(samp,partition,codebook);
quantized
quantized =
Columns 1 through 6
-1.0000 -1.0000 -1.0000 -1.0000 0.5000 0.5000
Columns 7 through 12
2.0000 2.0000 2.0000 2.0000 2.0000 3.0000
Column 13
3.0000
Determining Which Interval Each Input Is in
partition = [3,4,5,6,7,8,9];
index = quantiz([2 9 8],partition)
index =
0
6
5
36
lloyds
Optimize quantization parameters using the Lloyd algorithm
Syntax
[partition,codebook] = lloyds(trainingset,initcodebook);
Description
[partition,codebook] = lloyds(trainingset,initcodebook) optimizes the
scalar quantization parameters partition and codebook for the training data in the
vector trainingset. initcodebook, a vector of length at least 2, is the initial guess
of the codebook values. The output codebook is a vector of the same length as
initcodebook. The output partition is a vector whose length is one less than the
length of codebook.
37
38
Dugma 7.m
Companding a
Signal
In certain applications, such as speech processing, it is common
to use a logarithm computation, called a compressor, before
quantizing. The inverse operation of a compressor is called an
expander. The combination of a compressor and expander is
called a compander.
39
-law
compressor
Algorithm
For a given signal x, the output of the -law compressor is
V log 1
sgn(x)
y
log 1
where
V is the maximum value of the signal x,
is the -law parameter of the compander,
log is the natural logarithm
sgn is the signum function (sign in MATLAB).
40
A-law
compressor
A x
sgn(x)
1 log( A)
Ax
V 1 log
sgn(x)
1 log( A)
where
A is the A-law parameter of the compander
other elements are as in the -law case.
V
0 x
A
V
x V
A
41
compand
Source code mu-law or A-law compressor or expander
Syntax
out = compand(in,Mu,maxim);
out = compand(in,Mu,maxim,'mu/compressor');
out = compand(in,Mu,maxim,'mu/expander');
out = compand(in,A,maxim,'A/compressor');
out = compand(in,A,maxim,'A/expander');
42
compressed = compand(1:5,87.6,5,'a/compressor')
compressed =
3.5296 4.1629 4.5333 4.7961 5.0000
expanded = compand(compressed,87.6,5,'a/expander')
expanded =
1.0000 2.0000 3.0000 4.0000 5.0000
43
mu = 255;
sig = -4:.1:4;
sig = exp(sig);
V = max(sig);
% 1. Quantize using equal-length intervals and no compander.
[index,quants,distor] = quantiz(sig,0:floor(V),0:ceil(V));
% 2. use same partition and codebook, but compress
% before quantizing and expand afterwards.
compsig = compand(sig,mu,V,'mu/compressor');
[index,quants] = quantiz(compsig,0:floor(V),0:ceil(V));
newsig = compand(quants,mu,max(quants),'mu/expander');
distor2 = sum((newsig-sig).^2)/length(sig);
[distor, distor2]
44
The entire code will be under the name A -Law Compander as *.m file
Signal
e(k)
+
Quantization
source encode
quantized y(k)
+
Predictor
xpredicted(k)
45
Source encoded
index indx(k)
Quantization
source encode
quantized y(k)
+
Predictor
46
dpcmenco
Encode using differential pulse code modulation
Syntax
indx = dpcmenco(sig,codebook,partition,predictor)
Description
encode the vector sig.
47
dpcmdeco
Decode using differential pulse code modulation
Syntax
sig = dpcmdeco(indx,codebook,predictor);
[sig,quanterror] = dpcmdeco(indx,codebook,predictor);
Description
sig = dpcmdeco(indx,codebook,predictor)
implements differential pulse code demodulation to decode
the vector
indx.
The vector
quantization codebook.
Dugma8.m
partition = [-1:.1:.9];
codebook = [-1:.1:1];
t = [0:pi/50:2*pi];
x = sawtooth(3*t); % Original signal
% Quantize x using DPCM.
encodedx = dpcmenco(x,codebook,partition,predictor);
% Try to recover x from the modulated signal.
decodedx = dpcmdeco(encodedx,codebook,predictor);
plot(t,x,t,decodedx,'--')
distor = sum((x-decodedx).^2)/length(x) % Mean square error
The entire code will be under the name DPCM Encoding and
Decoding as *.m file
49
Dugma 8.m
dpcmopt
Optimize differential pulse code modulation parameters
Syntax
predictor = dpcmopt(trainingset,ord);
[predictor,codebook,partition] = dpcmopt(trainingset,ord,len);
[predictor,codebook,partition] =
dpcmopt(trainingset,ord,initcodebook);
50
Dugma9.m
compared to
Dugma8.m
t = [0:pi/50:2*pi];
x = sawtooth(3*t);
% Original signal
51
Quadrature
Amplitude
Modulation(QAM)
Amplitude
modulation (AM)
Double sideband
transmission
carrier (DSB-TC)
Phase
modulation (PM)
Double sideband
suppressed carrier
(DSB-SC)
52
Quadrature
Amplitude (QAM)
Frequency shift
keying (FSK)
Phase shift
keying (PSK)
53
Passband
Analog
mod/dem
amod
ademod
Digital
mod/dem
dmod
ddemod.
Modmap
Baseband
Analog
mod/dem
amodce
ademodce
Digital
mod/dem
Modmap
demodmap
ddemodce
dmodce
Complex envelope 54
55
x(t)
Baseband
presentation
0f
cos C t
A 1 x(t ) cos
C
t
A
Passband
presentation
56
Modulation/Demodulation.
ademod
- Analog modulation.
amodce
apkconst
ddemod
57
Modulation/Demodulation-continue
demodmap
dmod
- Digital modulation.
dmodce
modmap
qaskdeco
qaskenco
58
Analog Modulation
Techniques
59
amod
Passband modulation
60
amod
Passband modulation
61
Passband modulation
DSB-SC
m(t)
message
Amplifier
A cos c t
62
amod
Analog passband modulator
Syntax
y = amod(x,Fc,Fs,'amdsb-sc');
y = amod(x,Fc,Fs,'amdsb-tc',offset);
y = amod(x,Fc,Fs,'amssb/opt');
y = amod(x,Fc,Fs,'amssb/opt',num,den);
= amod(x,Fc,Fs,'amssb/opt',hilbertflag);
=y amod(x,Fc,Fs,'amdsb-tc',offset);
y = amod(x,Fc,Fs,'qam');
y = amod(x,Fc,Fs,'fm',deviation);
Offset -min(min(x))
y = amod(x,Fc,Fs,'pm',deviation);
y = amod(x,Fc,[Fs phase],...);
[y,t] = amod(...);
63
Ademod
The function ademod performs analog passband demodulation.
Syntax
z = ademod(y,Fc,Fs,'amdsb-tc',offset,num,den);
z = ademod(y,Fc,Fs,'amdsb-tc/costas',offset,num,den);
z = ademod(y,Fc,Fs,'amdsb-sc',num,den);
z = ademod(y,Fc,Fs,'amdsb-sc/costas',num,den);
z = ademod(y,Fc,Fs,'amssb',num,den);
z = ademod(y,Fc,Fs,'qam',num,den);
z = ademod(y,Fc,Fs,'fm',num,den,vcoconst);
z = ademod(y,Fc,Fs,'pm',num,den,vcoconst);
z = ademod(y,Fc,[Fs phase],...);
64
Passband modulation
DSB-SC
y t A m t cos(2 f c t ) noise( t )
^
m t
message
2 A sin( 2 f c t c )
Local Oscillator
z = ademod(y,Fc,Fs,'amdsb-sc',num,den);
65
amod
Analog passband modulator
Syntax
y = amod(x,Fc,Fs,'amdsb-sc');
y = amod(x,Fc,Fs,'amdsb-tc',offset);
y = amod(x,Fc,Fs,'amssb/opt');
y = amod(x,Fc,Fs,'amssb/opt',num,den);
y = amod(x,Fc,Fs,'amssb/opt',hilbertflag);
y = amod(x,Fc,Fs,'qam');
y = amod(x,Fc,Fs,'fm',deviation);
y = amod(x,Fc,Fs,'pm',deviation);
y = amod(x,Fc,[Fs phase],...);
[y,t] = amod(...);
66
Passband modulation
AM-SSB
x(t)
SIDE
SIDEBAND
BAND
FILTER
FILTER
cos c1 t
x(t ) cos c t
C
DSB
cos c 2 t
H f
SSB
USB
LSB
LSB
f
c
USB
67
f t f t F 1 Sgn f t 1
*
* t
Hilbert Filter
j , 0
H jSgn
j , 0
f t
H 1 e
S t
SSB
68
L.O
90
Hilebrt
filter
SSB
(t )
sin c t
s (t ) s(t ) sin c t
69
Ademod
The function ademod performs analog passband demodulation.
Syntax
z = ademod(y,Fc,Fs,'amdsb-tc',offset,num,den);
z = ademod(y,Fc,Fs,'amdsb-tc/costas',offset,num,den);
z = ademod(y,Fc,Fs,'amdsb-sc',num,den);
z = ademod(y,Fc,Fs,'amdsb-sc/costas',num,den);
z = ademod(y,Fc,Fs,'amssb',num,den);
z = ademod(y,Fc,Fs,'qam',num,den);
z = ademod(y,Fc,Fs,'fm',num,den,vcoconst);
z = ademod(y,Fc,Fs,'pm',num,den,vcoconst);
z = ademod(y,Fc,[Fs phase],...);
70
Quadrature Amplitude
Modulation
QAM
m I (t)
cos(2f c t c )
900
mQ (t)
y( t )
y( t ) m I ( t ) cos(2f c t c ) m Q ( t ) sin( 2f c t c )
y = amod(x,Fc,Fs,'qam');
71
Quadrature Amplitude
DeModulation
QAM
y( t )
LPF
m I (t)
LPF
mQ (t)
cos(2f c t c )
900
z = ademod(y,Fc,Fs,'qam',num,den);
72
73
Mathematical expressions
FM,PM
y = amod(x,Fc,Fs,'pm',deviation);
cos c t 2 k c t c
y t
cos
t k c m d
PM
FM
y = amod(x,Fc,Fs,'fm',deviation);
74
Ademod
FM,PM
75
Phase
detector
LPF
VCO
z = ademod(y,Fc,Fs,'fm',num,den,vcoconst)
implements frequency demodulation.
The spectrum of the demodulated signal is between min(y) + Fc
and max(y) + Fc. The demodulation process uses a phase-locked loop
composed of a multiplier (as a phase detector), a lowpass filter, and a
voltage-controlled oscillator (VCO).
If Fs is a two-element vector, then its second
element is the initial phase of the VCO, in radians.
The optional argument vcoconst is a scalar that represents the
VCO constant in Hz/V.
76
Phase
detector
LPF
1/s
VCO
z = ademod(y,Fc,Fs,'pm',num,den,vcoconst)
Implements phase demodulation. The demodulation
process uses a phase-locked loop (which acts as an
FM demodulator) cascaded with an integrator. The phase-locked
loop consists of a multiplier (as a phase detector), a lowpass
filter, and a
voltage-controlled oscillator (VCO).
If Fs is a two-element vector, then its second element is the
initial phase of the VCO, in radians.
The optional argument vcoconst is a scalar that represents the
77
input signal's sensitivity.
Example
Double- and Single-Sideband Comparison Example
% Sample the signal 100 times per second, for 2 seconds.
Fs = 100;
t = [0:2*Fs+1]'/Fs;
Fc = 10; % Carrier frequency
x = sin(2*pi*t); % Sinusoidal signal
% Modulate x using single- and double-sideband AM.
ydouble = amod(x,Fc,Fs,'amdsb-sc');
ysingle = amod(x,Fc,Fs,'amssb');
% Plot spectra of both modulated signals.
zdouble = fft(ydouble);
zdouble = abs(zdouble(1:length(zdouble)/2+1));
frqdouble = [0:length(zdouble)-1]*Fs/length(zdouble)/2;
plot(frqdouble,zdouble); % The plot on the left-hand side below
figure;
zsingle = fft(ysingle);
zsingle = abs(zsingle(1:length(zsingle)/2+1));
frqsingle = [0:length(zsingle)-1]*Fs/length(zsingle)/2;
plot(frqsingle,zsingle); % The plot on the right-hand side below
78
Example
Hilbert Filter Example
The next example uses a Hilbert filter in the time domain.
Fc = 25; % Carrier signal frequency
Fs = 100; % Sampling rate of signal
[numh,denh] = hilbiir(1/Fs,15/Fs,15); % Design Hilbert filter.
t = [0:1/Fs:5]'; % Times to sample the signal
x = cos(t); % Signal is a cosine wave.
y = amod(x,Fc,[Fs pi/4],'amssb',numh,denh); % Modulate,
% using a Hilbert filter in the time domain.
z = ademod(y,Fc,[Fs pi/4],'amssb'); % Demodulate.
plot(t,z) % Plot recovered signal.
%The resulting plot is on the left below. If you replace the sixth line above with
y = amod(x,Fc,[Fs pi/4],'amssb'); % Modulate,
hold on
plot(t,x,,r);
79
ademod uses a lowpass filter with sample time 1/Fs while demodulating,
in order to filter out the carrier signal.
To specify the lowpass filter, include num and den in the
list of input arguments. num and den are row vectors that
give the coefficients, in descending order, of the numerator
and denominator of the filter's transfer function.
Example:
butter is in the Signal Processing Toolbox.
[num,den] = butter(5,Fc*2/Fs);
80
z = ademod(y,Fc,Fs,'amdsb-tc',offset,num,den)
implements double-sideband amplitude demodulation.
offset is a vector whose kth entry is subtracted from the
kth signal after the demodulation.
z = ademod(y,Fc,Fs,'amdsb-tc/costas',offset,num,den) is the same as
the syntax above, except that the algorithm includes a Costas phase-locked loop.
z = ademod(y,Fc,Fs,'amdsb-sc',num,den)
implements double-sideband suppressed-carrier amplitude demodulation.
z = ademod(y,Fc,Fs,'amdsb-sc/costas',num,den) is the same as the syntax
above, except that the algorithm includes a Costas phase-locked loop.
z = ademod(y,Fc,Fs,'amssb',num,den) implements single-sideband
suppressed-carrier amplitude demodulation.
81
example
This example illustrates the use of the offset argument. Since the first ademod
command uses the same offset value of .3 that the amod command used, z1 is
similar to the original message signal. Since the second ademod command omits
offset, z2 has mean close to zero (not exactly zero because of roundoff error).
Fc = 25; % Carrier signal frequency
Fs = 100; % Sampling rate of signal
t = [0:1/Fs:5]'; % Times to sample the signals
x = [cos(t), sin(t)]; % Cosine signal and sine signal
y = amod(x,Fc,Fs,'amdsb-tc',.3); % Modulate
% and shift the values up by .3.
z1 = ademod(y,Fc,Fs,'amdsb-tc',.3); % Demodulate.
z2 = ademod(y,Fc,Fs,'amdsb-tc'); % Demodulate.
plot(t,z1,'b',t,z2,'r--') % Plot recovered signal.
82
example
Example: Varying the Filter's Cutoff Frequency
The code below modulates a sawtooth message signal, demodulates the resulting
signal using a Butterworth filter, and plots the original and recovered signals. Note
that the scaling in the butter function causes the cutoff frequency of the filter to be
F*Fs/2, not F itself.
83
Passband
Analog
mod/dem
Digital
mod/dem
Baseband
Analog
mod/dem
Digital
mod/dem
84
baseband modulation
Modulation
Method
x I (t)
DSB-SC
u(t)
SSB-SC
u(t)
DSB-TC
u(t) k
QAM
FM
PM
u I (t)
(t)
u(t)
u Q (t)
cos k vco u ( )
sin k vco u ( )
85
amodce
Analog baseband modulator with out carrier
Syntax
y = amodce(x,Fs,'amdsb-tc',offset);
y = amodce(x,Fs,'amdsb-sc');
y = amodce(x,Fs,'amssb');
y = amodce(x,Fs,'amssb/time',num,den);
y = amodce(x,Fs,'amssb/time');
y = amodce(x,Fs,'qam');
y = amodce(x,Fs,'fm',deviation);
y = amodce(x,Fs,'pm',deviation);
y = amodce(x,[Fs phase],...);
86
87