{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter No. 8 : Speech coding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example no 8.1 Page no. 420"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" The mean square error distortion = 0.333\n",
"\n",
" The output signal-to-distortion ratio = 19.82 dB\n",
"\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.\n",
"\n",
" This quantizer would be optimal for an input with a uniform pdf.\n"
]
}
],
"source": [
"from sympy.mpmath import quad\n",
"from math import log10\n",
"# To compute the mean square error distortion and output signal-to-distortion ratio.\n",
"\n",
"#Given data\n",
"l1=1# # 1st Quantization level\n",
"l2=3# # 2nd Quantization level\n",
"l3=5# # 3rd Quantization level\n",
"l4=7# # 4th Quantization level\n",
"\n",
"U1=(l1+l2)/2# # upper boundary of 1st level\n",
"U2=(l2+l3)/2# # upper boundary of 2nd level\n",
"U3=(l3+l4)/2# # upper boundary of 3rd level\n",
"U4=l4+(U1-l1)# # upper boundary of 4th level\n",
"L1=l1-(U1-l1)# # Lower boundary of 1st level\n",
"\n",
"D1=quad(lambda x:(x**3-2*x**2+x)/32,[L1,U1]) # Mean square error distortion of 1st level\n",
"D2=quad(lambda x:(x**3-6*x**2+9*x)/32,[U1,U2])# # Mean square error distortion of 2nd level\n",
"D3=quad(lambda x:(x**3-10*x**2+25*x)/32,[U2,U3])# # Mean square error distortion of 3rd level\n",
"D4=quad(lambda x:(x**3-14*x**2+49*x)/32,[U3,U4])# # Mean square error distortion of 4th level\n",
"D=D1+D2+D3+D4# # Total square error distortion\n",
"\n",
"P=quad(lambda x:x**3/32,[L1,U4])# # Signal power\n",
"\n",
"SDR=10*log10(P/D)# # Output signal-to-distortion ratio.\n",
"\n",
"# Displaying the result in command window\n",
"print '\\n The mean square error distortion = %0.3f'%(D)#\n",
"print '\\n The output signal-to-distortion ratio = %0.2f dB'%(SDR)#\n",
"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.'\n",
"print '\\n This quantizer would be optimal for an input with a uniform pdf.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example no 8.2 Page no. 424"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Effective transmission bit rate = 64.5 kbps\n",
"\n",
" Peak signal to quantization noise ratio = 52.93 dB\n",
"\n",
" Average signal to quantization noise ratio = 48.16 dB\n"
]
}
],
"source": [
"# To compute transmission bit rate, average and peak signal to quantization noise ratio\n",
"\n",
"\n",
"# Given data\n",
"fs=8*10**3# # Sampling frequency in Hz\n",
"n=8# # Number of bits per sample\n",
"stepsize=10*10**-3# # Time after which step size is recomputed\n",
"overhead=5# # Number of overhead bits\n",
"\n",
"N=fs*n# # Number of information bits pe second\n",
"Toverhead=overhead/stepsize# # The number of overhead bits/second\n",
"\n",
"# Effective transmission bit rate\n",
"bitrate=N+Toverhead# # Transmission bit rate in bps\n",
"\n",
"# Peak signal to quantization noise ratio\n",
"PSQNR=6.02*n+4.77# # Peak signal to quantization noise ratio in dB\n",
"\n",
"# Average signal to quantization noise ratio\n",
"ASQNR=6.02*n# # Average signal to quantization noise ratio in dB\n",
"\n",
"# Displaying the result in command window\n",
"print '\\n Effective transmission bit rate = %0.1f kbps'%(bitrate*10**-3)#\n",
"print '\\n Peak signal to quantization noise ratio = %0.2f dB'%(PSQNR)#\n",
"print '\\n Average signal to quantization noise ratio = %0.2f dB'%(ASQNR)#"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example no 8.3 Page no. 427"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Total encoding rate = 8.3 kbps\n"
]
}
],
"source": [
"# To compute the minimum encoding rateof given 4 sub-band coder\n",
"\n",
"\n",
"# Given data\n",
"N=4# # Total number of sub-bands\n",
"L1=225# # Lower limit of first sub-band\n",
"U1=450# # Lower limit of first sub-band\n",
"L2=450# # Lower limit of second sub-band\n",
"U2=900# # Lower limit of second sub-band\n",
"L3=1000# # Lower limit of third sub-band\n",
"U3=1500# # Lower limit of third sub-band\n",
"L4=1800# # Lower limit of fourth sub-band\n",
"U4=2700# # Lower limit of fourth sub-band\n",
"E1=4# # Encoding bit of first sub-band\n",
"E2=3# # Encoding bit of second sub-band\n",
"E3=2# # Encoding bit of third sub-band\n",
"E4=1# # Encoding bit of fourth sub-band\n",
"\n",
"# Sampling rate of the sub-bands according to Nyquist theorem\n",
"sr1=2*(U1-L1)# # Sampling rate of first sub-band in samples/second\n",
"sr2=2*(U2-L2)# # Sampling rate of second sub-band in samples/second\n",
"sr3=2*(U3-L3)# # Sampling rate of third sub-band in samples/second\n",
"sr4=2*(U4-L4)# # Sampling rate of fourth sub-band in samples/second\n",
"\n",
"# Total encoding rate\n",
"SR=sr1*E1+sr2*E2+sr3*E3+sr4*E4# # Total encoding rate in bps\n",
"\n",
"# Displaying the result in command window\n",
"print '\\n Total encoding rate = %0.1f kbps'%(SR*10**-3)#"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example no 8.4 Page no. 439"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Maximum net data rate = 10.5 kbps\n"
]
}
],
"source": [
"from __future__ import division\n",
"# To find the upper bound of the transmission bit rate\n",
"\n",
"\n",
"# Given data\n",
"FL=810*10**6# # Lower limit of forward channel frequency band\n",
"FU=826*10**6# # Upper limit of forward channel frequency band\n",
"N=1150# # Number of simultaneous users#\n",
"SE=1.68# # Spectral efficiency in bps/Hz\n",
"CR=0.5# # Coder rate\n",
"bandused=90/100# # 90% bandwidth is used\n",
"\n",
"bandwidth=bandused*(FU-FL)# # Total bandwidth available for traffic channels in Hz\n",
"Cbandwidth=bandwidth/N# # Maximum channel bandwidth in Hz\n",
"ChannelDR=SE*Cbandwidth# # Maximum channel data rate in bps\n",
"DR=ChannelDR*CR# # Maximum net data rate in bps\n",
"\n",
"# Displaying the result in command window\n",
"print '\\n Maximum net data rate = %0.1f kbps'%(DR*10**-3)#"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example no 8.5 Page no. 439"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" The gross channel bit rate = 16.75 kbps\n"
]
}
],
"source": [
"from __future__ import division\n",
"# To compute the gross channel data rate\n",
"\n",
"\n",
"# Given data\n",
"t=20*10**-3# # Duration of encoding of one block in second\n",
"B1=50# # The first bits in Type-1 channel\n",
"CRC1=10# # Number of CRC bits in Type-1 channel\n",
"FEC=0.5# # FEC rate for Type-1 channel\n",
"B2=132# # Next bits in Type-2 channel\n",
"CRC2=5# # Number of CRC bits in Type-2 channel\n",
"B3=78# # The last bits in Type-3 channel\n",
"\n",
"N1=(B1+CRC1)/FEC# # Total number of bits transmitted in Type-1 channel\n",
"N2=(B2+CRC2)# # Total number of bits transmitted in Type-2 channel\n",
"N3=B3# # Total number of bits transmitted in Type-3 channel\n",
"N=N1+N2+N3# # Total number of channel bits transmitted enery t seconda\n",
"\n",
"# The gross channel data rate\n",
"BR=N/t# # The gross channel data rate in bps\n",
"\n",
"# Displaying the result in command window\n",
"print '\\n The gross channel bit rate = %0.2f kbps'%(BR*10**-3)#\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}