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)
plt.xlim(xmax=10**fmax_log10) plt.ylim(ymin=-50) plt.xlabel('$f$ [Hz]') plt.ylabel('[dB]') plt.suptitle("Magnitude response of motor linearized model") plt.legend() plt.ion() plt.show() plt.ioff() # Compute optimal and benchmark NTFs hz0043_ir = impulse_response(hz0043) hz02_ir = impulse_response(hz02) hz06_ir = impulse_response(hz06) q0_0043 = q0_from_filter_ir(P, hz0043_ir) ntf0043 = ntf_fir_from_q0(q0_0043) q0_02 = q0_from_filter_ir(P, hz02_ir) ntf02 = ntf_fir_from_q0(q0_02) q0_06 = q0_from_filter_ir(P, hz06_ir) ntf06 = ntf_fir_from_q0(q0_06) delsig_ntf = synthesizeNTF(DELSIG_P, OSR, 3, 1.5, 0) # Plot the NTFs nyz0043 = evalTF(ntf0043, np.exp(2j * np.pi * ff / fphi)) nyz02 = evalTF(ntf02, np.exp(2j * np.pi * ff / fphi)) nyz06 = evalTF(ntf06, np.exp(2j * np.pi * ff / fphi)) delsig_ny = evalTF(delsig_ntf, np.exp(2j * np.pi * ff / fphi)) plt.figure() plt.semilogx(ff, dbv(np.abs(nyz0043)), 'b', label='opt @ $\sigma=0.043$') plt.semilogx(ff, dbv(np.abs(nyz02)), 'r', label='opt @ $\sigma=0.2$')
hz1_ab = sp.signal.zpk2tf(*hz1) hz2_ab = sp.signal.zpk2tf(*hz2) hz_ab_d = np.polymul(hz1_ab[1], hz2_ab[1]) hz_ab_n1 = np.polymul(hz1_ab[0], hz2_ab[1]) hz_ab_n2 = np.polymul(hz2_ab[0], hz1_ab[1]) hz_ab_n = np.polyadd(hz_ab_n1, hz_ab_n2) hz = sp.signal.tf2zpk(hz_ab_n, hz_ab_d) # Compute impulse response print("...computing impulse response of filter") hz_ir = impulse_response(hz, db=60) # Compute the optimal NTF print("... computing optimal NTF") q0 = q0_from_filter_ir(order, hz_ir) ntf_opti = ntf_fir_from_q0(q0, H_inf=H_inf) # Determine freq values for which plots are created fmin = 10**np.ceil(np.log10(10)) fmax = 10**np.floor(np.log10(fphi/2)) ff = np.logspace(np.log10(fmin), np.log10(fmax), 1000) # Compute frequency response data resp_filt = np.abs(evalTF(hz, np.exp(1j*2*np.pi*ff/fphi))) resp_opti = np.abs(evalTF(ntf_opti, np.exp(1j*2*np.pi*ff/fphi))) # Plot frequency response plt.figure() plt.semilogx(ff, dbv(resp_filt), 'b', label="Output filter") plt.semilogx(ff, dbv(resp_opti), 'r', label="Optimal NTF") plt.legend(loc="lower right")
# Generate filter. Transfer function is normalized to be 0dB in pass band # As an example, take cutoff freq at twice the top of the signal band to avoid # attenuation when the input signal is close to it. print("...generating filter") # Care: in butter the cut of frequency is specified as a number from 0 to 1 # where 1 is fphi/2, not fphi hz = sp.signal.butter(1, 2 * (2 * B) / fphi, btype='low') # Compute impulse response print("...computing impulse response of filter") hz_ir = impulse_response(hz, db=60) # Compute the optimal NTF print("... computing optimal NTF") q0 = q0_from_filter_ir(order, hz_ir) ntf_opti = ntf_fir_from_q0(q0, H_inf=H_inf) # Compute an NTF with DELSIG, for comparison print("... computing delsig NTF") ntf_delsig = synthesizeNTF(4, OSR, 3, H_inf, 0) # Determine freq values for which plots are created fmin = 10**np.ceil(np.log10(2 * B / OSR)) fmax = 10**np.floor(np.log10(fphi / 2)) ff = np.logspace(np.log10(fmin), np.log10(fmax), 1000) # Compute frequency response data resp_filt = np.abs(evalTF(hz, np.exp(1j * 2 * np.pi * ff / fphi))) resp_opti = np.abs(evalTF(ntf_opti, np.exp(1j * 2 * np.pi * ff / fphi))) resp_delsig = np.abs(evalTF(ntf_delsig, np.exp(1j * 2 * np.pi * ff / fphi)))
plt.xlim(xmax=10**fmax_log10) plt.ylim(ymin=-50) plt.xlabel('$f$ [Hz]') plt.ylabel('[dB]') plt.suptitle("Magnitude response of motor linearized model") plt.legend() plt.ion() plt.show() plt.ioff() # Compute optimal and benchmark NTFs hz0043_ir = impulse_response(hz0043) hz02_ir = impulse_response(hz02) hz06_ir = impulse_response(hz06) q0_0043 = q0_from_filter_ir(P, hz0043_ir) ntf0043 = ntf_fir_from_q0(q0_0043) q0_02 = q0_from_filter_ir(P, hz02_ir) ntf02 = ntf_fir_from_q0(q0_02) q0_06 = q0_from_filter_ir(P, hz06_ir) ntf06 = ntf_fir_from_q0(q0_06) delsig_ntf = synthesizeNTF(DELSIG_P, OSR, 3, 1.5, 0) # Plot the NTFs nyz0043 = evalTF(ntf0043, np.exp(2j*np.pi*ff/fphi)) nyz02 = evalTF(ntf02, np.exp(2j*np.pi*ff/fphi)) nyz06 = evalTF(ntf06, np.exp(2j*np.pi*ff/fphi)) delsig_ny = evalTF(delsig_ntf, np.exp(2j*np.pi*ff/fphi)) plt.figure() plt.semilogx(ff, dbv(np.abs(nyz0043)), 'b', label='opt @ $\sigma=0.043$')