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 test_q0_butt_lp3(self): # Generate filter. # 3rd order lowpass filter # Freq. passed to butterworth is normalized between 0 and 1 # where 1 is the Nyquist frequency B = 400. OSR = 256 fphi = B * OSR * 2 w0 = 2 * B / fphi hz = signal.butter(3, w0, 'lowpass', output='zpk') # Order P = 12 # Compute q0 in two ways ir = impulse_response(hz, db=80) q0_ir = q0_from_filter_ir(P, ir) q0_mr = q0_weighting(P, hz) np.testing.assert_allclose(q0_ir, q0_mr, atol=1E-7, rtol=1E-5)
def test_butt_bp8_ir(self): # Generate filter. # 8th order bandpass filter # Freq. passed to butterworth is normalized between 0 and 1 # where 1 is the Nyquist frequency fsig = 1000. B = 400. OSR = 64 fphi = B * OSR * 2 w0 = 2 * fsig / fphi B0 = 2 * B / fphi w1 = (np.sqrt(B0**2 + 4 * w0**2) - B0) / 2 w2 = (np.sqrt(B0**2 + 4 * w0**2) + B0) / 2 hz = signal.butter(4, [w1, w2], 'bandpass', output='zpk') ir = impulse_response(hz, db=80) ff = np.logspace(np.log10(w0 / 4), np.log10(w0 * 4), 100) vv1 = evalTF(hz, np.exp(2j * np.pi * ff)) vv2 = evalTF((ir, [1]), np.exp(2j * np.pi * ff)) np.testing.assert_allclose(db(vv1 / vv2), np.zeros_like(vv1), rtol=0, atol=3)
def test_q0_butt_bp8(self): # Generate filter. # 8th order bandpass filter # Freq. passed to butterworth is normalized between 0 and 1 # where 1 is the Nyquist frequency fsig = 1000. B = 400. OSR = 64 fphi = B * OSR * 2 w0 = 2 * fsig / fphi B0 = 2 * B / fphi w1 = (np.sqrt(B0**2 + 4 * w0**2) - B0) / 2 w2 = (np.sqrt(B0**2 + 4 * w0**2) + B0) / 2 hz = signal.butter(4, [w1, w2], 'bandpass', output='zpk') # Order P = 20 # Compute q0 in two ways ir = impulse_response(hz, db=80) q0_ir = q0_from_filter_ir(P, ir) q0_mr = q0_weighting(P, hz) np.testing.assert_allclose(q0_ir, q0_mr, atol=1E-7, rtol=1E-5)
def test_fir_ir(self): fir = np.asarray([1.0, 0.5, 0.25, 0.125]) zpk = (np.roots(fir), np.zeros(4), 1) ir = impulse_response(zpk, db=80) np.testing.assert_allclose(fir, ir)
plt.figure() plt.semilogx(ff, dbv(np.abs(yyz0043)), 'b', label='$\sigma=0.043$') plt.semilogx(ff, dbv(np.abs(yyz02)), 'r', label='$\sigma=0.2$') plt.semilogx(ff, dbv(np.abs(yyz06)), 'g', label='$\sigma=0.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))
w02 = 2*fsig2/fphi B02 = 2*B2/fphi w12 = (np.sqrt(B02**2+4*w02**2)-B02)/2 w22 = (np.sqrt(B02**2+4*w02**2)+B02)/2 hz2 = sp.signal.butter(2, [w12, w22], 'bandpass', output='zpk') 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)))
# FIR Order for optimal NTF order = 12 # Signal amplitude A = 0.4 # 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)
plt.figure() plt.semilogx(ff, dbv(np.abs(yyz0043)), 'b', label='$\sigma=0.043$') plt.semilogx(ff, dbv(np.abs(yyz02)), 'r', label='$\sigma=0.2$') plt.semilogx(ff, dbv(np.abs(yyz06)), 'g', label='$\sigma=0.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))