Beispiel #1
0
    def testMultipleQ2(self):
        """Test function for DS simulation with nq>1 2/2"""
        # filtering and simulation
        filtM1 = [0., 0., 0., 2., -1.]
        filtM2 = [1., -2., 1.]
        ntf_eq = zpk_multiply(self.ntfs[1, 1], self.ntfs[1, 1])
        M = self.nlev[0] - 1
        osr = 64
        f0 = 0.
        f1, f2 = ds.ds_f1f2(OSR=64, f0=0., complex_flag=False)
        delta = 2
        Amp = ds.undbv(-3)  # Test tone amplitude, relative to full-scale.
        f = 0.3  # will be adjusted to a bin
        N = 2**12
        f1_bin = int(np.round(f1 * N))
        f2_bin = int(np.round(f2 * N))
        fin = np.round(((1 - f) / 2 * f1 + (f + 1) / 2 * f2) * N)
        # input sine
        t = np.arange(0, N).reshape((1, -1))
        u = Amp * M * np.cos((2 * np.pi / N) * fin * t)
        vx, _, xmax, y = ds.simulateDSM(u, self.ABCD, nlev=self.nlev)
        # separate output #1 and output #2
        v1 = vx[0, :]
        v2 = vx[1, :]
        # filter and combine
        vf = lfilter(filtM1, [1.], v1) + lfilter(filtM2, [1.], v2)
        # compute the spectra
        window = ds.ds_hann(N)
        NBW = 1.5 / N
        spec0 = np.fft.fft(vf * window) / (M * N / 2) / ds.undbv(-6)
        spec1 = np.fft.fft(v1 * window) / (M * N / 2) / ds.undbv(-6)
        spec2 = np.fft.fft(v1 * window) / (M * N / 2) / ds.undbv(-6)
        freq = np.linspace(0, 0.5, N // 2 + 1)

        # smooth, calculate the theorethical response and the SNR for VF
        spec0_smoothed = ds.circ_smooth(np.abs(spec0)**2., 16)
        Snn0 = np.abs(ds.evalTF(ntf_eq, np.exp(
            2j * np.pi * freq)))**2 * 2 / 12 * (delta / M)**2
        snr0 = ds.calculateSNR(spec0[f1_bin:f2_bin + 1], fin - f1_bin)

        # smooth, calculate the theorethical response and the SNR for V1
        spec1_smoothed = ds.circ_smooth(np.abs(spec1)**2., 16)
        Snn1 = np.abs(ds.evalTF(self.ntfs[0, 0], np.exp(
            2j * np.pi * freq)))**2 * 2 / 12 * (delta / M)**2
        snr1 = ds.calculateSNR(spec1[f1_bin:f2_bin + 1], fin - f1_bin)

        assert snr0 > 40
        assert snr1 > 40
        assert snr0 - snr1 > 40
 def test_snr_is_inf(self):
     """ Test that a paricular SNR is infinite. """
     N = self.N
     hwfft = np.zeros((N / 2, ))
     hwfft[512] = 1.0  # specially crafted to have Inf snr
     snr = ds.calculateSNR(hwfft[:N / 2], 512)
     self.assertEqual(snr, np.Inf)
 def test_snr_is_40(self):
     """ Test that a particular SNR is within roundings errors of
     40 (dB?) """
     N = self.N
     snr = ds.calculateSNR(self.hwfft[:N / 2], int(N * self.f1))
     # Consider replacing with assertAlmostEqual
     self.assertTrue(np.allclose(snr, 40, atol=1e-8, rtol=1e-8))
 def test_snr_is_inf(self):
     """ Test that a paricular SNR is infinite. """
     N = self.N
     hwfft = np.zeros((N/2, ))
     hwfft[512] = 1.0  # specially crafted to have Inf snr
     snr = ds.calculateSNR(hwfft[:N/2], 512)
     self.assertEqual(snr, np.Inf)
 def test_snr_is_40(self):
     """ Test that a particular SNR is within roundings errors of
     40 (dB?) """
     N = self.N
     snr = ds.calculateSNR(self.hwfft[:N/2], int(N*self.f1))
     # Consider replacing with assertAlmostEqual
     self.assertTrue(np.allclose(snr, 40, atol=1e-8, rtol=1e-8))
    def testMultipleQ2(self):
        """Test function for DS simulation with nq>1 2/2"""
        # filtering and simulation
        filtM1 = [0., 0., 0., 2., -1.]
        filtM2 = [1., -2., 1.]
        ntf_eq = zpk_multiply(self.ntfs[1, 1], self.ntfs[1, 1])
        M = self.nlev[0] - 1
        osr = 64
        f0 = 0.
        f1, f2 = ds.ds_f1f2(OSR=64, f0=0., complex_flag=False)
        delta = 2
        Amp = ds.undbv(-3) # Test tone amplitude, relative to full-scale.
        f = 0.3 # will be adjusted to a bin
        N = 2**12
        f1_bin = np.round(f1*N)
        f2_bin = np.round(f2*N)
        fin = np.round(((1 - f)/2*f1 + (f + 1)/2*f2) * N)
        # input sine
        t = np.arange(0, N).reshape((1, -1))
        u = Amp*M*np.cos((2*np.pi/N)*fin*t)
        vx, _, xmax, y = ds.simulateDSM(u, self.ABCD, nlev=self.nlev)
        # separate output #1 and output #2
        v1 = vx[0, :]
        v2 = vx[1, :]
        # filter and combine
        vf = lfilter(filtM1, [1.], v1) + lfilter(filtM2, [1.], v2)
        # compute the spectra
        window = ds.ds_hann(N)
        NBW = 1.5/N
        spec0 = np.fft.fft(vf*window)/(M*N/2)/ds.undbv(-6)
        spec1 = np.fft.fft(v1*window)/(M*N/2)/ds.undbv(-6)
        spec2 = np.fft.fft(v1*window)/(M*N/2)/ds.undbv(-6)
        freq = np.linspace(0, 0.5, N/2 + 1)

        # smooth, calculate the theorethical response and the SNR for VF
        spec0_smoothed = ds.circ_smooth(np.abs(spec0)**2., 16)
        Snn0 = np.abs(ds.evalTF(ntf_eq, np.exp(2j*np.pi*freq)))**2 * 2/12*(delta/M)**2
        snr0 = ds.calculateSNR(spec0[f1_bin:f2_bin + 1], fin - f1_bin)

        # smooth, calculate the theorethical response and the SNR for V1
        spec1_smoothed = ds.circ_smooth(np.abs(spec1)**2., 16)
        Snn1 = np.abs(ds.evalTF(self.ntfs[0, 0], np.exp(2j*np.pi*freq)))**2 * 2/12*(delta/M)**2
        snr1 = ds.calculateSNR(spec1[f1_bin:f2_bin + 1], fin - f1_bin)

        assert snr0 > 40
        assert snr1 > 40
        assert snr0-snr1 > 40
Beispiel #7
0
    def test_sim_noiseshaper(self):
        fmt = Q(8, 18)
        input = fmt.Signal()
        dut = Noiseshaper(input, order=8, n_lev=64)

        sim = Simulator(dut)
        sim.add_clock(1 / 100e6)

        input_hist = []
        output_hist = []
        integrators_hist = [[] for _ in dut.stages]

        n = 8192
        f_nyquist = int(np.ceil(n / (2. * dut.osr)))
        f_test = np.floor(2. / 3. * f_nyquist)
        u = dut.n_lev * 0.5 * np.sin(2 * np.pi * f_test / n * np.arange(n))

        def testbench():
            for x in u:
                yield input.eq(x)

                input_hist.append(fmt.to_float((yield input.value)))
                output_hist.append(
                    fmt.to_float((yield dut.quantized_value.value)))
                for i, integrator in enumerate(dut.stages):
                    integrators_hist[i].append(
                        fmt.to_float((yield integrator.value)))

                yield

        sim.add_sync_process(testbench)

        sim.run()

        from matplotlib import pyplot as plt
        plt.plot(np.arange(n), output_hist, linewidth=1, label="output")
        plt.plot(np.arange(n), input_hist, label="input")
        plt.legend()
        plt.show()
        for i, integrator_hist in reversed(list(enumerate(integrators_hist))):
            plt.plot(np.arange(n),
                     integrator_hist,
                     linewidth=1,
                     label="integrator {}".format(i))
        plt.legend()
        plt.show()

        import deltasigma as ds
        f = np.linspace(0, 0.5, int(n / 2. + 1))

        v, xn, xmax, y = ds.simulateDSM(u,
                                        dut.h,
                                        nlev=len(dut.quantization_values))

        spec = np.fft.fft(v * ds.ds_hann(n)) / (n / 4)
        plt.plot(f, ds.dbv(spec[:int(n / 2. + 1)]), 'b', label='Simulation DS')

        spec = np.fft.fft(output_hist * ds.ds_hann(n)) / (n / 4)
        plt.plot(f,
                 ds.dbv(spec[:int(n / 2. + 1)]),
                 'g',
                 label='Simulation HW',
                 alpha=0.7)
        ds.figureMagic([0, 0.5], 0.05, None, [-160, 0], 20, None, (16, 6),
                       'Output Spectrum')
        plt.xlabel('Normalized Frequency')
        plt.ylabel('dBFS')
        snr = ds.calculateSNR(spec[2:f_nyquist + 1], f_test - 2)
        plt.text(0.05,
                 -10,
                 'SNR = %4.1fdB @ OSR = %d' % (snr, dut.osr),
                 verticalalignment='center')
        NBW = 1.5 / n
        Sqq = 4 * ds.evalTF(dut.h, np.exp(2j * np.pi * f))**2 / 3.
        plt.plot(f, ds.dbp(Sqq * NBW), 'm', linewidth=2, label='Expected PSD')
        plt.text(0.49,
                 -90,
                 'NBW = %4.1E x $f_s$' % NBW,
                 horizontalalignment='right')
        plt.legend(loc=4)
        plt.show()

        pwm_out = py_pwm.modulate(np.array(output_hist) + 32,
                                  n_bits=6,
                                  oversampling_ratio=1)
        n = n * 64
        f = np.linspace(0, 0.5, int(n / 2. + 1))
        spec = np.fft.fft(pwm_out * ds.ds_hann(n)) / (n / 4)
        plt.plot(f, ds.dbv(spec[:int(n / 2. + 1)]), 'b', label='PWM')
        ds.figureMagic([0, 0.5], 0.05, None, [-160, 0], 20, None, (16, 6),
                       'Output Spectrum')
        plt.xlabel('Normalized Frequency')
        plt.ylabel('dBFS')
        snr = ds.calculateSNR(spec[2:f_nyquist + 1], f_test - 2)
        plt.text(0.05,
                 -10,
                 'SNR = %4.1fdB @ OSR = %d' % (snr, dut.osr),
                 verticalalignment='center')
        plt.legend(loc=4)
        plt.show()