from __future__ import division from deltasigma import * order = 5 OSR = 32 # Synthesize! H0 = synthesizeNTF(order, OSR, opt=0) # 1. Plot the singularities. subplot(121) plotPZ(H0, markersize=5) 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)) # 2. Plot the magnitude responses. subplot(222) plot(f, magH0) figureMagic([0, 0.5], 0.05, None, [-100, 10], 10, None, (16, 8)) xlabel('Normalized frequency ($1\\rightarrow f_s)$') ylabel('dB') title('NTF Magnitude Response') # 3. Plot the magnitude responses in the signal band. 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)) semilogx(f*2*OSR, magH0) axis([fstart, 1.2, -100,- 30]) grid(True) sigma_H0 = dbv(rmsGain(H0, 0, 0.5/OSR)) semilogx([fstart, 1], sigma_H0*np.array([1, 1])) plot([fstart, 1], sigma_H0*np.array([1, 1]),'o') text(0.15, sigma_H0 + 5, 'rms gain = %5.0fdB' % sigma_H0) xlabel('Normalized frequency ($1\\rightarrow f_B$)') ylabel('dB') tight_layout() # Synthesize again! H0 = None H1 = synthesizeNTF(order, OSR, opt=1) # 1. Plot the singularities. subplot(121) plotPZ(H1, markersize=5) 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) magH1 = dbv(evalTF(H1, z)) # 2. Plot the magnitude responses. subplot(222) plot(f, magH1) figureMagic([0, 0.5], 0.05, None, [-100, 10], 10, None, (16, 8)) xlabel('Normalized frequency ($1\\rightarrow f_s)$') ylabel('dB') title('NTF Magnitude Response') # 3. Plot the magnitude responses in the signal band. subplot(224) fstart = 0.01 f = np.linspace(fstart, 1.2, 200)/(2*OSR) z = np.exp(2j*np.pi*f) magH1 = dbv(evalTF(H1, z)) semilogx(f*2*OSR, magH1) axis([fstart, 1.2, -100,- 30]) grid(True) sigma_H1 = dbv(rmsGain(H1, 0, 0.5/OSR)) semilogx([fstart, 1], sigma_H1*np.array([1, 1])) plot([fstart, 1], sigma_H1*np.array([1, 1]),'o') text(0.15, sigma_H1 + 5, 'RMS gain = %5.0fdB' % sigma_H1) xlabel('Normalized frequency ($1\\rightarrow f_B$)') ylabel('dB') tight_layout() # Synthesize! H0 = synthesizeNTF(order, OSR, opt=0) H1 = synthesizeNTF(order, OSR, opt=1) # 1. Plot the singularities. 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') hold(True) plotPZ(H0, markersize=5) 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. subplot(222) plot(f, magH0, label='All zeros in z=1') hold(True) plot(f, magH1, label='Optimized zeros') figureMagic([0, 0.5], 0.05, None, [-100, 10], 10, None, (16, 8)) xlabel('Normalized frequency ($1\\rightarrow f_s)$') ylabel('dB') legend(loc=4) title('NTF Magnitude Response') # 3. Plot the magnitude responses in the signal band. 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)) semilogx(f*2*OSR, magH0, label='All zeros in z=1') hold(True) semilogx(f*2*OSR, magH1, label='Optimized zeros') axis([fstart, 1.2, -100,- 30]) grid(True) sigma_H0 = dbv(rmsGain(H0, 0, 0.5/OSR)) sigma_H1 = dbv(rmsGain(H1, 0, 0.5/OSR)) semilogx([fstart, 1], sigma_H0*np.array([1, 1])) plot([fstart, 1], sigma_H0*np.array([1, 1]),'o') text(0.15, sigma_H0 + 5, 'RMS gain = %5.0fdB' % sigma_H0) semilogx([fstart, 1], sigma_H1*np.array([1, 1])) plot([fstart, 1], sigma_H1*np.array([1, 1]),'o') text(0.15, sigma_H1 + 5, 'RMS gain = %5.0fdB' % sigma_H1) xlabel('Normalized frequency ($1\\rightarrow f_B$)') ylabel('dB') legend(loc=4) tight_layout() order = 8 OSR = 64 opt = 2 f0 = 0.125 H = synthesizeNTF(order, OSR, opt, 1.5, f0) subplot(121) plotPZ(H) title('Bandpass NTF Poles and Zeros') f = np.concatenate((np.linspace(0, f0 - 1./(2.*OSR), 50), np.linspace(f0 - 1./ (2 * OSR), f0 + 1./(2.*OSR), 100), np.linspace(f0 + 1./(2.*OSR), 0.5, 50))) z = np.exp(2j * pi * f) magH = dbv(evalTF(H, z)) subplot(222) plot(f, magH) hold(True) G = (np.zeros((order/2,)), H[1], 1) k = 1./np.abs(evalTF(G, exp(2j*np.pi*f0))) G = (G[0], G[1], k) magG = dbv(evalTF(G, z)) plot(f, magG, 'r') figureMagic([0, 0.5], 0.05, None, [-100, 10], 10, None, (16, 8)) #axis([0, 0.5, -100, 10]) grid(True) xlabel('Normalized frequency ($1 \\rightarrow fs$)') ylabel('dB') title('Bandpass NTF/STF Magnitude Response') f = np.linspace(f0 - 0.3/OSR, f0 + 0.3/OSR) z = np.exp(2j*np.pi*f) magH = dbv(evalTF(H, z)) subplot(224) fstart = -.5 plot(2*OSR*(f - f0), magH) axis([- 0.6, 0.6, -100, -60]) grid(True) sigma_H = dbv(rmsGain(H, f0 - 0.25/OSR, f0 + 0.25/OSR)) hold(True) plot([-0.5, 0.5], sigma_H*np.array([1, 1])) plot([-0.5, 0.5], sigma_H*np.array([1, 1]), 'o') text(-.2, sigma_H + 5, 'rms gain = %5.0fdB' % sigma_H) xlabel('Normalized frequency offset') ylabel('dB') tight_layout()