# Chapter 8 - Waveform coding techniques¶

## Example 2 - pg 386¶

In :
#calculate the transmission bandwidth and final bit rate
import math
from math import log10
#given
f_m = 4.2*10**6#bandwidth of television signal
q = 512. #quantization levels

#calculations
#number of bits and quantization levels are related in binary PCM as q = 2^v
#where v is code word length
v = (log10(q)/log10(2));#code word length
BW = v*f_m#transmission channel bandwidth which is greater than or equal to obtained value
f_s = 2*f_m#sampling frequency which is greater than or equal to obtained value
r = v*f_s#signaling rate of final bit rate
SbyN_dB = 4.8 + 6*v#output signal to noise ratio which is less than or equal to obtained value

#results
print "i.  Code word length (bits) = ",v
print "ii. Transmission bandwidth (MHz) = ",round(BW/10**6,1)
print "iii.Final bit rate (bits/sec) = ",r
print "iv.Output signal to quantization noise ratio (dB) = ",SbyN_dB
print "Note:There is misprint in the question i.e TV signal bandwidth "

i.  Code word length (bits) =  9.0
ii. Transmission bandwidth (MHz) =  37.8
iii.Final bit rate (bits/sec) =  75600000.0
iv.Output signal to quantization noise ratio (dB) =  58.8
Note:There is misprint in the question i.e TV signal bandwidth


## Example 3 - pg 387¶

In :
#calculate the number of bits required, bandwidth required and signalling rate
import math
#given
f_m = 4.*10**3#maximum frequency or bans
x_max = 3.8#maximun input signal
P = 30.*10**-3#average power of signal
SbyN_dB= 20.#signal to noise ratio in db

#calculations
SbyN = math.exp((SbyN_dB/10)*math.log(10));
v = round((math.log10((SbyN*(x_max)**2)/(3*P))/math.log10(2.)/2.));#number of bits required per sample
BW = 30*v*f_m#transmission channel bandwidth which is greater than or equal to obtained value
r=BW*2#wkt signalling rate is two times the transmission bandwidth

#resulta
print "i.Number of bits required (bits) = ",round(v,2)
print "ii.Bandwidth required for 30 PCM coders (kHz) = ",round(BW/1000.,0)
print "iii.Signalling rate (bitspersecond) = ",round(r/1000.,0)

i.Number of bits required (bits) =  7.0
ii.Bandwidth required for 30 PCM coders (kHz) =  840.0
iii.Signalling rate (bitspersecond) =  1680.0


## Example 4 - pg 388¶

In :
#calculate the minumum sampling rate and minumum bit rate required
import math
#given
e_max = .001#maximum quantization error
x_max = 10.#maximum amplitude
x_min = -10.#minumum amplitude
f_m = 100.#bandwidth of ;input signal

#calculations
delta = 2*e_max#step size
q = (2*x_max)/delta#quantization levels
f_s = 2*f_m#sampling frequency
v = math.ceil(math.log10(q) /math.log10(2));#number of bits in the PCM word
r = v * f_s#bit rate required in the PCM signal which is greater than or equal to obtained value
BW = .5*r#transmission channel bandwidth which is greater than or equal to obtained value

#results
print "i.Minimum sampling rate required (Hz) = ",f_s
print "ii.Number of bits in each PCM word (bits) = ",round(v,2)
print "iii.Minimum bit rate required in the PCM signal (bits/sec) = ",round(r,0)
print "iv.Transmission bandwidth (Hz) = ",round(BW,0)

i.Minimum sampling rate required (Hz) =  200.0
ii.Number of bits in each PCM word (bits) =  14.0
iii.Minimum bit rate required in the PCM signal (bits/sec) =  2800.0
iv.Transmission bandwidth (Hz) =  1400.0


## Example 5 - pg 389¶

In :
#calculate the transmission bandwidth and sampling frequency
#given
f_m = 3.4*10**3#maximum frequency in the signal
N =24.#number of voice signals
r = 1.5*10**6#signaling rate
v  = 8.#bits  of encoder

#calculations
BW = N * f_m#transmission bandwidth
r_1 = r/N#bit rate for one channel
f_s = r_1/v#sampling frquency

#results
print "i.Transmission bandwidth (kHz) = ",BW/1000.
print "ii.Sampling frequency (Hz or samples per second) = ",f_s

i.Transmission bandwidth (kHz) =  81.6
ii.Sampling frequency (Hz or samples per second) =  7812.5


## Example 6 - pg 389¶

In :
#calculate the maximum message bandwidth and signal to noise ratio

#given
v = 7.#bits  of encoder
r = 50.*10**6#bit rate of the system

#calculations
f_m = r/(2*v)#maximum message bandwidth which is less than or equal to obtained value
SbyN_dB = 1.8 + 6*v#signal to noise ratio in dB

#results
print "i.Maximum message bandwidth (Hz) = ",round(f_m/10**6,2)
print "ii.Signal to noise ratio when modulating frquency is 1MHz applied (dB) = ",SbyN_dB

i.Maximum message bandwidth (Hz) =  3.57
ii.Signal to noise ratio when modulating frquency is 1MHz applied (dB) =  43.8


## Example 7 - pg 390¶

In :
#calculate the Minimum sampling rate and bit transmission rate
import math
from math import log
#given
f_m = 3*10**3#maximum frequency
M = 16.#number of quantization levels
q = M #number of quantization levels

#calculations
v = log(q)/log(2);#number of bits
f_s = 2*f_m#sampling frequency or rate which is greater than or equal to obtained value
r = v*f_s#bit transmission rate which is greater than or equal to obtained value

#results
print "i.Number of bits in a codeword (bits) = ",v
print "ii.Minimum sampling rate (kHz) =  ",f_s/1000.
print "iii.Bit transmission rate (bits/sec) = ",r

i.Number of bits in a codeword (bits) =  4.0
ii.Minimum sampling rate (kHz) =   6.0
iii.Bit transmission rate (bits/sec) =  24000.0


## Example 8 - pg 391¶

In :
#calculate the signal to noise ratio
import math
from math import log10
#given
f_m = 3.5*10**3#maximum frequency
r = 50*10**3#bit rate
v_rms = .2#rms value of input signal
R = 1#resistance
x_max = 2#maximum peak voltage

#calculations
f_s = 2*f_m;#sampling frequency
v = math.ceil(r/f_s);#number of bits
P = v_rms**2 / R#Normalized signal power
SbyN = ((3*P) * 2**(2*v)) /(x_max**2);#signal to noise ratio
SbyN_dB = 10*log10(SbyN)#signal to noise ratio in dB

#results
print "i.Signal to noise ratio in dB = ",round(SbyN_dB,0)

i.Signal to noise ratio in dB =  33.0


## Example 10 - pg 392¶

In :
#calculate the signal to noise ratio and number of bits needed
import math
#given
#x(t) = 3*cos(500*%pi*t)
v = 10.#number of bits
A_m = 3.#peak voltage
SbyN_2 = 40.#signal to noise to noise ratio in second condition

#calculations
SbyN = 1.8 +6*v#signal to noise ratio in dB
v_2 = (40 - 1.8)/6#number of bits needed for SbyN = 40

#results
print "i.Signal to noise to ratio in dB ",SbyN
print "ii.Number of bits needed for noise ratio 40 (bits) = ",math.ceil(v_2)

i.Signal to noise to ratio in dB  61.8
ii.Number of bits needed for noise ratio 40 (bits) =  7.0


## Example 11 - pg 393¶

In :
#calculate the maximum frequency

#given
v = 7.#number of bits
r = 56*10**3#signaling rate

#calculations
SbyN = 1.8 +6*v#signal to noise ratio in dB
f_s = r/v#sampling frequency
f_m = f_s/2#maximum frequency which is less than or equal to obtained value

#results
print "Maximum frequency (Hz) = ",f_m

Maximum frequency (Hz) =  4000.0


## Example 13 - pg 404¶

In :
#calculate the maximum amplitude
import math
#given
f_m = 3.*10**3#bandwidth or maximum frequency
n = 5.#system operation times
delta = 250.*10**-3#step size in volts
f_m1 = 2.*10**3#given maximum frequency to calculate amplitude

#calculations
NR = 2 * f_m#nyquist rate
f_s = n * NR#sampling frequency
T_s = 1/f_s#sampling interval
A_m =(delta/(2 * math.pi * f_m1* T_s))#Maximum amplitude

#result
print "Maximum amplitude for 2KHz input sinusoid (V) = ",round(A_m,1)

Maximum amplitude for 2KHz input sinusoid (V) =  0.6


## Example 14 - pg 406¶

In :
#calculate the signalling rate
import math
from math import log
#given
f_s = 8*10**3#sampling rate
q = 64.#quantization levels
delta = 31.25#step size

#calculations
v = log(q)/log(2);#no fo bits in the PC
f_s= (2*math.pi*3*10**3)/delta#signalling rate which should be greater than the obtaining value

#results
print "Signalling rate (kHz) = ",round(f_s,2)

Signalling rate (kHz) =  603.19


## Example 15 - pg 407¶

In :
#calculate the signal to noise ratio
import math
#given
f_m = 2.*10**3#maximum frequency
f_s = 64.*10**3#sampling frequency
f_M = 4.*10**3#cut off frequency of low pass filter

#calculation
SNR_0 = (3 * f_s**3) /(8 * math.pi**2 * f_m**2 * f_M);#signal to noise ratio of linear delta modulation system
SNR_dB = 10*math.log10(SNR_0);#SNR in dB

#result
print "Signal to noise ratio of linear delta modulation system (dB) = ",round(SNR_dB,2)

Signal to noise ratio of linear delta modulation system (dB) =  27.94


## Example 16 - pg 407¶

In :
#calculate the signal to noise ratio

#given
r = 64.*10**3#data rate
f_s = 8.*10**3#sampling frequency
N = 8.#number of samples

#calcualtion
SNR_q = 1.8 + 6*N#signal to noise ratio

#result
print "Signla to noise ratio (dB) = ",SNR_q
print "The SNR of a DM system is 27.94dB which is too poor as \ncompared to 49.8db of an 8 bit PCM system. Thus, for all\n the simplicity of Dm,it cannot perform as well as an\n 8 bit PCM"

Signla to noise ratio (dB) =  49.8
The SNR of a DM system is 27.94dB which is too poor as
compared to 49.8db of an 8 bit PCM system. Thus, for all
the simplicity of Dm,it cannot perform as well as an
8 bit PCM


## Example 17 - pg 413¶

In :
#calculate the sampling frequency and quantizing level
import math
#given
r = 36000.#bit rate of a channel
f_m = 3.2*10**3#maximum frequency

#calculations
f_s = 2*f_m#sampling frequency
v  = math.floor(r/f_s)#number of binary digits
q = 2**v#quantizing level
f_s2=r/v
#results
print "i.Sampling frequency (Hz) = ",f_s
print "ii.Number of binary digits = ",round(v,1)
print "iii.Quantizing level = ",round(q,0)
print "iv. Sampling rate (Hz) = ",f_s2

i.Sampling frequency (Hz) =  6400.0
ii.Number of binary digits =  5.0
iii.Quantizing level =  32.0
iv. Sampling rate (Hz) =  7200.0


## Example 20 - pg 415¶

In :
#calculate the number of required levels and output signal to quantizing noise ratio
import math
from math import log, exp, sqrt
#given
SbyN_0dB = 40.#signal to noise ratio in dB
SbyN_0 = exp((SbyN_0dB/10)*log(10))#signal to noise ratio
q = sqrt((2. / 3) * (SbyN_0));#quantizing level
v = math.ceil(log(q)/log(2.))#number of binary bits
q_1 = 2**v#number of levels required
SbyN_dB1 = 1.76 + 6.02*v#output signal-to-quantizing noise ratio in dB

#results
print "Number of required levels = ",v
print "Output signal-to-quantizing noise ratio (dB) = ",SbyN_dB1
print "Note : In the textbook they took number of levels as approximation so we get change\n in SbyN"

Number of required levels =  7.0
Output signal-to-quantizing noise ratio (dB) =  43.9
Note : In the textbook they took number of levels as approximation so we get change
in SbyN


## Example 21 - pg 416¶

In :
#calculate the minimum number of quantizing levels and bits, bandwidth required
import math
from math import log,exp,sqrt
#given
SbyN_dB = 30.#signal to noise ratio
f_s = 8000.#sampling rate

#calculations
#for Sbyn_dB = 1.76 + 20*logq
x1 = (1./ 20)*(SbyN_dB - 1.76)
q1 = exp(x1*log(10))#quantizing level for first case
v1 = math.ceil(log(q1)/log(2))# number of bits  for first case
f_PCM1 = (v1 / 2) * f_s#minimum required bandwidth  for first case
#for SbyN = 20logq - 10.1
x2 = (1./20) * (SbyN_dB + 10.1)
q2 =  exp(x2*log(10))#quantizing level for second case
v2 = math.ceil(log(q2)/log(2))# number of bits for second case
f_PCM2 =  (v2 / 2) * f_s#minimum required bandwidth for second case

#results
print "i.a.Minimum number of quantizing levels for first case = ",round(q1,2)
print "  b.Number of bits for first case = ",v1
print "  c.Minimum system bandwidth required for first case (kHz) = ",f_PCM1/1000.
print "ii.a.Minimum number of quantizing levels for second case = ",round(q2,1)
print "   b.Number of bits for second case = ",v2
print "   c.Minimum system bandwidth required for second case (kHz) = ",f_PCM2/1000.
print "Note:In the text book they took approximation in\nquantization levels and number bits"

i.a.Minimum number of quantizing levels for first case =  25.82
b.Number of bits for first case =  5.0
c.Minimum system bandwidth required for first case (kHz) =  20.0
ii.a.Minimum number of quantizing levels for second case =  101.2
b.Number of bits for second case =  7.0
c.Minimum system bandwidth required for second case (kHz) =  28.0
Note:In the text book they took approximation in
quantization levels and number bits