Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- from math import pi, sin, cos, fabs
- # Function for sampling
- def sample(signal):
- # I will take 4*len(signal) elements for a good signal presentation
- t = 0.0
- t_sample = list()
- halfElements = int((len(signal) - 1) / 2)
- # If signal has 201 elements ----> halfElements = 100
- # New halfElements will be 400 now
- stepOfSample = pi / (4*halfElements)
- sampledSignal = list()
- while t <= 2*pi:
- sampledSignal.append(sin(t))
- t_sample.append(t)
- t += stepOfSample
- return sampledSignal, t_sample
- # Function for quantization
- def quantization(sampledSignal, timeOfSample, k):
- # k = number of bits I send
- if k <= 0 or k != int(k):
- print("Error using 'quantization' function.")
- return -1000
- # Δ = Vpp / 2^k
- Vmax = max(sampledSignal)
- Vmin = min(sampledSignal)
- delta = (Vmax - Vmin) / (2**k)
- # Create the levels of quantization
- levels = list()
- level = Vmin
- while level < Vmax:
- levels.append(level + delta/2)
- level += delta
- # 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]
- # Now, its time for the values of sampledSignal to be snapped to the numbers contained in the "levels" vector
- quantizedSignal = list()
- for s in sampledSignal:
- minimumDiafora = Vmax - Vmin
- index = 0
- for i in range(len(levels)):
- if fabs(s - levels[i]) < minimumDiafora:
- minimumDiafora = fabs(s - levels[i])
- index = i
- quantizedSignal.append(levels[index])
- timeOfQuantization = timeOfSample
- return quantizedSignal, timeOfQuantization
- # *********************************************************************************************************
- # ********************************************* MAIN FUNCTION *********************************************
- # *********************************************************************************************************
- # 1. Create a list with time(t)-coordinates ----> t = [0, pi/100, 2*pi/100, ...., 2*pi-pi/100, 2*pi] with 201 elements
- t = list()
- i = 0.0
- step = pi / 100
- while i <= 2*pi:
- t.append(i)
- i += step
- # 2. Create the signal I will transmit: s(t) = sin(t)
- signal = list()
- for time in t:
- signal.append(sin(time))
- # 3. Create the sampled vector
- sampledSignal, timeOfSample = sample(signal)
- # 4. Quantization
- k = 4
- quantizedSignal, timeOfQuantization = quantization(sampledSignal, timeOfSample, k)
- plt.plot(t, signal, label='Transmitted Singal: s(t) = sin(t)')
- # plt.plot(timeOfSample, sampledSignal, label='Sample of: s(t) = sin(t)')
- plt.plot(timeOfQuantization, quantizedSignal, label='Quantized Singal if I send ' + str(k) + ' bits')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement