def test_mapABCD_2(self):
        """Test function for mapABCD() 2/2"""
        ABCD = np.array([[0., 0., 0.91561444, -0.91561444],
                         [1., 0., 0., -1.42857142], [0., 1., 0., 0.]])

        with self.assertRaises(ValueError):
            mapABCD(ABCD, 'DUMMY')
예제 #2
0
 def test_mapABCD_1(self):
     """Test function for mapABCD() 1/2"""
     for f0 in self.f0s:
         for form in self.forms:
             for order in self.orders:
                 if f0 != 0. and order % 2 == 1:
                     # odd-order pass band modulator
                     continue
                 # Optimized zero placement
                 print("Testing form: %s, order: %d, f0: %f" % \
                       (form, order, f0))
                 ntf = synthesizeNTF(order, self.osr, 2, self.Hinf, f0)
                 a1, g1, b1, c1 = realizeNTF(ntf, form)
                 # we check realize NTF too
                 self.assertTrue(np.allclose(a1, self.res[f0][form][order]['a'],
                                 atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(g1, self.res[f0][form][order]['g'],
                                 atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(b1, self.res[f0][form][order]['b'],
                                 atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(c1, self.res[f0][form][order]['c'],
                                 atol=1e-4, rtol=1e-3))
                 ABCD = stuffABCD(a1, g1, b1, c1, form)
                 a, g, b, c = mapABCD(ABCD, form)
                 self.assertTrue(np.allclose(a1, a, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(g1, g, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(b1, b, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(c1, c, atol=1e-4, rtol=1e-3))
예제 #3
0
    def test_simulateSNR_1(self):
        """Test function for simulateSNR() 1/4"""
        # first test: f0 = 0
        # Load test references
        fname = pkg_resources.resource_filename(__name__,
                                                "test_data/test_snr_amp.mat")
        amp_ref = scipy.io.loadmat(fname)['amp'].reshape((-1, ))
        snr_ref = scipy.io.loadmat(fname)['snr'].reshape((-1, ))
        amp_user_ref = scipy.io.loadmat(fname)['amp_user'].reshape((-1, ))
        snr_user_ref = scipy.io.loadmat(fname)['snr_user'].reshape((-1, ))

        order = 4
        osr = 256
        nlev = 2
        f0 = 0.22
        Hinf = 1.25
        form = 'CRFB'

        ntf = ds.synthesizeNTF(order, osr, 2, Hinf, f0)
        a1, g1, b1, c1 = ds.realizeNTF(ntf, form)
        ABCD = ds.stuffABCD(a1, g1, b1, c1, form)

        ABCD_ref = np.array([[1., -1.6252, 0, 0, -0.0789, 0.0789],
                             [1., -0.6252, 0, 0, -0.0756, 0.0756],
                             [0, 1., 1., -1.6252, -0.2758, 0.2758],
                             [0, 1., 1., -0.6252, 0.0843, -0.0843],
                             [0, 0, 0, 1., 1., 0]])
        self.assertTrue(np.allclose(ABCD, ABCD_ref, atol=9e-5, rtol=1e-4))

        # bonus test, mapABCD - realizeNTF - stuffABCD
        a2, g2, b2, c2 = ds.mapABCD(ABCD, form)
        self.assertTrue(np.allclose(a1, a2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(g1, g2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(b1, b2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(c1, c2, atol=1e-5, rtol=1e-5))

        # We do three tests:
        # SNR from ABCD matrix
        # SNR from NTF
        # SNR from LTI obj with user specified amplitudes
        snr, amp = ds.simulateSNR(ABCD, osr, None, f0, nlev)
        self.assertTrue(np.allclose(snr, snr_ref, atol=1, rtol=5e-2))
        self.assertTrue(np.allclose(amp, amp_ref, atol=5e-1, rtol=1e-2))
        snr2, amp2 = ds.simulateSNR(ntf, osr, None, f0, nlev)
        self.assertTrue(np.allclose(snr2, snr_ref, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(amp2, amp_ref, atol=1e-5, rtol=1e-5))
        amp_user = np.linspace(-100, 0, 200)[::10]
        snr_user, amp_user = ds.simulateSNR(lti(*ntf),
                                            osr=osr,
                                            amp=amp_user,
                                            f0=f0,
                                            nlev=nlev)
        self.assertTrue(
            np.allclose(snr_user, snr_user_ref[::10], atol=1e-5, rtol=1e-5))
        self.assertTrue(
            np.allclose(amp_user, amp_user_ref[::10], atol=1e-5, rtol=1e-5))
    def test_simulateSNR_1(self):
        """Test function for simulateSNR() 1/4"""
        # first test: f0 = 0
        # Load test references
        fname = pkg_resources.resource_filename(__name__,
                                                "test_data/test_snr_amp.mat")
        amp_ref = scipy.io.loadmat(fname)['amp'].reshape((-1,))
        snr_ref = scipy.io.loadmat(fname)['snr'].reshape((-1,))
        amp_user_ref = scipy.io.loadmat(fname)['amp_user'].reshape((-1,))
        snr_user_ref = scipy.io.loadmat(fname)['snr_user'].reshape((-1,))

        order = 4
        osr = 256
        nlev = 2
        f0 = 0.22
        Hinf = 1.25
        form = 'CRFB'

        ntf = ds.synthesizeNTF(order, osr, 2, Hinf, f0)
        a1, g1, b1, c1 = ds.realizeNTF(ntf, form)
        ABCD = ds.stuffABCD(a1, g1, b1, c1, form)

        ABCD_ref = np.array([[1., -1.6252, 0, 0, -0.0789, 0.0789],
                             [1., -0.6252, 0, 0, -0.0756, 0.0756],
                             [0, 1., 1., -1.6252, -0.2758, 0.2758],
                             [0, 1., 1., -0.6252, 0.0843, -0.0843],
                             [0, 0, 0, 1., 1., 0]])
        self.assertTrue(np.allclose(ABCD, ABCD_ref, atol=9e-5, rtol=1e-4))

        # bonus test, mapABCD - realizeNTF - stuffABCD
        a2, g2, b2, c2 = ds.mapABCD(ABCD, form)
        self.assertTrue(np.allclose(a1, a2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(g1, g2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(b1, b2, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(c1, c2, atol=1e-5, rtol=1e-5))

        # We do three tests:
        # SNR from ABCD matrix
        # SNR from NTF
        # SNR from LTI obj with user specified amplitudes
        snr, amp = ds.simulateSNR(ABCD, osr, None, f0, nlev)
        self.assertTrue(np.allclose(snr, snr_ref, atol=1, rtol=5e-2))
        self.assertTrue(np.allclose(amp, amp_ref, atol=5e-1, rtol=1e-2))
        snr2, amp2 = ds.simulateSNR(ntf, osr, None, f0, nlev)
        self.assertTrue(np.allclose(snr2, snr_ref, atol=1e-5, rtol=1e-5))
        self.assertTrue(np.allclose(amp2, amp_ref, atol=1e-5, rtol=1e-5))
        amp_user = np.linspace(-100, 0, 200)[::10]
        snr_user, amp_user = ds.simulateSNR(lti(*ntf), osr=osr, amp=amp_user,
                                            f0=f0, nlev=nlev)
        self.assertTrue(np.allclose(snr_user, snr_user_ref[::10], atol=1e-5,
                                    rtol=1e-5))
        self.assertTrue(np.allclose(amp_user, amp_user_ref[::10], atol=1e-5,
                                    rtol=1e-5))
예제 #5
0
    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_mapABCD_1(self):
     """Test function for mapABCD() 1/2"""
     for f0 in self.f0s:
         for form in self.forms:
             for order in self.orders:
                 if f0 != 0. and order % 2 == 1:
                     # odd-order pass band modulator
                     continue
                 # Optimized zero placement
                 print("Testing form: %s, order: %d, f0: %f" % \
                       (form, order, f0))
                 ntf = synthesizeNTF(order, self.osr, 2, self.Hinf, f0)
                 a1, g1, b1, c1 = realizeNTF(ntf, form)
                 # we check realize NTF too
                 self.assertTrue(
                     np.allclose(a1,
                                 self.res[f0][form][order]['a'],
                                 atol=1e-4,
                                 rtol=1e-3))
                 self.assertTrue(
                     np.allclose(g1,
                                 self.res[f0][form][order]['g'],
                                 atol=1e-4,
                                 rtol=1e-3))
                 self.assertTrue(
                     np.allclose(b1,
                                 self.res[f0][form][order]['b'],
                                 atol=1e-4,
                                 rtol=1e-3))
                 self.assertTrue(
                     np.allclose(c1,
                                 self.res[f0][form][order]['c'],
                                 atol=1e-4,
                                 rtol=1e-3))
                 ABCD = stuffABCD(a1, g1, b1, c1, form)
                 a, g, b, c = mapABCD(ABCD, form)
                 self.assertTrue(np.allclose(a1, a, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(g1, g, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(b1, b, atol=1e-4, rtol=1e-3))
                 self.assertTrue(np.allclose(c1, c, atol=1e-4, rtol=1e-3))
예제 #7
0
 def test_mapABCD_2(self):
     """Test function for mapABCD() 2/2"""
     ABCD = np.array([[0., 0., 0.91561444, -0.91561444],
                      [1., 0., 0., -1.42857142], [0., 1., 0., 0.]])
     mapABCD(ABCD, 'DUMMY')
예제 #8
0
 def test_mapABCD_2(self):
     """Test function for mapABCD() 2/2"""
     ABCD = np.array([[0., 0., 0.91561444, -0.91561444],
                      [1., 0., 0., -1.42857142],
                      [0., 1., 0., 0.]])
     mapABCD(ABCD, 'DUMMY') 
예제 #9
0
    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))
예제 #10
0
    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))