import pylab as plt
import numpy as np
from deltasigma import *
OSR = 4
order = 8
H_inf = 3
plt.figure(figsize=(12,6))
H0 = synthesizeNTF(order, OSR, 1, H_inf)
H1 = synthesizeChebyshevNTF(order, OSR, 0, H_inf)
# 1. Plot the singularities.
plt.subplot(121)
# we plot the singularities of the optimized NTF in light
# green with slightly bigger markers so that we can better
# distinguish the two NTF's when overlayed.
plotPZ(H1, markersize=7, color='#90EE90')
plt.hold(True)
plotPZ(H0, markersize=5)
plt.title('NTF Poles and Zeros')
f = np.concatenate((np.linspace(0, 0.75/OSR, 100), np.linspace(0.75/OSR, 0.5, 100)))
z = np.exp(2j*np.pi*f)
magH0 = dbv(evalTF(H0, z))
magH1 = dbv(evalTF(H1, z))
# 2. Plot the magnitude responses.
plt.subplot(222)
plt.plot(f, magH0, label='synthesizeNTF')
plt.hold(True)
plt.plot(f, magH1, label='synthesizeChebyshevNTF')
figureMagic([0, 0.5], 0.05, None, [-80, 20], 10, None)
plt.xlabel('Normalized frequency ($1\\rightarrow f_s)$')
plt.ylabel('dB')
plt.legend(loc=4)
plt.title('NTF Magnitude Response')
# 3. Plot the magnitude responses in the signal band.
plt.subplot(224)
fstart = 0.01
f = np.linspace(fstart, 1.2, 200)/(2*OSR)
z = np.exp(2j*np.pi*f)
magH0 = dbv(evalTF(H0, z))
magH1 = dbv(evalTF(H1, z))
plt.semilogx(f*2*OSR, magH0, label='synthesizeNTF')
plt.hold(True)
plt.semilogx(f*2*OSR, magH1, label='synthesizeChebyshevNTF')
plt.axis([fstart, 1, -50, 0])
plt.grid(True)
sigma_H0 = dbv(rmsGain(H0, 0, 0.5/OSR))
sigma_H1 = dbv(rmsGain(H1, 0, 0.5/OSR))
plt.semilogx([fstart, 1], sigma_H0*np.array([1, 1]), linewidth=3, color='#191970')
plt.text(0.15, sigma_H0 + 1.5, 'RMS gain = %5.0fdB' % sigma_H0)
plt.semilogx([fstart, 1], sigma_H1*np.array([1, 1]), linewidth=3, color='#228B22')
plt.text(0.15, sigma_H1 + 1.5, 'RMS gain = %5.0fdB' % sigma_H1)
plt.xlabel('Normalized frequency ($1\\rightarrow f_B$)')
plt.ylabel('dB')
plt.legend(loc=3)
plt.tight_layout()