Miscellaneous Codes

Chapter 5
Markov processes
Run length coding
Gray code
Markov Processes
Let S = {s1, …, sq} be a set of symbols. A jth-order Markov process
has probabilities p(si | si1 … sij) associated with it, the conditional
probability of seeing si after having seen si1 … sij. This is said to be
a j-memory source, and there are qj states in the Markov process.
Transition Graph
½
⅓
a
⅓
sj
Let j = 1. Think:
b
⅓
¼
¼
¼ ¼
a means “fair”
b means “rain”
c means “snow”
c
p(si | sj)
½
si
transition probability
Transition Matrix
Weather Example:
∑ outgoing edges = 1
next symbol
c
u
r
r
e
n
t
s
t
a
t
e
a
a
b
c
b
⅓ ⅓ ⅓
¼ ½ ¼
¼ ¼ ½
c
=M
p(si | sj)
i = column, j = row
5.2
Ergodic Equilibriums
Definition: A first-order Markov process M is said to be ergodic if
1.
From any state we can eventually get to any other state.
2.
The system reaches a limiting distribution.
( pa , pb , pc ) M  ( pa , pb , pc ) repeating this ... pM n  p.
Fact : lim M n  M  exists.
n 
pM   pe is called the equilibriu m solution, and satisfies pe M  pe .
3 4 4
In above example, pe   , ,  which is the overall average weather.
 11 11 11 
5.2
Predictive Coding
Assume a prediction algorithm for a binary source which given all prior
bits predicts the next.
prediction
input stream

s1 ….. sn1
en = pn  sn error
pn
What is transmitted is the error, ei. By knowing just the error, the predictor
also knows the original symbols.
source
sn

pn
predictor
en
en
channel
sn

destination
pn
predictor
must assume that both predictors are identical, and start in the same state
5.7
Accuracy: The probability of the predictor being correct is p = 1  q;
constant (over time) and independent of other prediction errors.
Let the probability of a run of exactly n 0’s, (0n1), be p(n) = pn ∙ q.
The probability of runs of any length n = 0, 1, 2, … is:


1
q
q
p q  q p  q 

 1

1 p 1 p q
n 0
n0
n
n

f ( p)



Expected length of a run   (n  1) p q  q  (n  1) p n
n
n 0


 f ( p)dp    (n  1) p dp   p
n
n 0
n 0
n 0
n 1
 1 
  c. So,
 c  p
1 p 
(1  p) p  p(1  p) 1  p  p
1
q 1
f ( p) 


. So, q  f ( p)  2 
2
2
2
(1  p)
(1  p)
(1  p)
q
q


Note: alternate method for calculating f(p), look at  p n 
 n 0 

2
5.8
Coding of Run Lengths
Send a k-digit binary number to represent a run of zeroes whose
length is between 0 and 2k  2. (small runs are in binary)
For run lengths larger than 2k  2, send 2k  1 (k ones) followed by
another k-digit binary number, etc. (large runs are in unary)
Let n = run length. Fix k = block length. Use division to get:
0 ≤ j < m = 2k  1
n=i∙m+j
(like “reading” the “matrix” with m cells and ∞ many rows)
j in binary
k
k


code

n
ik
0 1  1

1


1

1
B

1
B j ; length  (i  1)k  l n

 j
i
5.9
Expected length of run length code
Let p(n) = the probability of a run of exactly n 0’s:
0n1. The expected code length is:

 pn   l
n 0
n
But every n can be written uniquely as
i∙m + j where i ≥ 0, 0 ≤ j < m = 2k  1.
 m 1
 m 1
i 0 j 0

i 0 j 0

  p (i  m  j )  lim  j   p im j  q  (i  1)  k
m


1

p
im
j
im
 
 qk  (i  1)  p  p  qk  (i  1) p 
i 0
j 0
i 0
 q 

1
k
m
im
m
k (1  p ) (i  1) p  k (1  p )

m 2
m
(1  p )
1 p
i 0
m 1
5.9
Gray Code
Consider an analog-to-digital “flash” converter consisting of a
rotating wheel:
The maximum
error in the
scheme is ± ⅛
rotation
because …
0
0
1
0
1
1
1
1 1
0
0
0
0
1 1
1
1
0
0
0
0
0
1
imagine
“brushes”
contacting the
wheel in each of
the three circles
1
The Hamming Distance between adjacent positions is 1.
In ordinary binary, the maximum distance is 3 (the max. possible).
5.15-17
 0G0 


  
 0G n 
0
Inductively:
G (1)   
G (n  1)   2 1 
 1G2 n 1 
1
  


Computationally: Bi  bn 1 b0 bi  {0,1}  1G0 
Gi  g n 1 g 0 g i  {0,1}
Binary ↔ Gray
Encoding
g n 1  bn 1
g i  bi  bi 1 0  i  n  1
n 1
Decoding
bi   g j
j i
(keep a running total)
b3
b2
b1
b0
↓ ↘ ↓ ↘ ↓ ↘ ↓
g3
g2
g1
g0
↓
↓
↓
↓
b3 → b2 → b1 → b0
5.15-17