Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment 5

Quantization of Sinusoidal Signal


Apply Maths and the plot in python
And Show ErroR

--------------------------------------------

Mathematcal Equations for Signal to Quantization noise Ratio


---------------------------------------------
# Imports
import numpy as np
import matplotlib.pyplot as plt
import plotly.offline
import plotly.tools as tls
%matplotlib notebook

# Signal Processing Parameters


Fs = 32000 # Sampling frequency
T=1/Fs # Sampling Time

# Input Signal
A=1
freq=50
n_period=1
period=np.round((1/freq)*n_period*Fs).astype(int)
t = np.arange(Fs+1)*T # Time vector
sinewave = A*np.sin(2*np.pi*freq*t)

# Quantization and De-quantization


N=2
stepsize=(1.0-(-1.0))/(2**N)
#Encode
sinewave_quant_rise_ind=np.floor(sinewave/stepsize)
sinewave_quant_tread_ind=np.round(sinewave/stepsize)
#Decode
sinewave_quant_rise_rec=sinewave_quant_rise_ind*stepsize+stepsize/2
sinewave_quant_tread_rec=sinewave_quant_tread_ind*stepsize
# Shape for plotting
t_quant=np.delete(np.repeat(t[:period+1],2),-1)
sinewave_quant_rise_rec_plot=np.delete(np.repeat(sinewave_quant_rise_rec[:period+1],2)
,0)
sinewave_quant_tread_rec_plot=np.delete(np.repeat(sinewave_quant_tread_rec[:period+1
],2),0)

# Quantization Error
quant_error_tread=sinewave_quant_tread_rec-sinewave
quant_error_rise=sinewave_quant_rise_rec-sinewave

fig = plt.figure(figsize=(12,8))
plt.subplot(2,1,1)
plt.plot(t[:period+1],sinewave[:period+1], label='Original Signal')
plt.plot(t_quant,sinewave_quant_rise_rec_plot, label='Quantized Signal (Mid-Rise)')
plt.plot(t_quant,sinewave_quant_tread_rec_plot, label='Quantized Signal (Mid-Tread)')
plt.title('Orignal and Quantized Signals', fontsize=18)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.yticks(np.arange(-1-stepsize, 1+stepsize, stepsize))
#plt.legend()
plt.grid()
#plt.tight_layout()
plt.subplot(2,1,2)
plt.plot(t[:period+1],quant_error_tread[:period+1], label='Quantization Error')
plt.grid()
plt.title('Quantization Error', fontsize=18)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
#plt.tight_layout()
plt.subplots_adjust(hspace=0.5)

plotly_fig = tls.mpl_to_plotly(fig)
plotly_fig.layout.update(showlegend=True)
plotly.offline.iplot(plotly_fig)

You might also like