def test_ntf_butt_bp8_cvxpy_cvxopt(self): try: import cvxpy # analysis:ignore except: raise SkipTest("Modeler 'cvxpy' not installed") ntf1 = ntf_fir_weighting(self.order, self.hz, modeler='cvxpy', show_progress=False) np.testing.assert_allclose(np.sort(ntf1[0]), self.z_e, rtol=1e-7)
def test_ntf_butt_bp8_vs_legacy(self): # Compute q0 in two ways ir = impulse_response(self.hz, db=80) ntf1 = ntf_fir_weighting(self.order, self.hz, show_progress=False) q0 = q0_from_filter_ir(self.order, ir) ntf2 = ntf_fir_from_q0(q0, show_progress=False) mf1 = quantization_noise_gain(ntf1, self.hz) mf2 = quantization_noise_gain(ntf2, self.hz) np.testing.assert_allclose(mf2, mf1, rtol=1e-7) mf3 = quantization_noise_gain_by_conv(ntf1, self.hz) np.testing.assert_allclose(mf3, mf1, rtol=5e-6)
def bench_ntf_fir_weighting_cvxpy_cvxopt(self): try: import cvxpy # analysis:ignore except: raise SkipTest("Modeler 'cvxpy' not installed") print("Benchmarking NTF FIR synthesis with 'cvxpy' modeler + `cvxopt`") tic = time.clock() ntf = ntf_fir_weighting(self.order, self.hz, self.H_inf, modeler='cvxpy', show_progress=False) timing = time.clock()-tic mf = quantization_noise_gain(ntf, self.hz) vv = np.abs(evalTF(ntf, np.exp(2j*np.pi*self.ff))) pe = np.max(vv)-self.H_inf print("Timing: {:6.2f}".format(timing)) print("Accuracy on constraint: {:e}".format(pe)) print("Accuracy on goal: {:e}".format(mf))
return 1. if f < B/fphi else 1e-12 # Introduce a weighting component that magnifies weight for # frequencies < B/10 def weight2(f): return 50.-49.*f/(B/fphi/10.) if f < B/fphi/10. else 1. # Get the final weighting function as the product of the two def w2(f): return w1(f)*weight2(f) print("... computing optimal NTF...") ntf1 = ntf_fir_weighting(order, w1, quad_opts={'points': [B/fphi]}) ntf2 = ntf_fir_weighting(order, w2, quad_opts={'points': [B/fphi, B/fphi/10]}) # Prepare frequency axis for plotting fmin = 10**np.ceil(np.log10(B/OSR/100)) fmax = fphi/2 ff = np.logspace(np.log10(fmin), np.log10(fmax), 1000) resp_w1 = np.asarray(map(w1, ff/fphi)) resp_ntf1 = np.abs(evalTF(ntf1, np.exp(1j*2*np.pi*ff/fphi))) resp_w2 = np.asarray(map(w2, ff/fphi)) resp_ntf2 = np.abs(evalTF(ntf2, np.exp(1j*2*np.pi*ff/fphi))) ffa = np.logspace(np.log10(0.5E-5), np.log10(0.5), 1024) vv_a2 = dbv(np.abs(evalTF(ntf2, np.exp(1j*2*np.pi*ffa))))
vv = np.fromiter(list(map(w1, ff)), np.double) plt.figure() plt.plot(ff, vv) plt.xlim(1e-5, 1. / 2) plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1. / 2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.) plt.ylabel('$w(f)$', y=0.9) plt.grid(True, 'both') plt.suptitle('Weighting function') plt.tight_layout(rect=[0, 0, 1, 0.98]) print("... computing optimal NTF") ntf_opti = ntf_fir_weighting(order, w1, H_inf=H_inf) ntf_opti_mag = lambda f: np.abs(evalTF(ntf_opti, np.exp(-2j * np.pi * f))) print("... computing delsig NTF") ntf_delsig = synthesizeNTF(delsig_order, OSR, 3, H_inf, 0) ntf_delsig_mag = lambda f: np.abs(evalTF(ntf_delsig, np.exp(-2j * np.pi * f))) print("... plotting optimal NTF amplitude response") vv_mag = dbv(ntf_opti_mag(ff)) vv_mag_delsig = dbv(ntf_delsig_mag(ff)) plt.figure() plt.plot(ff, vv_mag, label='Proposed') plt.plot(ff, vv_mag_delsig, 'r-o', linewidth=0.5,
vv = np.fromiter(map(w1, ff), np.double) plt.figure() plt.plot(ff, vv) plt.xlim(1e-5, 1./2) plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1./2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.) plt.ylabel('$w(f)$', y=0.9) plt.grid(True, 'both') plt.suptitle('Weighting function') plt.tight_layout(rect=[0, 0, 1, 0.98]) print("... computing optimal NTF") ntf_opti = ntf_fir_weighting(order, w1, H_inf=H_inf) ntf_opti_mag = lambda f: np.abs(evalTF(ntf_opti, np.exp(-2j*np.pi*f))) print("... computing delsig NTF") ntf_delsig = synthesizeNTF(delsig_order, OSR, 3, H_inf, 0) ntf_delsig_mag = lambda f: np.abs(evalTF(ntf_delsig, np.exp(-2j*np.pi*f))) print("... plotting optimal NTF amplitude response") vv_mag = dbv(ntf_opti_mag(ff)) vv_mag_delsig = dbv(ntf_delsig_mag(ff)) plt.figure() plt.plot(ff, vv_mag, label='Proposed') plt.plot(ff, vv_mag_delsig, 'r-o', linewidth=0.5, markevery=24, markersize=3, label='Reference') plt.xlim(1e-5, 1./2) # plt.ylim(0,1.1)
plt.xlim(1e-5, 1. / 2) plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1. / 2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.) plt.ylabel('$w(f)$', y=0.9) plt.grid(True, 'both') plt.suptitle('Weighting function') plt.tight_layout(rect=[0, 0, 1, 0.98]) print("... computing optimal NTF") ntf_opti = ntf_fir_weighting(order, w1, H_inf=H_inf, cvxopt_opts={ 'reltol': 1E-11, 'abstol': 1E-9, 'feastol': 1E-9 }) ntf_opti_mag = lambda f: np.abs(evalTF(ntf_opti, np.exp(-2j * np.pi * f))) print("... plotting optimal NTF amplitude response") vv_mag = dbv(ntf_opti_mag(ff)) plt.figure() plt.plot(ff, vv_mag) plt.xlim(1e-5, 1. / 2) # plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1. / 2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.)
plt.figure() plt.plot(ff, vv) plt.xlim(1e-5, 1./2) plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1./2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.) plt.ylabel('$w(f)$', y=0.9) plt.grid(True, 'both') plt.suptitle('Weighting function') plt.tight_layout(rect=[0, 0, 1, 0.98]) print("... computing optimal NTF") ntf_opti = ntf_fir_weighting(order, w1, H_inf=H_inf, cvxopt_opts={'reltol': 1E-11, 'abstol': 1E-9, 'feastol': 1E-9}) ntf_opti_mag = lambda f: np.abs(evalTF(ntf_opti, np.exp(-2j*np.pi*f))) print("... plotting optimal NTF amplitude response") vv_mag = dbv(ntf_opti_mag(ff)) plt.figure() plt.plot(ff, vv_mag) plt.xlim(1e-5, 1./2) # plt.ylim(0, 1.1) plt.xscale('log', basex=10) plt.gca().set_xticks([1E-5, 1./2]) plt.gca().set_xticklabels(['$10^{-5}$', r'$\frac{1}{2}$']) plt.xlabel('$f$', x=1.) plt.ylabel((r'$\left|\mathit{NTF}\,\left(\mathrm{e}^{\mathrm{i} 2\pi f}' +
return 1. if f < B / fphi else 1e-12 # Introduce a weighting component that magnifies weight for # frequencies < B/10 def weight2(f): return 50. - 49. * f / (B / fphi / 10.) if f < B / fphi / 10. else 1. # Get the final weighting function as the product of the two def w2(f): return w1(f) * weight2(f) print("... computing optimal NTF...") ntf1 = ntf_fir_weighting(order, w1, quad_opts={'points': [B / fphi]}) ntf2 = ntf_fir_weighting(order, w2, quad_opts={'points': [B / fphi, B / fphi / 10]}) # Prepare frequency axis for plotting fmin = 10**np.ceil(np.log10(B / OSR / 100)) fmax = fphi / 2 ff = np.logspace(np.log10(fmin), np.log10(fmax), 1000) resp_w1 = np.fromiter(list(map(w1, ff / fphi)), np.double) resp_ntf1 = np.abs(evalTF(ntf1, np.exp(1j * 2 * np.pi * ff / fphi))) resp_w2 = np.fromiter(list(map(w2, ff / fphi)), np.double) resp_ntf2 = np.abs(evalTF(ntf2, np.exp(1j * 2 * np.pi * ff / fphi))) ffa = np.logspace(np.log10(0.5E-5), np.log10(0.5), 1024)