def test_scaleABCD_Q(self): """Test function for scaleABCD() with Imag ABCD""" ABCD_IN = np.array([[ 0.888 + 0.4598j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0.0516 + 0.j, 0.0516 + 0.j ], [ 0.6073 + 0.7945j, 0.9530 + 0.3028j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0.3230 + 0.j ], [ 0. + 0.j, 0.9459 + 0.3245j, 0.9239 + 0.3827j, 0. + 0.j, 0. + 0.j, 0.9162 + 0.j ], [ 0. + 0.j, 0. + 0.j, 0. + 0.j, 0.9239 - 0.3827j, 0. + 0.j, 0.1778 + 0.j ], [ 0. + 0.j, 0. + 0.j, -0.9935 - 0.1138j, -0.7405 - 0.672j, 0. + 0.j, 0. + 0.j ]]) ABCD, umax, S = ds.scaleABCD(ABCD_IN, nlev=9, f=1. / 16) #references here because I prefer to have 1 file umax_ref = 8 S_ref = np.array([[0.3586, 0., 0., 0.], [0., 0.1207, 0., 0.], [0., 0., 0.0805, 0.], [0., 0., 0., 0.3671]]) ABCD_ref = np.array( [[ 0.888037198535288 + 0.459771610712905j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0185052748572140 + 0.0j, 0.0185052748572140 + 4.35415940995948e-18j ], [ 0.204472470161495 + 0.267520232240738j, 0.953044748762363 + 0.302829501298050j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0389934281708177 + 0.0j ], [ 0.0 + 0.0j, 0.630626110921381 + 0.216330153871036j, 0.923879532511340 + 0.382683432365112j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0737451799430360 - 1.22879569706001e-17j ], [ 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.923879532511287 - 0.382683432365090j, 0.0 + 0.0j, 0.0652579452814600 + 1.01879713420556e-17j ], [ 0.0 + 0.0j, 0.0 + 0.0j, -12.3424213726332 - 1.41434395098423j, -2.01750453626270 - 1.83077728688503j, 0.0 + 0.0j, 0.0 + 0.0j ]]) assert np.allclose(ABCD, ABCD_ref, atol=1e-2, rtol=1e-1) assert np.allclose(S, S_ref, atol=1e-3, rtol=1e-2) assert umax == umax_ref
def test_scaleABCD(self): """Test function for scaleABCD()""" ABCD, umax, S = ds.scaleABCD(self.ABCD0, self.nlev, self.f0) # mapping the NTF to states, there is not a perfect match between # the original code and the scipy version. -> rtol approx 20% #if not np.allclose(ABCD, ABCD_ref, atol=1e-2, rtol=3e-1): # aerr = ABCD_ref-ABCD # rerr = 2*(ABCD_ref-ABCD)/(ABCD_ref+ABCD) # print(repr(ABCD_ref)) # print(repr(ABCD)) # print(aerr) # print(rerr) # this is a rather high relative error. We get it on Travis-CI # Probably related to the libs used? self.assertTrue(np.allclose(ABCD, self.ABCD_ref, atol=1e-2, rtol=30e-2)) self.assertTrue(np.allclose(umax, self.umax_ref, atol=1e-4, rtol=1e-3)) self.assertTrue(np.allclose(np.diag(S), self.Sdiag_ref, atol=1e-2, rtol=25e-1))
def test_scaleABCD_Q(self): """Test function for scaleABCD() with Imag ABCD""" ABCD_IN = np.array([[0.888+0.4598j, 0.+0.j, 0.+0.j, 0.+0.j, 0.0516+0.j, 0.0516+0.j], [0.6073+0.7945j, 0.9530+0.3028j, 0.+0.j, 0.+0.j, 0.+0.j, 0.3230+0.j], [0.+0.j, 0.9459+0.3245j, 0.9239+0.3827j, 0.+0.j, 0.+0.j, 0.9162+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.9239-0.3827j, 0.+0.j, 0.1778+0.j], [0.+0.j, 0.+0.j, -0.9935-0.1138j, -0.7405-0.672j, 0.+0.j, 0.+0.j]]) ABCD, umax, S = ds.scaleABCD(ABCD_IN, nlev=9, f=1./16) #references here because I prefer to have 1 file umax_ref = 8 S_ref = np.array([[0.3586, 0., 0., 0.], [0., 0.1207, 0., 0.], [0., 0., 0.0805, 0.], [0., 0., 0., 0.3671]]) ABCD_ref = np.array([[0.888037198535288 + 0.459771610712905j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0185052748572140 + 0.0j, 0.0185052748572140 + 4.35415940995948e-18j], [0.204472470161495 + 0.267520232240738j, 0.953044748762363 + 0.302829501298050j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0389934281708177 + 0.0j], [0.0 + 0.0j, 0.630626110921381 + 0.216330153871036j, 0.923879532511340 + 0.382683432365112j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0737451799430360 - 1.22879569706001e-17j], [0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.923879532511287 - 0.382683432365090j, 0.0 + 0.0j, 0.0652579452814600 + 1.01879713420556e-17j], [0.0 + 0.0j, 0.0 + 0.0j, -12.3424213726332 - 1.41434395098423j, -2.01750453626270 - 1.83077728688503j, 0.0 + 0.0j, 0.0 + 0.0j]]) assert np.allclose(ABCD, ABCD_ref, atol=1e-2, rtol=1e-1) assert np.allclose(S, S_ref, atol=1e-3, rtol=1e-2) assert umax == umax_ref
def __init__(self, input: FixedPointValue, osr=32, order=4, n_lev=2): self.input = input self.fmt = input.fmt self.h = synthesizeNTF(order, osr, 1) a, g, b, c = realizeNTF(self.h, form='CIFB') abcd = stuffABCD(a, g, b, c) abcd_scaled, umax, s = scaleABCD(abcd, n_lev) self.parameters = mapABCD(abcd_scaled) print(umax) self.order = order self.osr = osr assert n_lev > 1 self.n_lev = n_lev self.quantization_values = [i * 2 - (n_lev / 2) for i in range(n_lev)] self.output = Signal(range(0, len(self.quantization_values)))
def test_scaleABCD(self): """Test function for scaleABCD()""" ABCD, umax, S = ds.scaleABCD(self.ABCD0, self.nlev, self.f0) # mapping the NTF to states, there is not a perfect match between # the original code and the scipy version. -> rtol approx 20% #if not np.allclose(ABCD, ABCD_ref, atol=1e-2, rtol=3e-1): # aerr = ABCD_ref-ABCD # rerr = 2*(ABCD_ref-ABCD)/(ABCD_ref+ABCD) # print(repr(ABCD_ref)) # print(repr(ABCD)) # print(aerr) # print(rerr) # this is a rather high relative error. We get it on Travis-CI # Probably related to the libs used? self.assertTrue(np.allclose(ABCD, self.ABCD_ref, atol=1e-2, rtol=30e-2)) self.assertTrue(np.allclose(umax, self.umax_ref, atol=1e-4, rtol=1e-3)) self.assertTrue( np.allclose(np.diag(S), self.Sdiag_ref, atol=1e-2, rtol=25e-1))
def test_dsdemo3(self): """dsdemo3 test: Delta sigma modulator synthesis""" order = 5 R = 42 opt = 1 H = ds.synthesizeNTF(order, R, opt) # ## Evaluation of the coefficients for a CRFB topology a, g, b, c = ds.realizeNTF(H) # Use a single feed-in for the input # Lets check that stuffABCD understands that if b is scalar it means 1 feed-in. ABCD = ds.stuffABCD(a, g, b[0], c) # for passing the assertion below, we need b to have the trailing zeros b = np.concatenate((np.atleast_1d(b[0]), np.zeros((b.shape[0] - 1, )))) u = np.linspace(0, 0.6, 30) N = 1e4 T = np.ones((1, N)) maxima = np.zeros((order, len(u))) for i in range(len(u)): ui = u[i] v, xn, xmax, _ = ds.simulateDSM(ui * T, ABCD) maxima[:, i] = np.squeeze(xmax) if any(xmax > 1e2): umax = ui u = u[:i + 1] maxima = maxima[:, :i] break # save the maxima prescale_maxima = np.copy(maxima) # ## Scaled modulator # ### Calculate the scaled coefficients ABCDs, umax, _ = ds.scaleABCD(ABCD, N_sim=1e5) as_, gs, bs, cs = ds.mapABCD(ABCDs) # ### Calculate the state maxima u = np.linspace(0, umax, 30) N = 1e4 T = np.ones((N, )) maxima = np.zeros((order, len(u))) for i in range(len(u)): ui = u[i] v, xn, xmax, _ = ds.simulateDSM(ui * T, ABCDs) maxima[:, i] = xmax.squeeze() if any(xmax > 1e2): umax = ui u = u[:i] maxima = maxima[:, :i] break self.assertTrue(np.allclose(ABCD, self.data['ABCD'])) self.assertTrue( np.allclose(ABCDs, self.data['ABCDs'], atol=1e-2, rtol=5e-2)) self.assertTrue(np.allclose(a, self.data['a'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(b, self.data['b'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(g, self.data['g'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(c, self.data['c'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(as_, self.data['as'], atol=1e-2, rtol=5e-3)) self.assertTrue(np.allclose(bs, self.data['bs'], atol=5e-3, rtol=5e-3)) self.assertTrue(np.allclose(gs, self.data['gs'], atol=5e-3, rtol=5e-3)) self.assertTrue(np.allclose(cs, self.data['cs'], atol=3e-2, rtol=3e-2)) self.assertTrue( np.allclose(umax, self.data['umax'], atol=5e-3, rtol=5e-3)) self.assertTrue( np.allclose(maxima, self.data['maxima'], atol=2e-2, rtol=5e-2))
def test_dsdemo3(self): """dsdemo3 test: Delta sigma modulator synthesis""" order = 5 R = 42 opt = 1 H = ds.synthesizeNTF(order, R, opt) # ## Evaluation of the coefficients for a CRFB topology a, g, b, c = ds.realizeNTF(H) # Use a single feed-in for the input # Lets check that stuffABCD understands that if b is scalar it means 1 feed-in. ABCD = ds.stuffABCD(a, g, b[0], c) # for passing the assertion below, we need b to have the trailing zeros b = np.concatenate((np.atleast_1d(b[0]), np.zeros((b.shape[0] - 1,)))) u = np.linspace(0, 0.6, 30) N = 1e4 T = np.ones((1, N)) maxima = np.zeros((order, len(u))) for i in range(len(u)): ui = u[i] v, xn, xmax, _ = ds.simulateDSM(ui*T, ABCD); maxima[:, i] = np.squeeze(xmax) if any(xmax > 1e2): umax = ui u = u[:i+1] maxima = maxima[:, :i] break # save the maxima prescale_maxima = np.copy(maxima) # ## Scaled modulator # ### Calculate the scaled coefficients ABCDs, umax, _ = ds.scaleABCD(ABCD, N_sim=1e5) as_, gs, bs, cs = ds.mapABCD(ABCDs) # ### Calculate the state maxima u = np.linspace(0, umax, 30) N = 1e4 T = np.ones((N,)) maxima = np.zeros((order, len(u))) for i in range(len(u)): ui = u[i] v, xn, xmax, _ = ds.simulateDSM(ui*T, ABCDs) maxima[:, i] = xmax.squeeze() if any(xmax > 1e2): umax = ui; u = u[:i] maxima = maxima[:, :i] break self.assertTrue(np.allclose(ABCD, self.data['ABCD'])) self.assertTrue(np.allclose(ABCDs, self.data['ABCDs'], atol=1e-2, rtol=5e-2)) self.assertTrue(np.allclose(a, self.data['a'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(b, self.data['b'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(g, self.data['g'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(c, self.data['c'], atol=1e-5, rtol=1e-3)) self.assertTrue(np.allclose(as_, self.data['as'], atol=1e-2, rtol=5e-3)) self.assertTrue(np.allclose(bs, self.data['bs'], atol=5e-3, rtol=5e-3)) self.assertTrue(np.allclose(gs, self.data['gs'], atol=5e-3, rtol=5e-3)) self.assertTrue(np.allclose(cs, self.data['cs'], atol=3e-2, rtol=3e-2)) self.assertTrue(np.allclose(umax, self.data['umax'], atol=5e-3, rtol=5e-3)) self.assertTrue(np.allclose(maxima, self.data['maxima'], atol=2e-2, rtol=5e-2))