# Chapter 8: Speech Coding and Channel Coding¶

## Example 8.1, Page 227¶

In [1]:
import math

#Variable declaration
Pdiff=-3.;  #in dB
AMR1=12.2; #in kbps
AMR2=7.95;  #in kbps
AMR3=4.75;  #in kbps

#Calculations
#CG(dB)=10math.log{(DPDCH(kbps)+DPCCH)/(DPDCH(AMR bit rate (kbps))+ DPCCH)}
CG1=10*math.log10((AMR1+AMR1*10**(Pdiff/10))/(AMR2+AMR1*10**(Pdiff/10)));
CG2=10*math.log10((AMR1+AMR1*10**(Pdiff/10))/(AMR3+AMR1*10**(Pdiff/10)));

#Results
print 'By reducing the AMR bit rate from 12.2 to 7.95 kbps coverage gain becomes %.2f dB'%CG1;
print 'By reducing the AMR bit rate from 7.95 to 4.75 kbps coverage gain becomes %.2f dB'%CG2;

By reducing the AMR bit rate from 12.2 to 7.95 kbps coverage gain becomes 1.15 dB
By reducing the AMR bit rate from 7.95 to 4.75 kbps coverage gain becomes 2.27 dB


## Example 8.2, Page 239¶

In [2]:
import numpy as np
import sympy
from sympy import symbols

#Variable declaration
K=4; #constraint length
r=1./2; #code rate(n/k)
x=symbols('x');#Defining x as a ploynomial variable
G1=1+x**2+x**3;
G2=1+x+x**2+x**3;
ins=np.array([1, 0, 1, 1, 1]);#input(first bit first)

#Calculations&Results
#with reference to Fig 8.9 on page no 239
g1=np.array([1, 0, 1, 1]); #converting from G1 polynomial to bit form
g2=[1, 1, 1, 1];##converting from G2 polynomial to bit form
x1=np.convolve(g1,ins)
x2=np.convolve(g2,ins)
V1=x1%2;
V2=x2%2;
print "Multiplexing the V1 and V2 to get required output sequence as"
a=5;
for i in range(0,6):
print '%d%d'%(V2[a],V1[a]);
a=a-1;


Multiplexing the V1 and V2 to get required output sequence as
10
10
10
00
10
11


## Example 8.3, Page 246¶

In [3]:
import numpy as np

#Variable declaration
BitStream=np.array([0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]);#Last bit to first bit

# Interleaver
Input1=[[1, 0, 0, 0],[1,0,0,0],[1,1,1,0],[0,0,0,0]]      #Writing data row wise

#Calculations&Results
print "GIven Bit stream is",BitStream
print "Input to interleaver is",Input1

Output1=np.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1]);    # Reading data column wise
print "Output of interleaver is",Output1
#De-interleaver
Input2=[[1, 1, 1, 0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]  #Writing o/p data row wise
# Let From 6th to 9th bits have Burst Error
print "Input to de-interleaver is",Input2
#Output of deinterleaver

Output2= [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1];
print "Output of de-interleaver is",Output2
print  "Bits with Burst error were from 6th to 9th. But in output of de-interleaver, they relocated to positions 3rd, 6th, 10th and 14th.";

GIven Bit stream is [0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 1]
Input to interleaver is [[1, 0, 0, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 0, 0]]
Output of interleaver is [0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1]
Input to de-interleaver is [[1, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
Output of de-interleaver is [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]
Bits with Burst error were from 6th to 9th. But in output of de-interleaver, they relocated to positions 3rd, 6th, 10th and 14th.