# Chapter No. 8 : Speech coding¶

## Example no 8.1 Page no. 420¶

In :
from sympy.mpmath import quad
from math import log10
# To compute the mean square error distortion and output signal-to-distortion ratio.

#Given data
l1=1#                                                                # 1st Quantization level
l2=3#                                                                # 2nd Quantization level
l3=5#                                                                # 3rd Quantization level
l4=7#                                                                # 4th Quantization level

U1=(l1+l2)/2#                                                        # upper boundary of 1st level
U2=(l2+l3)/2#                                                        # upper boundary of 2nd level
U3=(l3+l4)/2#                                                        # upper boundary of 3rd level
U4=l4+(U1-l1)#                                                       # upper boundary of 4th level
L1=l1-(U1-l1)#                                                       # Lower boundary of 1st level

D1=quad(lambda x:(x**3-2*x**2+x)/32,[L1,U1])                          # Mean square error distortion of 1st level
D2=quad(lambda x:(x**3-6*x**2+9*x)/32,[U1,U2])#                        # Mean square error distortion of 2nd level
D3=quad(lambda x:(x**3-10*x**2+25*x)/32,[U2,U3])#                      # Mean square error distortion of 3rd level
D4=quad(lambda x:(x**3-14*x**2+49*x)/32,[U3,U4])#                      # Mean square error distortion of 4th level
D=D1+D2+D3+D4#                                                       # Total square error distortion

P=quad(lambda x:x**3/32,[L1,U4])#                                     # Signal power

SDR=10*log10(P/D)#                                                   # Output signal-to-distortion ratio.

# Displaying the result in command window
print '\n The mean square error distortion = %0.3f'%(D)#
print '\n The output signal-to-distortion ratio = %0.2f dB'%(SDR)#
print '\n To minimize the distortion, we need to place the quantization levels closer at amplitudes close to 8 and farther at amplitudes close to zero.'
print '\n This quantizer would be optimal for an input with a uniform pdf.'

 The mean square error distortion = 0.333

The output signal-to-distortion ratio = 19.82 dB

To minimize the distortion, we need to place the quantization levels closer at amplitudes close to 8 and farther at amplitudes close to zero.

This quantizer would be optimal for an input with a uniform pdf.


## Example no 8.2 Page no. 424¶

In :
# To compute transmission bit rate, average and peak signal to quantization noise ratio

# Given data
fs=8*10**3#                                    # Sampling frequency in Hz
n=8#                                          # Number of bits per sample
stepsize=10*10**-3#                            # Time after which step size is recomputed
overhead=5#                                   # Number of overhead bits

N=fs*n#                                       # Number of information bits pe second
Toverhead=overhead/stepsize#                  # The number of overhead bits/second

# Effective transmission bit rate
bitrate=N+Toverhead#                         # Transmission bit rate in bps

# Peak signal to quantization noise ratio
PSQNR=6.02*n+4.77#                           # Peak signal to quantization noise ratio in dB

# Average signal to quantization noise ratio
ASQNR=6.02*n#                                # Average signal to quantization noise ratio in dB

# Displaying the result in command window
print '\n Effective transmission bit rate = %0.1f kbps'%(bitrate*10**-3)#
print '\n Peak signal to quantization noise ratio = %0.2f dB'%(PSQNR)#
print '\n Average signal to quantization noise ratio = %0.2f dB'%(ASQNR)#

 Effective transmission bit rate = 64.5 kbps

Peak signal to quantization noise ratio = 52.93 dB

Average signal to quantization noise ratio = 48.16 dB


## Example no 8.3 Page no. 427¶

In :
# To compute the minimum encoding rateof given 4 sub-band coder

# Given data
N=4#                                                   # Total number of sub-bands
L1=225#                                                # Lower limit of first sub-band
U1=450#                                                # Lower limit of first sub-band
L2=450#                                                # Lower limit of second sub-band
U2=900#                                                # Lower limit of second sub-band
L3=1000#                                               # Lower limit of third sub-band
U3=1500#                                               # Lower limit of third sub-band
L4=1800#                                               # Lower limit of fourth sub-band
U4=2700#                                               # Lower limit of fourth sub-band
E1=4#                                                  # Encoding bit of first sub-band
E2=3#                                                  # Encoding bit of second sub-band
E3=2#                                                  # Encoding bit of third sub-band
E4=1#                                                  # Encoding bit of fourth sub-band

# Sampling rate of the sub-bands according to Nyquist theorem
sr1=2*(U1-L1)#                                         # Sampling rate of first sub-band in samples/second
sr2=2*(U2-L2)#                                         # Sampling rate of second sub-band in samples/second
sr3=2*(U3-L3)#                                         # Sampling rate of third sub-band in samples/second
sr4=2*(U4-L4)#                                         # Sampling rate of fourth sub-band in samples/second

# Total encoding rate
SR=sr1*E1+sr2*E2+sr3*E3+sr4*E4#                        # Total encoding rate in bps

# Displaying the result in command window
print '\n Total encoding rate = %0.1f kbps'%(SR*10**-3)#

 Total encoding rate = 8.3 kbps


## Example no 8.4 Page no. 439¶

In :
from __future__ import division
# To find the upper bound of the transmission bit rate

# Given data
FL=810*10**6#                                                 # Lower limit of forward channel frequency band
FU=826*10**6#                                                 # Upper limit of forward channel frequency band
N=1150#                                                      # Number of simultaneous users#
SE=1.68#                                                     # Spectral efficiency in bps/Hz
CR=0.5#                                                      # Coder rate
bandused=90/100#                                             # 90% bandwidth is used

bandwidth=bandused*(FU-FL)#                                  # Total bandwidth available for traffic channels in Hz
Cbandwidth=bandwidth/N#                                      # Maximum channel bandwidth in Hz
ChannelDR=SE*Cbandwidth#                                     # Maximum channel data rate in bps
DR=ChannelDR*CR#                                             # Maximum net data rate in bps

# Displaying the result in command window
print '\n Maximum net data rate = %0.1f kbps'%(DR*10**-3)#

 Maximum net data rate = 10.5 kbps


## Example no 8.5 Page no. 439¶

In :
from __future__ import division
# To compute the gross channel data rate

# Given data
t=20*10**-3#                                          # Duration of encoding of one block in second
B1=50#                                               # The first bits in Type-1 channel
CRC1=10#                                             # Number of CRC bits in Type-1 channel
FEC=0.5#                                             # FEC rate for Type-1 channel
B2=132#                                              # Next bits in Type-2 channel
CRC2=5#                                              # Number of CRC bits in Type-2 channel
B3=78#                                               # The last bits in Type-3 channel

N1=(B1+CRC1)/FEC#                                    # Total number of bits transmitted in Type-1 channel
N2=(B2+CRC2)#                                        # Total number of bits transmitted in Type-2 channel
N3=B3#                                               # Total number of bits transmitted in Type-3 channel
N=N1+N2+N3#                                          # Total number of channel bits transmitted enery t seconda

# The gross channel data rate
BR=N/t#                                              # The gross channel data rate in bps

# Displaying the result in command window
print '\n The gross channel bit rate = %0.2f kbps'%(BR*10**-3)#

 The gross channel bit rate = 16.75 kbps