Advertisement
makispaiktis

Quantization for every signal

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