Advertisement
makispaiktis

Quantization in Communication Systems

Jun 8th, 2020 (edited)
1,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. from math import pi, sin, cos, fabs
  3.  
  4. # Function for sampling
  5. def sample(signal):
  6.     # I will take 4*len(signal) elements for a good signal presentation
  7.     t = 0.0
  8.     t_sample = list()
  9.     halfElements = int((len(signal) - 1) / 2)
  10.     # If signal has 201 elements ----> halfElements = 100
  11.     # New halfElements will be 400 now
  12.     stepOfSample =  pi / (4*halfElements)
  13.     sampledSignal = list()
  14.     while t <= 2*pi:
  15.         sampledSignal.append(sin(t))
  16.         t_sample.append(t)
  17.         t += stepOfSample
  18.     return sampledSignal, t_sample
  19.  
  20.  
  21. # Function for quantization
  22. def quantization(sampledSignal, timeOfSample, k):
  23.     # k = number of bits I send
  24.     if k <= 0 or k != int(k):
  25.         print("Error using 'quantization' function.")
  26.         return -1000
  27.     # Δ = Vpp / 2^k
  28.     Vmax = max(sampledSignal)
  29.     Vmin = min(sampledSignal)
  30.     delta = (Vmax - Vmin) / (2**k)
  31.     # Create the levels of quantization
  32.     levels = list()
  33.     level = Vmin
  34.     while level < Vmax:
  35.         levels.append(level + delta/2)
  36.         level += delta
  37.     # For 3 bits and s(t) = sin(t) ----> Vmax = -Vmin = 1 ----> delta = (1-(-1)) / 2^3 = 0.25 ----> levels = [-0.875, -0.625, -0.375, -0.125, 0.125, 0.375, 0.625, 0.875]
  38.     # Now, its time for the values of sampledSignal to be snapped to the numbers contained in the "levels" vector
  39.     quantizedSignal = list()
  40.     for s in sampledSignal:
  41.         minimumDiafora = Vmax - Vmin
  42.         index = 0
  43.         for i in range(len(levels)):
  44.             if fabs(s - levels[i]) < minimumDiafora:
  45.                 minimumDiafora = fabs(s - levels[i])
  46.                 index = i
  47.         quantizedSignal.append(levels[index])
  48.     timeOfQuantization = timeOfSample
  49.     return quantizedSignal, timeOfQuantization
  50.  
  51. # *********************************************************************************************************
  52. # ********************************************* MAIN FUNCTION *********************************************
  53. # *********************************************************************************************************
  54.  
  55. # 1. Create a list with time(t)-coordinates ----> t = [0, pi/100, 2*pi/100, ...., 2*pi-pi/100, 2*pi] with 201 elements
  56. t = list()
  57. i = 0.0
  58. step = pi / 100
  59. while i <= 2*pi:
  60.     t.append(i)
  61.     i += step
  62.  
  63. # 2. Create the signal I will transmit: s(t) = sin(t)
  64. signal = list()
  65. for time in t:
  66.     signal.append(sin(time))
  67. # 3. Create the sampled vector
  68. sampledSignal, timeOfSample = sample(signal)
  69. # 4. Quantization
  70. k = 4
  71. quantizedSignal, timeOfQuantization = quantization(sampledSignal, timeOfSample, k)
  72.  
  73.  
  74. plt.plot(t, signal, label='Transmitted Singal: s(t) = sin(t)')
  75. # plt.plot(timeOfSample, sampledSignal, label='Sample of: s(t) = sin(t)')
  76. plt.plot(timeOfQuantization, quantizedSignal, label='Quantized Singal if I send ' + str(k) + ' bits')
  77. plt.legend()
  78. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement