Example #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 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
 def test_plotSpectrum(self):
     """Test function for plotSpectrum()"""
     f0 = 0
     osr = 32
     quadrature = False
     Hinf = 1.5
     order = 3
     ntf = ds.synthesizeNTF(order, osr, 0, Hinf, f0)
     f1, f2 = ds.ds_f1f2(osr, f0, quadrature)
     delta = 2
     Amp = ds.undbv(-3)
     f = 0.3
     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)
     t = np.arange(0, N)
     u = Amp*np.cos((2*np.pi/N)*fin*t)
     v, xn, xmax, y = ds.simulateDSM(u, ntf, 2)
     window = ds.ds_hann(N)
     NBW = 1.5/N
     spec0 = fft(v * window)/(N/4)
     freq = np.linspace(0, 0.5, N/2 + 1)
     # plotting
     plt.subplot(211)
     plt.plot(freq, ds.dbv(spec0[:N/2 + 1]), 'c', linewidth=1, label='$S$')
     plt.hold(True)
     spec_smoothed = ds.circ_smooth(np.abs(spec0)**2., 16)
     plt.plot(freq, ds.dbp(spec_smoothed[:N/2 + 1]), 'b--', linewidth=2,
              label='$\\mathrm{circ\\_smooth}(S)$')
     ds.plotSpectrum(spec0, fin, 'r', linewidth=2,
                     label='$\\mathrm{plotSpectrum}(S)$')
     Snn = np.abs(ds.evalTF(ntf, np.exp(2j*np.pi*freq)))**2 * 2/12*(delta)**2
     plt.plot(freq, ds.dbp(Snn*NBW), 'm', linewidth=1.5,
              label='$\mathrm{from\\ NTF}$')
     plt.text(0.5, -3, 'NBW = %.1e ' % NBW, horizontalalignment='right',
              verticalalignment='top')
     ds.figureMagic((0, 0.5), None, None, (-140, 0), 20, None)
     plt.ylabel('Spectrum [dB]')
     ax = plt.gca()
     ax.set_title('Smoothing and plotting for LOG and LIN axes')
     plt.legend(loc=4)
     plt.subplot(212)
     plt.plot(freq, ds.dbv(spec0[:N/2 + 1]), 'c', linewidth=1, label='$S$')
     plt.hold(True)
     ds.plotSpectrum(spec0, fin, '--r', linewidth=2,
                     label='$\\mathrm{plotSpectrum}(S)$')
     plt.plot(freq, ds.dbp(spec_smoothed[:N/2 + 1]), 'b', linewidth=2,
              label='$\\mathrm{circ\\_smooth}(S)$')
     plt.plot(freq, ds.dbp(Snn*NBW), 'm', linewidth=1.5,
              label='$\mathrm{from\\ NTF}$')
     plt.text(0.5, -3, 'NBW = %.1e ' % NBW, horizontalalignment='right',
              verticalalignment='top')
     ds.figureMagic((0, 0.5), None, None, (-140, 0), 20, None)
     ax = plt.gca()
     ax.set_xscale('linear')
     plt.ylabel('Spectrum [dB]')
     plt.xlabel('Normalized frequency ($f_s \\rightarrow 1$)')
     plt.legend(loc=4)
Example #4
0
 def test_plotSpectrum(self):
     """Test function for plotSpectrum()"""
     f0 = 0
     osr = 32
     quadrature = False
     Hinf = 1.5
     order = 3
     ntf = ds.synthesizeNTF(order, osr, 0, Hinf, f0)
     f1, f2 = ds.ds_f1f2(osr, f0, quadrature)
     delta = 2
     Amp = ds.undbv(-3)
     f = 0.3
     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)
     t = np.arange(0, N)
     u = Amp * np.cos((2 * np.pi / N) * fin * t)
     v, xn, xmax, y = ds.simulateDSM(u, ntf, 2)
     window = ds.ds_hann(N)
     NBW = 1.5 / N
     spec0 = fft(v * window) / (N / 4)
     freq = np.linspace(0, 0.5, N // 2 + 1)
     # plotting
     plt.subplot(211)
     plt.plot(freq,
              ds.dbv(spec0[:N // 2 + 1]),
              'c',
              linewidth=1,
              label='$S$')
     #plt.hold(True)
     spec_smoothed = ds.circ_smooth(np.abs(spec0)**2., 16)
     plt.plot(freq,
              ds.dbp(spec_smoothed[:N // 2 + 1]),
              'b--',
              linewidth=2,
              label='$\\mathrm{circ\\_smooth}(S)$')
     ds.plotSpectrum(spec0,
                     fin,
                     'r',
                     linewidth=2,
                     label='$\\mathrm{plotSpectrum}(S)$')
     Snn = np.abs(ds.evalTF(ntf, np.exp(
         2j * np.pi * freq)))**2 * 2 / 12 * (delta)**2
     plt.plot(freq,
              ds.dbp(Snn * NBW),
              'm',
              linewidth=1.5,
              label='$\\mathrm{from\\ NTF}$')
     plt.text(0.5,
              -3,
              'NBW = %.1e ' % NBW,
              horizontalalignment='right',
              verticalalignment='top')
     ds.figureMagic((0, 0.5), None, None, (-140, 0), 20, None)
     plt.ylabel('Spectrum [dB]')
     ax = plt.gca()
     ax.set_title('Smoothing and plotting for LOG and LIN axes')
     plt.legend(loc=4)
     plt.subplot(212)
     plt.plot(freq,
              ds.dbv(spec0[:N // 2 + 1]),
              'c',
              linewidth=1,
              label='$S$')
     #plt.hold(True)
     ds.plotSpectrum(spec0,
                     fin,
                     '--r',
                     linewidth=2,
                     label='$\\mathrm{plotSpectrum}(S)$')
     plt.plot(freq,
              ds.dbp(spec_smoothed[:N // 2 + 1]),
              'b',
              linewidth=2,
              label='$\\mathrm{circ\\_smooth}(S)$')
     plt.plot(freq,
              ds.dbp(Snn * NBW),
              'm',
              linewidth=1.5,
              label='$\\mathrm{from\\ NTF}$')
     plt.text(0.5,
              -3,
              'NBW = %.1e ' % NBW,
              horizontalalignment='right',
              verticalalignment='top')
     ds.figureMagic((0, 0.5), None, None, (-140, 0), 20, None)
     ax = plt.gca()
     ax.set_xscale('linear')
     plt.ylabel('Spectrum [dB]')
     plt.xlabel('Normalized frequency ($f_s \\rightarrow 1$)')
     plt.legend(loc=4)
 def test_circ_smooth(self):
     A = np.arange(1, 101)
     b = ds.circ_smooth(A, 16)
     self.assertTrue(np.allclose(self.bt, b, atol=1e-8, rtol=1e-5))