Ltu Ex 06284 Se PDF
Ltu Ex 06284 Se PDF
Ltu Ex 06284 Se PDF
MASTER'S THESIS
Hans Eklund
HANS EKLUND
This report covers a master thesis in signal processing. It deals with solving a
problem in a special type of audio encoder used in the Swedish speech newspaper
system. However, design methods and algorithms developed or investigated are
rather general. A large part of the report covers the fundamental theory of
phase locked loops and can be regarded as a beginners introduction to the field.
Along the way, the theory aims at a software implementation, and therefore
tries to deal with such specific matters. Also the issues with implementing a
time critical system in digital hardware is outlined and a custom method for
achieving phase-lock is presented. A successful implementation was made on a
digital signal processor based hardware platform.
Contents
1 Introduction 1
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 The audio encoder . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Results 29
3.1 Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2 Real-time hardware implementation . . . . . . . . . . . . . . . . 30
3.2.1 Hardware specification . . . . . . . . . . . . . . . . . . . . 30
3.2.2 Developing environment . . . . . . . . . . . . . . . . . . . 30
3.2.3 Measurements . . . . . . . . . . . . . . . . . . . . . . . . 32
4 Conclusion 35
A Program code 36
iii
iv
List of Figures
v
3.3 Oscilloscope dump of the SPLL input at 19 kHz and the in phase
locked half frequency output. . . . . . . . . . . . . . . . . . . . . 34
3.4 Oscilloscope dump of the FFT of the SPLL output. 10dBV per
square. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
vi
Acknowledgement
Thanks goes to the Rubico AB founders Anders and Per for having
me as their Master Thesis student and for their support in every
aspect. Thanks to James Leblanc for valuable insights and patience
in this long drama. Also, hats off for Robert Selberg for acting
as the thesis opponent. Thanks to the percolator for keeping the
coffee warm and aromatic. Finally, thanks to all the staff, current
and past, at Rubico for being around for support and laughter.
vii
viii
Chapter 1
Introduction
Engineering is all about development. This master thesis uses new technology
to improve an old system. In this case the new technology are digital signal
processors - DSP. The system in need of improvement is the Swedish speech
newspaper distribution system.
1.1 Overview
The speech newspaper system has been available since mid nineteen eighties
and makes newspapers available to the visually impaired. Newspaper staff read
and record the written articles, advertisements, radio and TV tableaus etc. The
speech newspapers are distributed by mail on a tape, or via an ordinary FM
network, taking advantage of unused channel capacity during night.
The technique used in several parts of the current system were developed
during that time. One part sits in between the recording studio and the FM
link. That part scrambles the speech in a certain way to ensure only subscribers
of the speech newspaper can listen to it. Currently, the scrambling is performed
by analog electronics that has to be tuned once a year. It has now been proposed
that the old audio encoder may be replaced by a more flexible digital system.
Using a digital method to scramble the audio is attractive in several aspects.
First and foremost, the aspect of quality. Using the old analog equipment
requires regular service and tuning of parameters. With a digital platform the
way the audio gets encoded does not change with time. Second, the new coder
platform is flexible and will be easy to upgrade for future demands.
are common in analog communications theory, see [1] for details. The effect
is that the frequency contents will be reversed if not decoded correctly, high
pitch sound will become low and vice versa, as seen in Fig. 1.3. Before sending
the modulated audio to the external FM network, a disturbing 1 KHz tone is
added to further decrease the hearability. At the heart of the scrambling al-
VSB Modulation
9.5 kHz
MPX tone 1 kHz
input PLL tone
gorithm is the VSB modulation that shifts the spectrum, as described earlier.
The audio has to be modulated upon a signal at half the frequency of a 19 KHz
tone available in the so called MPX signal. The MPX signal is the baseband
information signal in the FM system with spectral contents roughly as seen in
Fig. 1.2. How do we generate a signal, locked in phase, at exactly half the
frequency? And what method is suitable for a software implementation?
0 15 19 23 38 53 57 f in kHz
1.3 Objectives
The coding method in the system is obsolete in many ways. The coding is
performed in a way that is really sub par when it comes to security, but for
the application it works. If the system would be replaced by an entirely digital
one, the coding method would be of another kind. However since the coder has
to be backward compatible with old receivers, the new coder has to be able to
1.3 Objectives 3
20
−20
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
4
x 10
Spectrum of signal after modulation upon 9.5Khz carrier.
40
Magnitude [dB]
20
−20
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2
4
x 10
Spectrum of signal after VSB filter.
40
20
−20
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2
Frequency[Hz] x 10
4
implement the analog scrambling described above. Most of the blocks in the
coder are more or less trivial to implement, such as filters and the modulation.
As hinted above, the block that needs extra attention is the synchronization
block used to synthesize the 9.5 kHz signal before the VSB modulation scheme.
The company that wanted this thesis to be made initially had experience,
equipment and software already available for a specific platform. So the solution
had to be tailored for that particular target. Therefore, the main objective of
the thesis was to investigate the possibility of implementing the synchronization
block in a DSP for a real time application.
The specifications on what to be accomplished where quite clear though.
The audio input was to be modulated onto a carrier at exactly half the frequency
of the 19 kHz MPX-signal and at a specific phase shift. Early on a possible
solution to the problem was seen. It was concluded that a phase locked loop
might do the job, if properly designed. However, the field of phase-locked loops
4
is deep and somewhat intricate. A thorough study on the subject was made to
lay the foundation to the design.
1.4 Outline
The report is structured into two main blocks. An introduction and design
guide to phase locked loops with en emphasis on software implementation comes
first. The chapter after is shorter and provide a presentation of a working
implementation of the theory. Last of all the results are discussed and further
work are suggested.
Chapter 2
A Phase-locked loop is a device that makes one system track another. It syn-
chronizes an output signal with a reference in frequency and in phase. Here
one system is the pilot tone from the MPX-signal and the other is our own syn-
thesized signal. Both are periodic functions of time, sinusoids or square waves.
However, instead of viewing the two signals as functions of time, think of them
as phasors in the complex plane. As complex phasors, the two signals are two
vectors rotating around the plane. Fig. 2.1 demonstrates the concept. The
Im{zn}
j(w1t)
z1 = e
z2 = ej(w2t)
q1
q2
Re{zn}
Figure 2.1: Complex representation of two signals. The arrows move around since their
phase (angle) is a function of time. The speed of the rotation is the frequency
of the sinusoids.
The phase is the property of interest for the PLL designer. As an example,
assume the first vector is rotating at constant angular frequency ω1 (t) = ωc . To
make the new, generated signal follow the input signal, the new one has to adjust
6 Phase locked loops
its phase to minimize the distance to the reference, that is the phase error,
denoted as θe . Adjusting its phase is done by either increasing or decreasing its
angular frequency. When both vectors are moving about at the same rate we
say they are locked to each other. In the locked state the phase error between
the two systems are zero or constant, depending on the system design. If the
reference signal deviates from its current angular frequency and a phase error
develops, a control system acts upon the second system to make the phase error
smaller. The control system locks the phase of the output to the input, hence
the name - Phase locked loop.
Phase locked loops are used mainly in two fields of application. When
analog signals are modulating a high-frequency carrier the PLL is needed to
demodulate the received signal back to the baseband. Classical modulation
schemes include amplitude modulation (AM), frequency modulation (FM) and
phase modulation (PM). An important application was found in 1950, the color
subcarrier in television systems was recovered with the use of a PLL.
The other important application is in the field of frequency synthesis, that is
creating a signal with correct and stable frequency. The synthesis application
is commonly found in the sending part of communication systems where the
carrier is created for bandpass signaling. By synthesis we mean that a signal
from an oscillator is fed to the PLL and the output is a signal with another
frequency.
In this thesis, the application is frequency synthesis and the PLL described
below is designed for that purpose. It does not differ much from the PLL used in
receivers, the main difference is a parameter used to set a multiplication/division
ratio of the input frequency.
uin(t)
Phase Loop VCO
uout(t)
detector filter
ufb(t)
Frequency
divider
carries its information, not in the amplitude A1 , not in its frequency ω1 , but
in the phase θ1 (t). To clarify what happens in phase when the frequency is
changed in a certain way, a few examples might help. Assume that the frequency
is constant, ω0 for t < 0 and then abruptly changes frequency by ∆ω at t = 0.
That is, makes a frequency step as seen in Fig. 2.3a. After t = 0 the reference
frequency can be written as
We clearly see that the phase signal θ1 is a ramp with slope ∆ω. That reminds
us of an important relationship to always bare in mind, the angular frequency
of a signal is the first derivative of its phase with respect to time,
dθ1
ω1 = .
dt
So whatever is done in frequency, expect the phase signal to be the integral
of the frequency action. Another example, if the signal changes frequency lin-
early, that is, makes a frequency ramp, the phase of the signal will increase
quadratically, see Fig. 2.3b.
8 Phase locked loops
0 0
−1 −1
−1 0 1 −1 0 1
Frequency of signal Frequency of signal
0.02 0.2
rad/s
0.01 0.1
0 0
−1 0 1 −1 0 1
Phase of signal Phase of signal
50 200
rad
0 0
−50 −200
−1 0 1 −1 0 1
time time
Figure 2.3: A few signals applied to a PLL. Observe the relationship between frequency and
phase signals. (a) Frequency step. (b) Frequency ramp.
Considering input and the output of the PLL are phase signals, we can do the
system analysis using the transfer function H(s). Hence, we consider the system
linear with respect to phase relationships. We also assume that the LPLL is
locked and remains so in the near future. The Laplace transforms of the input
and output phase functions, θin (t) and θout (t) respectively, defines the transfer
function
Θout (s)
H(s) = . (2.4)
Θin (s)
The function H(s) is now a phase transfer function, and the model is only valid
for small changes in phase of the reference, if the phase error becomes too big
too fast, the LPLL will unlock and a non-linear process takes place. Although
that process is described by a cumbersome non-linear differential equation it
can be understood on an intuitive level.
To express H(s) we must know the transfer functions of the three building
blocks of the LPLL, as seen in Fig. 2.2. As stated earlier, a PLL is nothing but
a control system for phase signals, therefore we can rely on basic control theory
when explaining the linear model. A control system with a regulator in series
with a process has a well known transfer function when the process output is
fed back and subtracted from the system input as in Fig. 2.4.
2.3 A linear model 9
Regulator Process
Gg(s)
Feedback system
Figure 2.4: A simple control system with a regulator, process and feedback.
Let the regulator have the transfer function Gr (s) and the process in need
of regulation have the transfer function Gp (s). The system transfer function is
then described by
Gr (s)Gp (s)
H(s) = . (2.5)
1 + Gr (s)Gp (s)Gg (s)
Such a relationship is derived by any basic text on control theory, such as [3]. In
the PLL case the regulator is the loop filter and the process in need of regulation
is the VCO.
Uin Uf b
ud (t) = (cos(θin − θf b ) − cos(2ωt + θin + θf b )). (2.7)
2
10 Phase locked loops
The first term is the wanted ”dc” component. The higher order component
cos(2ωt + θin + θf b ) will be canceled by the subsequent filter, hence they can be
neglected in the linear model. The output can then be simplified to
ud (t) ≈ Kd θe (2.11)
The parameter Ko is called VCO gain, and is specific to the selected VCO. It
has to be considered when designing PLLs in hardware when the VCO is not
designed from scratch, but chosen suitably. K0 has the dimension rad s−1 V −1 .
2.3 A linear model 11
When designing the PLL in software it can be arbitrarily chosen since the filter
design will accommodate for it. More on determining constants in section 2.3.5.
However, what we need is the transfer function of the VCO. As seen in Eq. 2.12
the angular frequency of the VCO is
ωout (t) = ωc + Ko uf (t)
But we do not want to express the VCO frequency, we want the phase transfer
function. Therefore, by definition, the phase θout (t) is given by integration of
the frequency variation K0 uf (t).
Z Z
θout = K0 uf dt = K0 uf dt
The laplace transform for integration over time is 1/s. The laplace transform
for the output phase θout is then
K0
Θout (s) = Uf (s) (2.13)
s
The transfer function of the VCO is then simply
Θout (s) K0
= (2.14)
Uf (s) s
Therefore the VCO is nothing but an integrator for phase.
Static accuracy Since we can only tolerate a small phase difference or drift
between the pilot tone input and the half frequency output we have defined the
demand of static accuracy: zero remaining error when the input is changed.
It has to be asymptotically zero independently how the input is changed. The
regulator has to cope with whatever action is done at the input.
below. Also speed and stability are contradictory demands, higher speed implies
smaller stability margins and vice versa. I let the speed demand be defined as:
as fast as possible when stability demands are met. Defining the speed of the
system comes down to make the closed loop system have a suitable damping
factor.
Stability This is probably the most important, and therefore the most com-
plex part of the entire paper. As hinted above the actual behavior of the LPLL
is described by a non linear differential equation. The complete system stabil-
ity and how the filter affects it in all situations is not theoretically derived or
predicted by this thesis due to its high complexity. Instead testing and simula-
tions of the system is made to ensure that the system is stable even when in its
non-linear mode of operation. The linear part of the stability problem however,
can be analyzed using the properties of linear systems using the model derived
in this chapter. Stability issues is discussed further below, once the filter type
is selected.
Selecting a filter
The selection of the correct loop filter implies the selection of the type and
order of the filter so the closed loop system type and order can accommodate
the above demands. Type refers to the number of poles in the transfer function,
the order is the same as the highest degree of the characteristic equation. Best
[2] mentions three basic filter types common in PLL applications, seen in Fig.
2.5. Clearly they are all low pass filters with different cutoff frequencies. The
first is called passive lag filter. Its transfer function F(s) is given by
1 + sτ1
F1 (s) = (2.18)
1 + s(τ1 + τ2 )
The second type has a similar transfer function, but has an additional gain term
Ka , given by
1 + sτ2
F2 (s) = Ka (2.19)
1 + sτ1
But how do we motivate the selection of one and not the other? The criterion
of static accuracy is zero phase error. A filter that fulfills that criterion will be
a strong candidate. The phase is defined as Θe = Θin − Θf b , seen in Fig. 2.6.
14 Phase locked loops
−20
−30
−3 −2 −1 0 1 2
10 10 10 10 10 10
Frequency (rad/sec)
20
Magnitude (dB)
10
−10
−3 −2 −1 0 1 2
10 10 10 10 10 10
Frequency (rad/sec)
40
30
Magnitude (dB)
20
10
−10
−3 −2 −1 0 1 2
10 10 10 10 10 10
Frequency (rad/sec)
Figure 2.5: Bode diagram of the filter types mentioned. (top) Passive lag filter. (mid) Active
lag filter with Ka = 10. (bottom) PI-filter, observe the high gain near DC.
Qin(s) + Qe(s)
Kd
Ud(s)
F(s)
Uf(s) _K 0
Qout(s)
s
-
Qfb(s)
Kn
Feedback gain
Assume that g is causal and that G = L{g} is rational. If all poles to sG(s)
has negative real part, then
lim g(t) = lim sG(s)
t→+∞ s→+0
It gives us the opportunity to find the error as time goes to infinity without
having to transform Θe (s) back to the time domain.
Tracking frequency means that the frequency of the feedback signal uf b (t)
has to be adjusted correctly by the VCO if a new frequency suddenly appears
in the reference uin (t). That is, the system senses a frequency step. Now recall
that a frequency step, as in Fig. 2.3, really is a phase ramp for our linear system.
Therefore we know what our system has to deal with to follow specifications.
The laplace transform of the a phase ramp input is
Cv
Ri (s) = ,
s2
as given by any laplace transform table, or derived by any text on linear systems
such as [5]. Cv is the frequency difference in radians per second at the phase
detector. If a the phase ramp is applied to our error transfer function as in Eq.
2.22, we have the Laplace transform of the error as
Cv 1 Cv
Θe (s) = Θin (s)E(s) = 2 K
= 2 (2.23)
s 1 + Kd F (s) s Kn
0 s + Kd K0 F (s)Kn s
Now following the final value theorem we get
Cv
lim θe (t) = lim sΘe (s) = lim s (2.24)
t→+∞ s→+0 s→+0 s2 + Kd K0 F (s)Kn s
The final expression is then
Cv
lim θe (t) = lim (2.25)
t→+∞ s→+0 s + Kd K0 F (s)Kn
Any filter transfer function F (s) can now be inserted in Eq. 2.25 to obtain the
final error when the system is subject to a phase ramp. A generalized filter
transfer function can be expressed as
N (s)
F (s) = . (2.26)
D(s)sn
16 Phase locked loops
Where N (s) and D(s) are the nominator and denominator polynomials respec-
tively. in the laplace domain, sn are poles at s = 0. Inserting Eq. 2.26 into Eq.
2.25 gives us
Cv Cv D(s)sn
θe (∞) = lim = lim (2.27)
s→+0 N (s)
s + Kd K0 D(s)s s→+0 sn+1 D(s) + Kd K0 Kd N (s)
n Kn
Inspection of Eq. 2.27 tells us that if n ≥ 1 the limit will approach zero as time
goes to infinity. We can therefore state that if we want our LPLL to track the
reference phase with zero phase error, a filter pole in s = 0 is needed.
For example,the first filter suggested by Best [2]. Inserting Eq. 2.18 into
2.25 gives us
Cv Cv
lim θe (t) = lim 1+sτ1 = , (2.28)
t→+∞ s→+0 s+ Kd K0 1+s(τ Kn Kd K0 Kn
1 +τ2 )
1+sτ1
by observing that 1+s(τ 1 +τ2 )
→ 1 as s → 0. That filter does not reduce the
error to zero, however it may very well do if the loop gain(Kd K0 Kn ) is kept
high enough. The second filter, F2 has a similar transfer function and yields a
similar final error, reduced by the amplification factor Ka . The third suggested
filter, the ”PI filter” of Eq. 2.20 has a pole in s = 0 and should be a candidate
capable of reducing final error to zero. The error is calculated as
Cv
lim θe (t) = lim =0 (2.29)
t→+∞ s→+0 s + Kd K0 1+sτ
sτ1 Kn
2
At this point, we have covered the four main blocks of the linear model
of the LPLL. By closing the loop and seeing it as a feedback control system,
the final analysis can be made. The filter and amplification constants can be
selected to give the system its desired overall characteristics.
by remembering the general transfer function for control theory as in Eq. 2.5.
When analyzing the closed loop it is convenient to put the transfer function on
a special form, the so called normalized form, by making the dominator be
D = s2 + 2ζωn s + ωn2
where ωn is the natural frequency and ζ is the damping factor. Simplifying Eq.
2.30 further,
Kd K0 (1 + τ2 s) Kd K0 (1+τ
τ1
2 s)
H(s) = = Kd Kn K0 τ2 Kd Kn K0
(2.31)
τ1 s + Kd Kn K0 (1 + τ2 s) s2 + τ1 s + τ1
s
K0 Kd Kn ωn τ2
ωn = ζ= (2.32)
τ1 2
1 2
Kn (2ωn ζs + ωn )
H(s) = (2.33)
s2 + 2ωn ζs + ωn2
Also, the phase error transfer function, Eq. 2.21 can be rewritten in terms of
the defined damping factor and natural frequency as
1 s2
E(s) = 1 − H(s) = (2.34)
Kn s + 2ωn ζs + ωn2
2
From this point, using Eq. 2.33 it is easy to investigate the transient response
of the PLL as we would on any control system. The parameters ωn - the natural
frequency, and ζ - the damping factors, are key design parameters. Once they
are determined, the filter parameters τ1 and τ2 can be obtained and the PLL
design is complete. A system with a high damping factor is said to be over
damped and the response may become sluggish. If the damping factor is too
low, as in an under damped system, the system may become oscillatory. Setting
ζ = √12 is usually a good tradeoff between speed and stability. We see how the
damping factor ζ affects the step response of the error function in Fig. 2.7.
18 Phase locked loops
0.8
0.6
e
←ζ=2.00
θ
0.4 ←ζ=0.71
←ζ=0.30
0.2
←ζ=0.05
0
0 0.05 0.1 0.15 0.2
Time[sec.]
Figure 2.7: Step response of the LPLL error function for various damping factors ζ when
system is set to follow at half the frequency of the input.
20
10
|E(jω)|(dB)
−10 ←ζ=2.00
←ζ=0.71
−20
←ζ=0.30
−30
←ζ=0.05
−40
−1 0 1 2
10 10 10 10
ω/ω
n
Figure 2.8: Amplitude response of the LPLL for various damping factors, ζ, plotted against
normalized frequency ω/ωn
generated by the phase detector is nothing but noise for this application. The
2.4 Non linear properties 19
tone will affect the VCO and make it tremble around its desired frequency.
Therefore we may want to narrow down the bandwidth of the system to make
the damping high at that double frequency. This is where the noise criterion
comes into the picture.
A low loop bandwidth will reject high frequency noise fed into, or created in
the system. But how low can it be set? For this particular PLL implementation
the input signal is most likely to be stable and it does not contain any baseband
information that needs to be preserved. If we were to design an FM receiver we
would have to consider the bandwidth of the baseband signal when determining
the natural frequency.
Determining a correct natural frequency ωn implies determining some im-
portant non linear properties of the LPLL. A brief overview of non linear LPLL
behavior will be covered in the next section and if it has any implications on
how the filter parameters will be calculated.
The answers to those questions are not given by theoretical derivation of math-
ematical relations, instead experiments was conducted to ensure that the LPLL
performed well enough for the task. However the question of a suitable natural
frequency of the system still remains to be answered.
The LPLL has four important stability regions. The regions are defined as
certain deviations in frequency from the quiescent frequency of the VCO and
how fast the reference frequency changes, as seen in Fig. 2.9. The following
definitions are taken from [2], with additional comments:
20 Phase locked loops
w0
1. The hold range ∆ωH . This is the range where the PLL can statically
maintain phase tracking. That is, if the reference frequency deviates this
far from the designed quiescent frequency the LPLL will unlock and the
phase error will go to infinity. This is independently of the speed the
frequency was changed.
2. The pull-in range ∆ωP . This is the range within which an LPLL will
always become locked if unlocked. This range can be infinite if the correct
filter is used. The pull-in process is slow and will be explained below.
3. The pull-out range ∆ωP O This is the dynamic limit for stable operation
of a PLL. If tracking is lost within this range, an LPLL normally will lock
again. This process is slow if it is a pull-in process. This defines how large
a frequency step the LPLL can handle without unlocking.
4. The lock range ∆ωL . This is the frequency range within which a PLL
locks within a single-beat note between reference frequency and output
frequency. It is independent of the speed the phase was changed, just as
long as it does not exceed this range.
The definitions are not specific to his book, but are standard terms among PLL
designers. However they do not apply to the other types of PLLs mentioned,
the digital PLL and the all-digital PLL.
As a vivid example of the non-linear behavior, an actual pull-in process
simulated in Matlab is shown in Fig. 2.10. The LPLL is initially locked in
phase (π/2 rad out of phase as explained above). At t = 1 a large frequency step
(larger than the pull-out range) is applied and the LPLL loses phase tracking,
but since the reference is within the pull-in range, the LPLL will try to pull the
VCO closer to the reference.
2.4 Non linear properties 21
60
50
Phase[rad]
40
30
20
10
Figure 2.10: The pull-in process for a LPLL implemented in software. it finally settled at
21 × π = 65.97 radians out of phase.
After the pull-in process, the linear locking phase takes place and it settles
at N × π out of phase, which is a true phase lock. In the unlocked state,
the phase detector modulates the VCO in a nonharmonic way. That implies
that the output of the VCO is nonharmonic. The frequency of the VCO is
then sometimes closer to the reference, and sometimes further from it. That
is, we have a time dependent frequency difference. But the frequency of the
VCO varies in such way that is more often closer to than further from the
reference. Therefore, in each cycle, the average frequency is slightly shifted
towards the reference. This asymmetry of the VCO causes it to change the
average frequency even faster towards the reference. The VCO is pulling itself
in.
a0 = 1
a1 = −1
µ ¶
T 1
b0 = 1+
2τ1 tan(T /2τ2 )
µ ¶
T 1
b1 = 1−
2τ1 tan(T /2τ2 )
After transforming Eq. 2.37 back to the discrete time domain, the following
difference equation is the result,
The filtered output uf [n] is then used to control the total phase in the SCO. The
expression ’total phase’ will be explained below. The analog PI filter described
above and its digital version can be seen in Fig. 2.11.
Amplitude resp.
100
Analog
Magnitude [dB]
Digital
50
−50
−5 0 5
10 10 10
Frequency [Hz]
Phase resp.
50
Phase [deg]
−50
−100
−5 0 5
10 10 10
Frequency [Hz]
Figure 2.11: Analog PI filter and its digital counterpart created using the bilinear transform.
24 Phase locked loops
See [7], or any basic textbook on digital systems for details on the subject of
number representation and overflow.
Another problem when dealing with fixed point hardware is the limited pre-
cision in fractional numbers, mainly concerning filter coefficients. Rounding
off a high precision filter coefficient from a filter design tool may result in a
malfunctioning filter. This is an important subject and the depth is beyond
the scope of this report. Filter of higher order are more likely to fall into this
category and it is vital to implement the filter in a manner that reduces the
risk with low precision coefficients. The question of suitable filter structures
comes up. The direct form of IIR filters are seldom used directly in implemen-
tations, instead the filter is reordered into a sequence of second order sections.
Such an arrangement is called Cascade form. The loop filter implementation
in this thesis is of second order only and hence the filter cannot be reduced in
complexity.
Computation time
This is simply the question of available computing time versus the time needed
by the algorithm. In a system like a PLL, the entire algorithm has to be
run at each sampling instance. An accurate prediction of the execution time
needed by the algorithm is probably cumbersome to do on beforehand. Instead
implementing the algorithm in a high level language and then measuring the
cycles needed in a suitable simulation/test environment, or on a general purpose
CPU, like a workstation. If it does not meet the timing requirements, suitable
optimizations in low level assembly might be needed or as a last resort, change
the target hardware to a faster one.
A sample-by-sample based algorithm that does not finish in time is easily
detected. Since each sample arrives at a fixed rate, and will be processed by a
prioritized interrupt, the processor will have to stop the algorithm calculations
for the previous sample to serve the interrupt. Serving the interrupt most likely
implies running a new cycle of the algorithm. Therefore the algorithm will never
be run completely on any sample, also the interrupt call stack will be full and
results will be undefined at best, probably the processor will hang. At any case,
the PLL output will speak for itself.
If other tasks is to be run on the hardware at the same time, a user interface
for example, the designer may want to consider using a real-time operating
system that handle resources and help the algorithm with timing issues. The
subject of real-time systems cover this, and an in depth study is beyond the
scope of this text.
Codec delay
The hardware interfacing with the outer world feeds the processor with samples
at a fixed sample rate. This hardware is in this case called a codec and consists
26 Phase locked loops
Sampled domain
SPLL
uin(t) uin[n] uout[n]
ADC Phase Loop
SCO(f2[n])
delay detector filter
DAC Frequency
delay multiplier
fm
ADC Phase Slow MA Parameter
delay detector filter decision
Figure 2.12: An SPLL with secondary oscillator controlled by a phase adjusting subsys-
tem. The dashed line is the adjustments made at specific times decided by the
parameter decision block, and not at system sampling instances.
needed to the total phase defined by Eq. 2.39. With this method of an external
phase adjustment, any phase differences introduced, inside or outside the SPLL
is compensated for. The phase of the final output is therefore an adjusted
2.5 The Software Phase Locked Loop 27
But since φ2 , the accumulator for the SPLL, can not be touched, we keep a
separate accumulator φ3 . It will really be φ2 + Σφm since both are set to zero
at start. In plain text, this means that φ3 follow φ2 in frequency but adds a
small correcting phase shift. The adjusted phase is then fed to a sine generator
that generates the final output.
The parameter decision block is implemented as a threshold device, making
the phase corrections be
and
φm = 0, ug [m] <= |L|
where ug [m] being the filtered output of the secondary phase detector. L being
a predefined threshold. If the phase difference is driven below the threshold,
the filter before the parameter decision block is made slower to suppress jitter
and increase accuracy. A very slow moving average filter worked well for this
implementation. If the error was still not small enough the same routine was
performed again until the phase error was reduced below a certain desired
minimal phase error. Also, if the phase error increased above the threshold
the slow filter was released. The parametric low pass filter was necessary to
maintain speed while the error was large, and to maintain accuracy when the
error became smaller.
Figure 2.13: Improvement in signal quality due to a slower loop filter switched in when
system is locked in frequency.
Chapter 3
Results
This report has up to this point covered the theory of linear phase locked loops
and an additional discussion on how to implement them in software. A work-
ing SPLL was designed using the methods presented in the previous chapter.
To gain understanding in how the SPLL operates, tests and experiments were
conducted using Matlab mostly. However, as the SPLL design began to look
promising, the developing process where continued on the target hardware sim-
ulation platform.
3.1 Matlab
The Matlab implementation began with allocations of signals and result data
vectors. Main design parameters such as SCO center frequency and filter nat-
ural frequency were set. Before simulation the analog filter design were dis-
cretized using Matlabs bilinear command. Also the filter constants were
rounded off to emulate the 16-bit fixed point environment on the target hard-
ware. The piece of commented MATLAB program code below show how it was
implemented. It shows the sample-by-sample loop where the SPLL is running.
for(n=2:indEnd-1)
% Phase detector, uin - noisy input sinusoid
ud(n)=uin(n)*ufb(n);
% Estimate phase
phi2(n+1)=phi2(n)+w0*Ts+uf(n);
% Wrap phase
if(phi2(n+1)>pi)
phi2(n+1)=(phi2(n+1)-2*pi);
end
% SCO
uout(n+1)=sin(phi2(n+1));
% Feedback
ufb(n+1)=Uf*sin(Kn*phi2(n+1));
end
The signals in the program code can be seen in Fig. 3.1. The center frequency of
the SCO was set to 4 kHz in the example and the input kept a stable frequency
during the first second. After one second the input were stepped up to 4.1 kHz
and the SPLL where allowed to settle. A DC offset can be seen in the signal
uf [n] due to the offset from the center frequency of the SCO.
−1
0 5 10 15 20 25 30 35 40
phi2[n] − Wrapped phase
pi
−pi
0 5 10 15 20 25 30 35 40
ufb[n] − Feedback to phase detector
0.5
−0.5
0 5 10 15 20 25 30 35 40
uout[n] − Output at divided frequency
1
−1
0 5 10 15 20 25 30 35 40
u [n] − Phase detector output
d
0.5
−0.5
0 5 10 15 20 25 30 35 40
uf[n] − Filtered phase detector output
0.01
0.005
0
0 5 10 15 20 25 30 35 40
Figure 3.1: The signals of importance in a discrete linear PLL. It is locked to a noisy input
of 4.1 kHz.
32 Results
3.2.3 Measurements
This final section is dedicated to a discussion around a couple of figures that
shows a working SPLL on the DSP hardware described above. The SPLL is set
up to have a center SCO frequency of 10 kHz. The loop filter is a PI-filter with
natural frequency of 20 Hz. A Matlab script was made to test the SPLL with
a sequence of test signals. In the following example, it was set to generate a
sinusoidal signal at 10 kHz which was played back using a PC Audio Card. At
a certain time the frequency was abruptly stepped to 9 kHz. The signal going
into the SPLL was recorded as well as the divided SPLL output. The results of
the experiment can be seen in Fig. 3.2. This is what happened with the SPLL
during the process:
before t = 1.90 The output is phase-locked at half the input frequency. The
input is a low noise sinusoid at 10 kHz. The secondary jitter-reducing
filter is applied as a result of an earlier phase-lock process, therefore the
output is quite pure.
t=1.910 The input makes a frequency step down to 9 kHz. The SPLL senses
the frequency step, the error signal is built up. At this time the jitter-
reducing filter is removed and several disturbing overtone can be seen in
the spectrogram. A pull-in process starts right away.
t=1.920 The pull-in process is visible on the spectrogram as the SCO is in-
creasing and decreasing its frequency, however as described in the theory,
its tendency is in the right direction.
t=1.927 The pull-in process is done and the SPLL is locked in frequency.
t=2.025 The phase-locking subsystem senses that the phase error is smaller
than a certain threshold and stops the correction process. At this point
the secondary jitter-reducing filter is applied in the SPLL.
Figure 3.2: Spectrogram of input and output to the SPLL implemented on a DSP.
34 Results
Figure 3.3: Oscilloscope dump of the SPLL input at 19 kHz and the in phase locked half
frequency output.
Figure 3.4: Oscilloscope dump of the FFT of the SPLL output. 10dBV per square.
Chapter 4
Conclusion
The theory of linear PLL were studied and the most relevant parts were outlined
in this report, as well as design methods for implementing one in software for
a real-time application. A working implementation were tested in both Matlab
and in real-time. The report deals with subjects such as control theory, analog
and digital filter design, C programming and writing of a technical report. The
solution to the codec delay problem were custom made, but proved to work well
for the particular application.
The thesis was very rewarding from both a theoretical and a practical point
of view. Knowledge from a broad range of courses studied during my engineering
education was used throughout the work. Nothing was obvious to start with,
and i find the solution to be creative rather than close to optimal.
As a valuable bonus to the thesis work; i was hired by the firm Rubico and
has by the time of writing worked there for over a year.
Appendix A
Program code
The complete source code used in the thesis is not included since it is owned
by the company that offered this thesis. However, questions regarding the
implementation in general is best answered by sending the author an e-mail.
Bibliography
[1] Leon W. Couch III. Digital and analog communication systems. Prentice
hall, sixth edition, 2001.
[5] Sven Spanne. LINEARA SYSTEM. KFS AB, 3rd edition, 1995.