Пример #1
0
 def test_interleaving(self):
     # Takes 3 streams (a, b and c)
     # Outputs 2 streams.
     # First (d) is interleaving of a and b.
     # Second (e) is interleaving of a and b and c.  c is taken in
     #     chunks of 2 which are reversed.
     A = (1, 2, 3, 4, 5)
     B = (11, 12, 13, 14, 15)
     C = (99, 98, 97, 96, 95, 94, 93, 92, 91, 90)
     expected_D = (1, 11, 2, 12, 3, 13, 4, 14, 5, 15)
     expected_E = (1, 11, 98, 99, 2, 12, 96, 97, 3, 13, 94, 95, 4, 14, 92,
                   93, 5, 15, 90, 91)
     mapping = [
         [(0, 0), (1, 0)],  # mapping to produce D
         [(0, 0), (1, 0), (2, 1), (2, 0)],  # mapping to produce E
     ]
     srcA = gr.vector_source_f(A, False, 1)
     srcB = gr.vector_source_f(B, False, 1)
     srcC = gr.vector_source_f(C, False, 2)
     vmap = gr.vector_map(gr.sizeof_int, (1, 1, 2), mapping)
     dstD = gr.vector_sink_f(2)
     dstE = gr.vector_sink_f(4)
     self.tb.connect(srcA, (vmap, 0))
     self.tb.connect(srcB, (vmap, 1))
     self.tb.connect(srcC, (vmap, 2))
     self.tb.connect((vmap, 0), dstD)
     self.tb.connect((vmap, 1), dstE)
     self.tb.run()
     self.assertEqual(expected_D, dstD.data())
     self.assertEqual(expected_E, dstE.data())
Пример #2
0
    def test_stretch_01(self):
        tb = self.tb

        data = 10*[1,]
        data0 = map(lambda x: x/20.0, data)
        data1 = map(lambda x: x/10.0, data)

        expected_result0 = 10*[0.05,]
        expected_result1 = 10*[0.1,]

        src0 = gr.vector_source_f(data0, False)
        src1 = gr.vector_source_f(data1, False)
        inter = gr.streams_to_vector(gr.sizeof_float, 2)
        op = blocks.stretch_ff(0.1, 2)
        deinter = gr.vector_to_streams(gr.sizeof_float, 2)
        dst0 = gr.vector_sink_f()
        dst1 = gr.vector_sink_f()
        
        tb.connect(src0, (inter,0))
        tb.connect(src1, (inter,1))
        tb.connect(inter, op)
        tb.connect(op, deinter)
        tb.connect((deinter,0), dst0)
        tb.connect((deinter,1), dst1)
        tb.run()

        dst0_data = dst0.data()
        dst1_data = dst1.data()

        self.assertFloatTuplesAlmostEqual(expected_result0, dst0_data, 4)
        self.assertFloatTuplesAlmostEqual(expected_result1, dst1_data, 4)
Пример #3
0
    def __init__(self,
                 N,
                 sps,
                 rolloff,
                 ntaps,
                 bw,
                 noise,
                 foffset,
                 toffset,
                 poffset,
                 mode=0):
        gr.top_block.__init__(self)

        rrc_taps = gr.firdes.root_raised_cosine(sps, sps, 1.0, rolloff, ntaps)

        gain = 2 * scipy.pi / 100.0
        nfilts = 32
        rrc_taps_rx = gr.firdes.root_raised_cosine(nfilts, sps * nfilts, 1.0,
                                                   rolloff, ntaps * nfilts)

        data = 2.0 * scipy.random.randint(0, 2, N) - 1.0
        data = scipy.exp(1j * poffset) * data

        self.src = gr.vector_source_c(data.tolist(), False)
        self.rrc = gr.interp_fir_filter_ccf(sps, rrc_taps)
        self.chn = gr.channel_model(noise, foffset, toffset)
        self.off = gr.fractional_interpolator_cc(0.20, 1.0)

        if mode == 0:
            self.clk = gr.pfb_clock_sync_ccf(sps, gain, rrc_taps_rx, nfilts,
                                             nfilts // 2, 3.5)
            self.taps = self.clk.get_taps()
            self.dtaps = self.clk.get_diff_taps()

            self.vsnk_err = gr.vector_sink_f()
            self.vsnk_rat = gr.vector_sink_f()
            self.vsnk_phs = gr.vector_sink_f()

            self.connect((self.clk, 1), self.vsnk_err)
            self.connect((self.clk, 2), self.vsnk_rat)
            self.connect((self.clk, 3), self.vsnk_phs)

        else:  # mode == 1
            mu = 0.5
            gain_mu = 0.1
            gain_omega = 0.25 * gain_mu * gain_mu
            omega_rel_lim = 0.02
            self.clk = digital.clock_recovery_mm_cc(sps, gain_omega, mu,
                                                    gain_mu, omega_rel_lim)

            self.vsnk_err = gr.vector_sink_f()

            self.connect((self.clk, 1), self.vsnk_err)

        self.vsnk_src = gr.vector_sink_c()
        self.vsnk_clk = gr.vector_sink_c()

        self.connect(self.src, self.rrc, self.chn, self.off, self.clk,
                     self.vsnk_clk)
        self.connect(self.off, self.vsnk_src)
Пример #4
0
    def __init__(self, N, sps, rolloff, ntaps, bw, noise, foffset, toffset, poffset):
        gr.top_block.__init__(self)

        rrc_taps = gr.firdes.root_raised_cosine(
            sps, sps, 1.0, rolloff, ntaps)

        data = 2.0*scipy.random.randint(0, 2, N) - 1.0
        data = scipy.exp(1j*poffset) * data

        self.src = gr.vector_source_c(data.tolist(), False)
        self.rrc = filter.interp_fir_filter_ccf(sps, rrc_taps)
        self.chn = filter.channel_model(noise, foffset, toffset)
        self.fll = digital.fll_band_edge_cc(sps, rolloff, ntaps, bw)

        self.vsnk_src = gr.vector_sink_c()
        self.vsnk_fll = gr.vector_sink_c()
        self.vsnk_frq = gr.vector_sink_f()
        self.vsnk_phs = gr.vector_sink_f()
        self.vsnk_err = gr.vector_sink_f()

        self.connect(self.src, self.rrc, self.chn, self.fll, self.vsnk_fll)
        self.connect(self.rrc, self.vsnk_src)
        self.connect((self.fll,1), self.vsnk_frq)
        self.connect((self.fll,2), self.vsnk_phs)
        self.connect((self.fll,3), self.vsnk_err)
Пример #5
0
 def test_interleaving(self):
     # Takes 3 streams (a, b and c)
     # Outputs 2 streams.
     # First (d) is interleaving of a and b.
     # Second (e) is interleaving of a and b and c.  c is taken in
     #     chunks of 2 which are reversed.
     A = (1, 2, 3, 4, 5)
     B = (11, 12, 13, 14, 15)
     C = (99, 98, 97, 96, 95, 94, 93, 92, 91, 90)
     expected_D = (1, 11, 2, 12, 3, 13, 4, 14, 5, 15)
     expected_E = (1, 11, 98, 99, 2, 12, 96, 97, 3, 13, 94, 95,
                   4, 14, 92, 93, 5, 15, 90, 91)
     mapping = [[(0, 0), (1, 0)], # mapping to produce D
                [(0, 0), (1, 0), (2, 1), (2, 0)], # mapping to produce E
                ]
     srcA = gr.vector_source_f(A, False, 1)
     srcB = gr.vector_source_f(B, False, 1)
     srcC = gr.vector_source_f(C, False, 2)
     vmap =  gr.vector_map(gr.sizeof_int, (1, 1, 2), mapping)
     dstD = gr.vector_sink_f(2)
     dstE = gr.vector_sink_f(4)
     self.tb.connect(srcA, (vmap, 0))
     self.tb.connect(srcB, (vmap, 1)) 
     self.tb.connect(srcC, (vmap, 2)) 
     self.tb.connect((vmap, 0), dstD)
     self.tb.connect((vmap, 1), dstE)
     self.tb.run()
     self.assertEqual(expected_D, dstD.data())
     self.assertEqual(expected_E, dstE.data())
Пример #6
0
    def test_deint_001(self):
        lenx = 64
        src = gr.vector_source_f(range(lenx))
        op = gr.deinterleave(gr.sizeof_float)
        dst0 = gr.vector_sink_f()
        dst1 = gr.vector_sink_f()
        dst2 = gr.vector_sink_f()
        dst3 = gr.vector_sink_f()

        self.tb.connect(src, op)
        self.tb.connect((op, 0), dst0)
        self.tb.connect((op, 1), dst1)
        self.tb.connect((op, 2), dst2)
        self.tb.connect((op, 3), dst3)
        self.tb.run()

        expected_result0 = tuple(range(0, lenx, 4))
        expected_result1 = tuple(range(1, lenx, 4))
        expected_result2 = tuple(range(2, lenx, 4))
        expected_result3 = tuple(range(3, lenx, 4))

        self.assertFloatTuplesAlmostEqual(expected_result0, dst0.data())
        self.assertFloatTuplesAlmostEqual(expected_result1, dst1.data())
        self.assertFloatTuplesAlmostEqual(expected_result2, dst2.data())
        self.assertFloatTuplesAlmostEqual(expected_result3, dst3.data())
Пример #7
0
    def test_deint_001 (self):
        lenx = 64
        src = gr.vector_source_f (range (lenx))
        op = gr.deinterleave (gr.sizeof_float)
        dst0 = gr.vector_sink_f ()
        dst1 = gr.vector_sink_f ()
        dst2 = gr.vector_sink_f ()
        dst3 = gr.vector_sink_f ()

        self.tb.connect (src, op)
        self.tb.connect ((op, 0), dst0)
        self.tb.connect ((op, 1), dst1)
        self.tb.connect ((op, 2), dst2)
        self.tb.connect ((op, 3), dst3)
        self.tb.run ()

        expected_result0 = tuple (range (0, lenx, 4))
        expected_result1 = tuple (range (1, lenx, 4))
        expected_result2 = tuple (range (2, lenx, 4))
        expected_result3 = tuple (range (3, lenx, 4))

        self.assertFloatTuplesAlmostEqual (expected_result0, dst0.data ())
        self.assertFloatTuplesAlmostEqual (expected_result1, dst1.data ())
        self.assertFloatTuplesAlmostEqual (expected_result2, dst2.data ())
        self.assertFloatTuplesAlmostEqual (expected_result3, dst3.data ())
    def test_deint_001 (self):
        lenx = 64
        src0 = gr.vector_source_f (range (lenx))
        op = gr.deinterleave (gr.sizeof_float,4)
        dst0 = gr.vector_sink_f ()
        dst1 = gr.vector_sink_f ()
        dst2 = gr.vector_sink_f ()
        dst3 = gr.vector_sink_f ()

        self.tb.connect (src0, op)
        op.connect(dst0,usesPortName="float_out_1")
        op.connect(dst1,usesPortName="float_out_2")
        op.connect(dst2,usesPortName="float_out_3")
        op.connect(dst3,usesPortName="float_out_4")
        self.tb.run ()

        expected_result0 = tuple (range (0, lenx, 4))
        expected_result1 = tuple (range (1, lenx, 4))
        expected_result2 = tuple (range (2, lenx, 4))
        expected_result3 = tuple (range (3, lenx, 4))

        self.assertFloatTuplesAlmostEqual (expected_result0, dst0.data())
        self.assertFloatTuplesAlmostEqual (expected_result1, dst1.data())
        self.assertFloatTuplesAlmostEqual (expected_result2, dst2.data ())
        self.assertFloatTuplesAlmostEqual (expected_result3, dst3.data ())
    def test_deint_001(self):
        lenx = 64
        src0 = gr.vector_source_f(range(lenx))
        op = gr.deinterleave(gr.sizeof_float, 4)
        dst0 = gr.vector_sink_f()
        dst1 = gr.vector_sink_f()
        dst2 = gr.vector_sink_f()
        dst3 = gr.vector_sink_f()

        self.tb.connect(src0, op)
        op.connect(dst0, usesPortName="float_out_1")
        op.connect(dst1, usesPortName="float_out_2")
        op.connect(dst2, usesPortName="float_out_3")
        op.connect(dst3, usesPortName="float_out_4")
        self.tb.run()

        expected_result0 = tuple(range(0, lenx, 4))
        expected_result1 = tuple(range(1, lenx, 4))
        expected_result2 = tuple(range(2, lenx, 4))
        expected_result3 = tuple(range(3, lenx, 4))

        self.assertFloatTuplesAlmostEqual(expected_result0, dst0.data())
        self.assertFloatTuplesAlmostEqual(expected_result1, dst1.data())
        self.assertFloatTuplesAlmostEqual(expected_result2, dst2.data())
        self.assertFloatTuplesAlmostEqual(expected_result3, dst3.data())
Пример #10
0
    def __init__(self, N, sps, rolloff, ntaps, bw, noise, foffset, toffset, poffset):
        gr.top_block.__init__(self)

        rrc_taps = gr.firdes.root_raised_cosine(
            sps, sps, 1.0, rolloff, ntaps)

        data = 2.0*scipy.random.randint(0, 2, N) - 1.0
        data = scipy.exp(1j*poffset) * data

        self.src = gr.vector_source_c(data.tolist(), False)
        self.rrc = gr.interp_fir_filter_ccf(sps, rrc_taps)
        self.chn = gr.channel_model(noise, foffset, toffset)
        self.fll = digital.fll_band_edge_cc(sps, rolloff, ntaps, bw)

        self.vsnk_src = gr.vector_sink_c()
        self.vsnk_fll = gr.vector_sink_c()
        self.vsnk_frq = gr.vector_sink_f()
        self.vsnk_phs = gr.vector_sink_f()
        self.vsnk_err = gr.vector_sink_f()

        self.connect(self.src, self.rrc, self.chn, self.fll, self.vsnk_fll)
        self.connect(self.rrc, self.vsnk_src)
        self.connect((self.fll,1), self.vsnk_frq)
        self.connect((self.fll,2), self.vsnk_phs)
        self.connect((self.fll,3), self.vsnk_err)
Пример #11
0
    def __init__(self, N, sps, rolloff, ntaps, bw, noise,
                 foffset, toffset, poffset, mode=0):
        gr.top_block.__init__(self)

        rrc_taps = gr.firdes.root_raised_cosine(
            sps, sps, 1.0, rolloff, ntaps)

        gain = 2*scipy.pi/100.0
        nfilts = 32
        rrc_taps_rx = gr.firdes.root_raised_cosine(
            nfilts, sps*nfilts, 1.0, rolloff, ntaps*nfilts)
            
        data = 2.0*scipy.random.randint(0, 2, N) - 1.0
        data = scipy.exp(1j*poffset) * data

        self.src = gr.vector_source_c(data.tolist(), False)
        self.rrc = gr.interp_fir_filter_ccf(sps, rrc_taps)
        self.chn = gr.channel_model(noise, foffset, toffset)
        self.off = gr.fractional_interpolator_cc(0.20, 1.0)

        if mode == 0:
            self.clk = gr.pfb_clock_sync_ccf(sps, gain, rrc_taps_rx,
                                             nfilts, nfilts//2, 3.5)
            self.taps = self.clk.get_taps()
            self.dtaps = self.clk.get_diff_taps()

            self.vsnk_err = gr.vector_sink_f()
            self.vsnk_rat = gr.vector_sink_f()
            self.vsnk_phs = gr.vector_sink_f()

            self.connect((self.clk,1), self.vsnk_err)
            self.connect((self.clk,2), self.vsnk_rat)
            self.connect((self.clk,3), self.vsnk_phs)
            
        else: # mode == 1
            mu = 0.5
            gain_mu = 0.1
            gain_omega = 0.25*gain_mu*gain_mu
            omega_rel_lim = 0.02
            self.clk = digital.clock_recovery_mm_cc(sps, gain_omega,
                                                    mu, gain_mu,
                                                    omega_rel_lim)

            self.vsnk_err = gr.vector_sink_f()

            self.connect((self.clk,1), self.vsnk_err)

        self.vsnk_src = gr.vector_sink_c()
        self.vsnk_clk = gr.vector_sink_c()

        self.connect(self.src, self.rrc, self.chn, self.off, self.clk, self.vsnk_clk)
        self.connect(self.off, self.vsnk_src)
 def test_010_run(self):
     expected = (1.0, 2.0, 3.0, 4.0)
     hblock = gr.top_block("test_block")
     src = gr.vector_source_f(expected, False)
     sink1 = gr.vector_sink_f()
     sink2 = gr.vector_sink_f()
     hblock.connect(src, sink1)
     hblock.connect(src, sink2)
     hblock.run()
     actual1 = sink1.data()
     actual2 = sink2.data()
     self.assertEquals(expected, actual1)
     self.assertEquals(expected, actual2)
Пример #13
0
 def test_010_run(self):
     expected = (1.0, 2.0, 3.0, 4.0)
     hblock = gr.top_block("test_block")
     src = gr.vector_source_f(expected, False)
     sink1 = gr.vector_sink_f()
     sink2 = gr.vector_sink_f()
     hblock.connect(src, sink1)
     hblock.connect(src, sink2)
     hblock.run()
     actual1 = sink1.data()
     actual2 = sink2.data()
     self.assertEquals(expected, actual1)
     self.assertEquals(expected, actual2)
Пример #14
0
 def test_complex_to_float_2(self):
     src_data = (1 + 2j, 3 + 4j, 5 + 6j, 7 + 8j, 9 + 10j)
     expected_data1 = (1.0, 3.0, 5.0, 7.0, 9.0)
     expected_data2 = (2.0, 4.0, 6.0, 8.0, 10.0)
     src = gr.vector_source_c(src_data)
     op = blocks_swig.complex_to_float()
     dst1 = gr.vector_sink_f()
     dst2 = gr.vector_sink_f()
     self.tb.connect(src, op)
     self.tb.connect((op, 0), dst1)
     self.tb.connect((op, 1), dst2)
     self.tb.run()
     self.assertFloatTuplesAlmostEqual(expected_data1, dst1.data())
     self.assertFloatTuplesAlmostEqual(expected_data2, dst2.data())
Пример #15
0
 def test_complex_to_float_2(self):
     src_data = (1+2j, 3+4j, 5+6j, 7+8j, 9+10j)
     expected_data1 = (1.0, 3.0, 5.0, 7.0, 9.0)
     expected_data2 = (2.0, 4.0, 6.0, 8.0, 10.0)
     src = gr.vector_source_c(src_data)
     op = blocks_swig.complex_to_float()
     dst1 = gr.vector_sink_f()
     dst2 = gr.vector_sink_f()
     self.tb.connect(src, op)
     self.tb.connect((op, 0), dst1)
     self.tb.connect((op, 1), dst2)
     self.tb.run()
     self.assertFloatTuplesAlmostEqual(expected_data1, dst1.data())
     self.assertFloatTuplesAlmostEqual(expected_data2, dst2.data())
Пример #16
0
    def test__002_t(self):
        """
        Generate a signal with SNR as given below.
        Calculate the RMSE.
        """
        nsamples = 1024
        n_trials = 100
        samp_rate = 32000
        SNR = 20  # in dB
        self.siggen = siggen.signal_generator(n_sinusoids=1,
                                              SNR=SNR,
                                              samp_rate=samp_rate,
                                              nsamples=nsamples * n_trials)

        self.stream = gr.stream_to_vector(gr.sizeof_gr_complex, nsamples)
        self.esprit = specest.esprit_vcf(n=1, m=64, nsamples=nsamples)
        self.sink = gr.vector_sink_f(vlen=1)
        # wire it up ...
        self.tb.connect(self.siggen, self.stream, self.esprit, self.sink)
        self.tb.run()
        MSE = 0.0
        omega = self.siggen.omegas()[0]
        for i in range(n_trials):
            MSE += (omega - self.sink.data()[i])**2.0
        print '\n' + 70 * '-'
        print 'Testing specest_esprit_vcf ...'
        print 'Ran %u trials to estimate the frequency' % n_trials
        print 'Used %u samples to estimate the frequency' % nsamples
        print 'Sampling rate %s' % eng_notation.num_to_str(samp_rate)
        print 'SNR of %u dB' % SNR
        print 'Root mean square error %g' % numpy.sqrt(MSE / n_trials)
        print 'Cramer-Rao Bound %g' % numpy.sqrt(
            6 / 10**(SNR / 10.0) / nsamples**3)
        print 70 * '-'
    def test_fff_000(self):
        N = 1000         # number of samples to use
        fs = 1000        # baseband sampling rate
        rrate = 1.123    # resampling rate

        nfilts = 32

        freq = 100
        signal = gr.sig_source_f(fs, gr.GR_SIN_WAVE, freq, 1)
        head = gr.head(gr.sizeof_float, N)
        pfb = filter.pfb_arb_resampler_fff(rrate, taps)
        snk = gr.vector_sink_f()

        self.tb.connect(signal, head, pfb, snk)
        self.tb.run() 

        Ntest = 50
        L = len(snk.data())
        t = map(lambda x: float(x)/(fs*rrate), xrange(L))

        phase = 0.53013
        expected_data = map(lambda x: math.sin(2.*math.pi*freq*x+phase), t)

        dst_data = snk.data()
        self.assertFloatTuplesAlmostEqual(expected_data[-Ntest:], dst_data[-Ntest:], 3)
Пример #18
0
    def test_003(self):
        stream = [[float(0)] * 10, [float(1)] * 10, [float(2)] * 10]
        mux = [
            0, 2, 2, 1, 1, 2, 0, 2, 0, 0, 1, 0, 1, 0, 1, 2, 1, 2, 1, 1, 2, 0,
            0, 2, 1, 2, 0, 1, 2, 0
        ]

        imux = []
        for x in mux:
            imux.append(int(x))

        data = [
            gr.vector_source_f(stream[0]),
            gr.vector_source_f(stream[1]),
            gr.vector_source_f(stream[2])
        ]
        dst = gr.vector_sink_f()

        uut = ofdm.static_mux_v(gr.sizeof_float, imux)

        self.fg.connect(data[0], (uut, 0))
        self.fg.connect(data[1], (uut, 1))
        self.fg.connect(data[2], (uut, 2))

        self.fg.connect(uut, dst)
        self.fg.run()

        self.assertEqual(map(float, mux), list(dst.data()))
Пример #19
0
 def test_001_detect(self):
     """ Send two bursts, with zeros in between, and check
     they are both detected at the correct position and no
     false alarms occur """
     n_zeros = 15
     fft_len = 32
     cp_len = 4
     sig_len = (fft_len + cp_len) * 10
     sync_symbol = [(random.randint(0, 1) * 2) - 1
                    for x in range(fft_len / 2)] * 2
     tx_signal = [0,] * n_zeros + \
                 sync_symbol[-cp_len:] + \
                 sync_symbol + \
                 [(random.randint(0, 1)*2)-1 for x in range(sig_len)]
     tx_signal = tx_signal * 2
     add = gr.add_cc()
     sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len)
     sink_freq = gr.vector_sink_f()
     sink_detect = gr.vector_sink_b()
     self.tb.connect(gr.vector_source_c(tx_signal), (add, 0))
     self.tb.connect(gr.noise_source_c(gr.GR_GAUSSIAN, .005), (add, 1))
     self.tb.connect(add, sync)
     self.tb.connect((sync, 0), sink_freq)
     self.tb.connect((sync, 1), sink_detect)
     self.tb.run()
     sig1_detect = sink_detect.data()[0:len(tx_signal) / 2]
     sig2_detect = sink_detect.data()[len(tx_signal) / 2:]
     self.assertTrue(
         abs(sig1_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len)
     self.assertTrue(
         abs(sig2_detect.index(1) - (n_zeros + fft_len + cp_len)) < cp_len)
     self.assertEqual(numpy.sum(sig1_detect), 1)
     self.assertEqual(numpy.sum(sig2_detect), 1)
Пример #20
0
    def test_002(self):
        udp_rcv = gr.udp_source( gr.sizeof_float, '0.0.0.0', 0, eof=False )
        rcv_port = udp_rcv.get_port()

        udp_snd = gr.udp_sink( gr.sizeof_float, '127.0.0.1', 65500 )
        udp_snd.connect( 'localhost', rcv_port )

        n_data = 16
        src_data = [float(x) for x in range(n_data)]
        expected_result = tuple(src_data)
        src = gr.vector_source_f(src_data)
        dst = gr.vector_sink_f()

        self.tb_snd.connect( src, udp_snd )
        self.tb_rcv.connect( udp_rcv, dst )

        self.tb_rcv.start()
        self.tb_snd.run()
        udp_snd.disconnect()
        self.timeout = False
        q = Timer(3.0,self.stop_rcv)
        q.start()
        self.tb_rcv.wait()
        q.cancel()

        result_data = dst.data()
        self.assertEqual(expected_result, result_data)
        self.assert_(self.timeout)  # source ignores EOF?
Пример #21
0
    def test_quad_demod_001(self):
        f = 1000.0
        fs = 8000.0

        src_data = []
        for i in xrange(200):
            ti = i / fs
            src_data.append(cmath.exp(2j * cmath.pi * f * ti))

        # f/fs is a quarter turn per sample.
        # Set the gain based on this to get 1 out.
        gain = 1.0 / (cmath.pi / 4)

        expected_result = [
            0,
        ] + 199 * [1.0]

        src = gr.vector_source_c(src_data)
        op = analog.quadrature_demod_cf(gain)
        dst = gr.vector_sink_f()

        self.tb.connect(src, op)
        self.tb.connect(op, dst)
        self.tb.run()

        result_data = dst.data()
        self.assertComplexTuplesAlmostEqual(expected_result, result_data, 5)
    def test_001_ff(self):
        N = 10000        # number of samples to use
        fs = 1000        # baseband sampling rate
        rrate = 1.123    # resampling rate

        freq = 10
        signal = gr.sig_source_f(fs, gr.GR_SIN_WAVE, freq, 1)
        head = gr.head(gr.sizeof_float, N)
        op = filter.fractional_interpolator_ff(0, rrate)
        snk = gr.vector_sink_f()

        self.tb.connect(signal, head, op, snk)
        self.tb.run() 
        import time
        time.sleep(2)
        
        Ntest = 5000
        L = len(snk.data())
        t = map(lambda x: float(x)/(fs/rrate), xrange(L))

        phase = 0.1884
        expected_data = map(lambda x: math.sin(2.*math.pi*freq*x+phase), t)
        dst_data = snk.data()

        self.assertFloatTuplesAlmostEqual(expected_data[-Ntest:], dst_data[-Ntest:], 3)
Пример #23
0
 def test_complex_to_float_2 (self):
     src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
     expected_result0 = (0, 1, -1, 3, -3, -3)
     expected_result1 = (0, 0, 0, 4, -4, 4)
     src = gr.vector_source_c (src_data)
     op = gr.complex_to_float ()
     dst0 = gr.vector_sink_f ()
     dst1 = gr.vector_sink_f ()
     self.tb.connect (src, op)
     self.tb.connect ((op, 0), dst0)
     self.tb.connect ((op, 1), dst1)
     self.tb.run ()
     actual_result = dst0.data ()
     self.assertFloatTuplesAlmostEqual (expected_result0, actual_result)
     actual_result = dst1.data ()
     self.assertFloatTuplesAlmostEqual (expected_result1, actual_result)
Пример #24
0
    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Top Block")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.plot_sink_0 = plot_sink.plot_sink_f(
            self.GetWin(),
            title="Scope Plot",
            vlen=1,
            decim=1,
        )
        self.Add(self.plot_sink_0.win)
        self.gr_vector_sink_x_0 = gr.vector_sink_f(1)
        self.gr_serial_0 = gr_ser.ser()
        self.gr_serial_0.set_parameters("/dev/ttyACM1", 9600, 8, "N", 1)

        self.const_source_x_0 = gr.sig_source_f(0, gr.GR_CONST_WAVE, 0, 0, 1)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.gr_serial_0, 0), (self.gr_vector_sink_x_0, 0))
        self.connect((self.gr_serial_0, 0), (self.plot_sink_0, 0))
        self.connect((self.const_source_x_0, 0), (self.gr_serial_0, 0))
Пример #25
0
    def test_001 (self):
        src_data = (1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1)

        expected_data = (   0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 1.384, 0.794, 1.384, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.794, 1.384, 2.411, 1.384, 0.794, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.794, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
                            0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000  )
        Np = 4
        P = 4
        L = 2
        src = gr.vector_source_c(src_data, False)
        cyclo_fam = specest.cyclo_fam(Np, P, L)

        sink = gr.vector_sink_f(2*Np)
        self.tb.connect(src, cyclo_fam, sink)
        self.tb.run()
        estimated_data =  sink.data()[-2*P*L*(2*Np):]
        self.assertFloatTuplesAlmostEqual(expected_data,estimated_data,3)
Пример #26
0
    def __init__(self, dBm, pfa, pfd, useless_bw, plot_histogram):
        gr.top_block.__init__(self)

        # Constants
        samp_rate = 2.4e6
        samples_per_band = 16
        tcme = 1.9528
        output_pfa = True
        debug_stats = False
        self.histogram = plot_histogram
        primary_user_location = 5
        mu = 0
        fft_size = 4096
        nframes_to_check = 1
        nframes_to_average = 1
        downconverter = 1

        src_data = self.generateRandomSignalSource(
            dBm, fft_size, mu, nframes_to_check * nframes_to_average)

        # Blocks
        src = gr.vector_source_c(src_data)
        s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
        fftb = fft.fft_vcc(fft_size, True, (window.blackmanharris(1024)),
                           False, 1)
        self.ss = howto.spectrum_sensing_cf(samp_rate, fft_size,
                                            samples_per_band, pfd, pfa, tcme,
                                            output_pfa, debug_stats,
                                            primary_user_location, useless_bw,
                                            self.histogram, nframes_to_check,
                                            nframes_to_average, downconverter)
        self.sink = gr.vector_sink_f()

        # Connections
        self.connect(src, s2v, fftb, self.ss, self.sink)
Пример #27
0
    def xtest_fff_004(self):
        random.seed(0)
        for i in xrange(25):
            sys.stderr.write("\n>>> Loop = %d\n" % (i,))
            src_len = 4096
            src_data = make_random_float_tuple(src_len)
            ntaps = int(random.uniform(2, 1000))
            taps = make_random_float_tuple(ntaps)
            expected_result = reference_filter_fff(1, taps, src_data)

            src = gr.vector_source_f(src_data)
            op = gr.fft_filter_fff(1, taps)
            dst = gr.vector_sink_f()
    	    tb = gr.top_block()
            tb.connect(src, op, dst)
            tb.run()
            result_data = dst.data()

            #print "src_len =", src_len, " ntaps =", ntaps
            try:
                self.assert_fft_float_ok2(expected_result, result_data, abs_eps=1.0)
            except:
                expected = open('expected', 'w')
                for x in expected_result:
                    expected.write(`x` + '\n')
                actual = open('actual', 'w')
                for x in result_data:
                    actual.write(`x` + '\n')
                raise
Пример #28
0
 def test_complex_to_float_2(self):
     src_data = (0, 1, -1, 3 + 4j, -3 - 4j, -3 + 4j)
     expected_result0 = (0, 1, -1, 3, -3, -3)
     expected_result1 = (0, 0, 0, 4, -4, 4)
     src = gr.vector_source_c(src_data)
     op = gr.complex_to_float()
     dst0 = gr.vector_sink_f()
     dst1 = gr.vector_sink_f()
     self.tb.connect(src, op)
     self.tb.connect((op, 0), dst0)
     self.tb.connect((op, 1), dst1)
     self.tb.run()
     actual_result = dst0.data()
     self.assertFloatTuplesAlmostEqual(expected_result0, actual_result)
     actual_result = dst1.data()
     self.assertFloatTuplesAlmostEqual(expected_result1, actual_result)
Пример #29
0
    def test_complex_to_arg (self):
        pi = math.pi
        input_data = (0, pi/6, pi/4, pi/2, 3*pi/4, 7*pi/8,
                      -pi/6, -pi/4, -pi/2, -3*pi/4, -7*pi/8)

        expected_result = (0.0,                  # 0
                           0.52382522821426392,  # pi/6
                           0.78539806604385376,  # pi/4
                           1.5707963705062866,   # pi/2
                           2.3561947345733643,   # 3pi/4
                           2.7491819858551025,   # 7pi/8
                           -0.52382522821426392, # -pi/6
                           -0.78539806604385376, # -pi/4
                           -1.5707963705062866,  # -pi/2
                           -2.3561947345733643,  # -3pi/4
                           -2.7491819858551025)  # -7pi/8

        src_data = tuple ([math.cos (x) + math.sin (x) * 1j for x in input_data])
        src = gr.vector_source_c (src_data)
        op = gr.complex_to_arg ()
        dst = gr.vector_sink_f ()
        self.tb.connect (src, op)
        self.tb.connect (op, dst)
        self.tb.run ()
        actual_result = dst.data ()

        self.assertFloatTuplesAlmostEqual (expected_result, actual_result, 5)
Пример #30
0
    def xtest_004_decim_random_vals(self):
        MAX_TAPS = 9
        MAX_DECIM = 7
        OUTPUT_LEN = 9

        random.seed(0)    # we want reproducibility

        for ntaps in xrange(1, MAX_TAPS + 1):
            for decim in xrange(1, MAX_DECIM+1):
                for ilen in xrange(ntaps + decim, ntaps + OUTPUT_LEN*decim):
                    src_data = random_floats(ilen)
                    taps = random_floats(ntaps)
                    expected_result = reference_dec_filter(src_data, decim, taps)

                    tb = gr.top_block()
                    src = gr.vector_source_f(src_data)
                    op = gr.rational_resampler_base_fff(1, decim, taps)
                    dst = gr.vector_sink_f()
                    tb.connect(src, op, dst)
                    tb.run()
                    tb = None
                    result_data = dst.data()
                    L1 = len(result_data)
                    L2 = len(expected_result)
                    L = min(L1, L2)
                    if False:
                        sys.stderr.write('delta = %2d: ntaps = %d decim = %d ilen = %d\n' % (L2 - L1, ntaps, decim, ilen))
                        sys.stderr.write('  len(result_data) = %d  len(expected_result) = %d\n' %
                                         (len(result_data), len(expected_result)))
                    self.assertEqual(expected_result[0:L], result_data[0:L])
Пример #31
0
    def xtest_fff_003(self):
        random.seed(0)
        for i in xrange(25):
            sys.stderr.write("\n>>> Loop = %d\n" % (i,))
            src_len = 4096
            src_data = make_random_float_tuple(src_len)
            ntaps = int(random.uniform(2, 1000))
            taps = make_random_float_tuple(ntaps)
            expected_result = reference_filter_fff(1, taps, src_data)

            src = gr.vector_source_f(src_data)
            op = gr.fft_filter_fff(1, taps)
            dst = gr.vector_sink_f()
    	    tb = gr.top_block()
            tb.connect(src, op, dst)
            tb.run()
            result_data = dst.data()

            #print "src_len =", src_len, " ntaps =", ntaps
            try:
                self.assert_fft_float_ok2(expected_result, result_data, abs_eps=1.0)
            except:
                expected = open('expected', 'w')
                for x in expected_result:
                    expected.write(`x` + '\n')
                actual = open('actual', 'w')
                for x in result_data:
                    actual.write(`x` + '\n')
                raise
Пример #32
0
    def test_001_t(self):
        # set up fg
        src_data1 = [0] * 100
        src_data2 = [1] * 1000
        src_data = tuple(src_data1 + src_data2)

        src = gr.vector_source_f(src_data)
        sqr = multiorder_tf()
        sqr.set_parameters(1, 0, 0, 0.5, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1,
                           1100)

        #Preload
        sqr.input_config(1).preload_items = 1
        dst = gr.vector_sink_f()

        self.tb.connect(src, sqr)
        self.tb.connect(sqr, dst)

        self.tb.run()
        # check data
        result_data = dst.data()

        import matplotlib.pyplot as plt
        plt.plot(result_data)
        plt.show()
Пример #33
0
    def test_006_interp_decim(self):
        taps = (0,1,0,0)
        src_data = range(10000)
        interp = 3
        decimation = 2

        expected_result = reference_interp_dec_filter(src_data, interp, decimation, taps)

	tb = gr.top_block()
        src = gr.vector_source_f(src_data)
        op = gr.rational_resampler_base_fff(interp, decimation, taps)
        dst = gr.vector_sink_f()
        tb.connect(src, op)
        tb.connect(op, dst)
        tb.run()
        result_data = dst.data()

        L1 = len(result_data)
        L2 = len(expected_result)
        L = min(L1, L2)
        if False:
            sys.stderr.write('delta = %2d: ntaps = %d decim = %d ilen = %d\n' %
                             (L2 - L1, len(taps), decimation, len(src_data)))
            sys.stderr.write('  len(result_data) = %d  len(expected_result) = %d\n' %
                             (len(result_data), len(expected_result)))
        self.assertEqual(expected_result[1:L], result_data[1:L])
Пример #34
0
    def test_001(self):
        port = 65500

        n_data = 16
        src_data = [float(x) for x in range(n_data)]
        expected_result = tuple(src_data)
        src = gr.vector_source_f(src_data)
        udp_snd = gr.udp_sink( gr.sizeof_float, 'localhost', port )
        self.tb_snd.connect( src, udp_snd )

        udp_rcv = gr.udp_source( gr.sizeof_float, 'localhost', port )
        dst = gr.vector_sink_f()
        self.tb_rcv.connect( udp_rcv, dst )

        self.tb_rcv.start()
        self.tb_snd.run()
        udp_snd.disconnect()
        self.timeout = False
        q = Timer(3.0,self.stop_rcv)
        q.start()
        self.tb_rcv.wait()
        q.cancel()

        result_data = dst.data()
        self.assertEqual(expected_result, result_data)
        self.assert_(not self.timeout)
Пример #35
0
    def test__002_t (self):
        """
        Generate a signal with SNR as given below.
        Calculate the RMSE.
        """
        nsamples = 1024
        n_trials = 100
        samp_rate = 32000
        SNR = 20 # in dB
        self.siggen = siggen.signal_generator(n_sinusoids = 1,
                                              SNR = SNR, samp_rate = samp_rate,
                                              nsamples = nsamples * n_trials)

        self.stream = gr.stream_to_vector(gr.sizeof_gr_complex, nsamples)
        self.esprit = specest.esprit_vcf(n=1, m=64, nsamples = nsamples)
        self.sink = gr.vector_sink_f(vlen=1)
        # wire it up ...
        self.tb.connect(self.siggen, self.stream, self.esprit, self.sink)
        self.tb.run()
        MSE = 0.0
        omega = self.siggen.omegas()[0]
        for i in range(n_trials):
            MSE += (omega - self.sink.data()[i])**2.0
        print '\n' + 70*'-'
        print 'Testing specest_esprit_vcf ...'
        print 'Ran %u trials to estimate the frequency' % n_trials
        print 'Used %u samples to estimate the frequency' % nsamples
        print 'Sampling rate %s' % eng_notation.num_to_str(samp_rate)
        print 'SNR of %u dB' % SNR
        print 'Root mean square error %g' % numpy.sqrt(MSE/n_trials)
        print 'Cramer-Rao Bound %g' % numpy.sqrt(6/10**(SNR/10.0)/nsamples**3)
        print 70*'-'
Пример #36
0
    def test_003_multiburst(self):
        """ Send several bursts, see if the number of detects is correct.
        Burst lengths and content are random.
        """
        n_bursts = 42
        fft_len = 32
        cp_len = 4
        tx_signal = []
        for i in xrange(n_bursts):
            sync_symbol = [(random.randint(0, 1) * 2) - 1
                           for x in range(fft_len / 2)] * 2
            tx_signal += [0,] * random.randint(0, 2*fft_len) + \
                         sync_symbol[-cp_len:] + \
                         sync_symbol + \
                         [(random.randint(0, 1)*2)-1 for x in range(fft_len * random.randint(5,23))]
        add = gr.add_cc()
        sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len)
        sink_freq = gr.vector_sink_f()
        sink_detect = gr.vector_sink_b()
        channel = gr.channel_model(0.005)
        self.tb.connect(gr.vector_source_c(tx_signal), channel, sync)
        self.tb.connect((sync, 0), sink_freq)
        self.tb.connect((sync, 1), sink_detect)
        self.tb.run()
        n_bursts_detected = numpy.sum(sink_detect.data())
        # We allow for one false alarm or missed burst
        self.assertTrue(
            abs(n_bursts_detected - n_bursts) <= 1,
            msg="""Because of statistics, it is possible (though unlikely)
that the number of detected bursts differs slightly. If the number of detects is
off by one or two, run the test again and see what happen.
Detection error was: %d """ % (numpy.sum(sink_detect.data()) - n_bursts))
   def __init__(self, dBm, pfa, pfd, useless_bw):
      gr.top_block.__init__(self)

      # Constants
      samp_rate = 2.4e6
      samples_per_band = 16
      tcme = 1.9528
      output_pfa = True
      debug_stats = False
      histogram = True
      primary_user_location = 0
      mu = 0
      fft_size = 16
      history = 3

      src_data = [1+1j]*fft_size*history

		# Blocks
      src = gr.vector_source_c(src_data)
      s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
      self.ss = howto.spectrum_sensing_cf(samp_rate,fft_size,samples_per_band,pfd,pfa,tcme,output_pfa,debug_stats,primary_user_location,useless_bw,histogram,history)
      self.sink = gr.vector_sink_f()

		# Connections
      self.connect(src, s2v, self.ss, self.sink)
Пример #38
0
 def test_002_freq(self):
     """ Add a fine frequency offset and see if that get's detected properly """
     fft_len = 32
     cp_len = 4
     # This frequency offset is normalized to rads, i.e. \pi == f_s/2
     max_freq_offset = 2 * numpy.pi / fft_len  # Otherwise, it's coarse
     freq_offset = ((2 * random.random()) - 1) * max_freq_offset
     sig_len = (fft_len + cp_len) * 10
     sync_symbol = [(random.randint(0, 1) * 2) - 1
                    for x in range(fft_len / 2)] * 2
     tx_signal = sync_symbol[-cp_len:] + \
                 sync_symbol + \
                 [(random.randint(0, 1)*2)-1 for x in range(sig_len)]
     mult = gr.multiply_cc()
     add = gr.add_cc()
     sync = digital.ofdm_sync_sc_cfb(fft_len, cp_len, True)
     channel = gr.channel_model(0.005, freq_offset / 2.0 / numpy.pi)
     sink_freq = gr.vector_sink_f()
     sink_detect = gr.vector_sink_b()
     self.tb.connect(gr.vector_source_c(tx_signal), channel, sync)
     self.tb.connect((sync, 0), sink_freq)
     self.tb.connect((sync, 1), sink_detect)
     self.tb.run()
     phi_hat = sink_freq.data()[sink_detect.data().index(1)]
     est_freq_offset = 2 * phi_hat / fft_len
     self.assertAlmostEqual(est_freq_offset, freq_offset, places=2)
   def __init__(self, dBm, pfa, pfd, useless_bw, plot_histogram):
      gr.top_block.__init__(self)

      # Constants
      samp_rate = 2.4e6
      samples_per_band = 16
      tcme = 1.9528
      output_pfa = True
      debug_stats = False
      self.histogram = plot_histogram
      primary_user_location = 5
      mu = 0
      fft_size = 4096
      nframes_to_check = 1
      nframes_to_average = 1
      downconverter = 1

      src_data = self.generateRandomSignalSource(dBm, fft_size, mu, nframes_to_check*nframes_to_average)

		# Blocks
      src = gr.vector_source_c(src_data)
      s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
      fftb = fft.fft_vcc(fft_size, True, (window.blackmanharris(1024)), False, 1)
      self.ss = howto.spectrum_sensing_cf(samp_rate,fft_size,samples_per_band,pfd,pfa,tcme,output_pfa,debug_stats,primary_user_location,useless_bw,self.histogram,nframes_to_check,nframes_to_average,downconverter)
      self.sink = gr.vector_sink_f()

		# Connections
      self.connect(src, s2v, fftb, self.ss, self.sink)
Пример #40
0
    def test_001(self):
        """ Run test:
        - No overlap
        - Hamming window from Python
        - Constant input signal of amplitude 1

        The given input signal has a known power of 1. Therefore, the integral of the
        PSD estimation result should be pretty close to 1."""
        fft_len = 256
        overlap = 0
        ma_len = 1
        window = hamming(fft_len)

        src_data = (1,) * ((ma_len + 1) * fft_len)
        src = gr.vector_source_c(src_data, False)
        welch = specest.welch(fft_len, overlap, ma_len, False, window)
        sink = gr.vector_sink_f(fft_len)

        self.tb.connect(src, welch, sink)
        self.tb.run()

        dst_data =  sink.data()
        dst_data = array(dst_data[-fft_len:])
        power_est = sum(dst_data) * 2 * pi / fft_len

        self.assertAlmostEqual(power_est, 1, 5)
Пример #41
0
    def test_005_(self):

        src_data = (1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0)

        dwav = numpy.array(src_data)
        wvps = numpy.zeros(3)
        # wavelet power spectrum
        scl = 1.0/sqr(dwav[0])
        k = 1
        for e in range(len(wvps)):
            wvps[e] = scl*sqr(dwav[k:k+(01<<e)]).sum()
            k += 01<<e

        src = gr.vector_source_f(src_data, False, len(src_data))
        kon = wavelet_swig.wvps_ff(len(src_data))
        dst = gr.vector_sink_f(int(math.ceil(math.log(len(src_data), 2))))

        self.tb.connect(src, kon)
        self.tb.connect(kon, dst)

        self.tb.run()
        snk_data = dst.data()

        sum = 0
        for (u,v) in zip(snk_data, wvps):
            w = u - v
            sum += w * w
        sum /= float(len(snk_data))
        assert sum < 1e-6
Пример #42
0
    def test__001_t(self):
        """
        Generate a signal with n_sinusoids sinusoids
        and a SNR of SNR.
        Another Check to see if it's working at all.
        """
        n_sinusoids = 2
        nsamples = 2048
        samp_rate = 32000
        SNR = 10  # in dB
        decimals = 3
        self.siggen = siggen.signal_generator(n_sinusoids=n_sinusoids,
                                              SNR=SNR,
                                              samp_rate=samp_rate,
                                              nsamples=nsamples)

        self.stream = gr.stream_to_vector(gr.sizeof_gr_complex, nsamples)
        self.esprit = specest.esprit_vcf(n=n_sinusoids,
                                         m=100,
                                         nsamples=nsamples)
        self.sink = gr.vector_sink_f(vlen=n_sinusoids)
        # wire it up ...
        self.tb.connect(self.siggen, self.stream, self.esprit, self.sink)
        for i in range(100):
            self.tb.run()
            for (g, e) in zip(sorted(list(self.sink.data())),
                              self.siggen.omegas()):
                self.assertAlmostEqual(g, e, decimals)
Пример #43
0
    def test02(self):
        # Test float/float version
        omega = 2
        gain_omega = 0.01
        mu = 0.5
        gain_mu = 0.01
        omega_rel_lim = 0.001

        self.test = digital_swig.clock_recovery_mm_ff(omega, gain_omega, mu, gain_mu, omega_rel_lim)

        data = 100 * [1]
        self.src = gr.vector_source_f(data, False)
        self.snk = gr.vector_sink_f()

        self.tb.connect(self.src, self.test, self.snk)
        self.tb.run()

        expected_result = 100 * [0.99972]  # doesn't quite get to 1.0
        dst_data = self.snk.data()

        # Only compare last Ncmp samples
        Ncmp = 30
        len_e = len(expected_result)
        len_d = len(dst_data)
        expected_result = expected_result[len_e - Ncmp :]
        dst_data = dst_data[len_d - Ncmp :]

        # print expected_result
        # print dst_data

        self.assertFloatTuplesAlmostEqual(expected_result, dst_data, 5)
Пример #44
0
    def test_complex_to_arg(self):
        pi = math.pi
        input_data = (0, pi / 6, pi / 4, pi / 2, 3 * pi / 4, 7 * pi / 8,
                      -pi / 6, -pi / 4, -pi / 2, -3 * pi / 4, -7 * pi / 8)

        expected_result = (
            0.0,  # 0
            0.52382522821426392,  # pi/6
            0.78539806604385376,  # pi/4
            1.5707963705062866,  # pi/2
            2.3561947345733643,  # 3pi/4
            2.7491819858551025,  # 7pi/8
            -0.52382522821426392,  # -pi/6
            -0.78539806604385376,  # -pi/4
            -1.5707963705062866,  # -pi/2
            -2.3561947345733643,  # -3pi/4
            -2.7491819858551025)  # -7pi/8

        src_data = tuple([math.cos(x) + math.sin(x) * 1j for x in input_data])
        src = gr.vector_source_c(src_data)
        op = gr.complex_to_arg()
        dst = gr.vector_sink_f()
        self.tb.connect(src, op)
        self.tb.connect(op, dst)
        self.tb.run()
        actual_result = dst.data()

        self.assertFloatTuplesAlmostEqual(expected_result, actual_result, 3)
Пример #45
0
    def __init__(self, n_sinusoids = 1, SNR = 10, samp_rate = 32e3, nsamples = 2048):
        gr.hier_block2.__init__(self, "ESPRIT/MUSIC signal generator",
                                gr.io_signature(0, 0, gr.sizeof_float),
                                gr.io_signature(1, 1, gr.sizeof_gr_complex))
        sigampl = 10.0**(SNR/10.0) # noise power is 1
        self.srcs = list()

        self.n_sinusoids = n_sinusoids
        self.samp_rate = samp_rate
        # create our signals ...
        for s in range(n_sinusoids):
            self.srcs.append(gr.sig_source_c(samp_rate,
                gr.GR_SIN_WAVE,1000 * s + 2000,
                numpy.sqrt(sigampl/n_sinusoids)))

        seed = ord(os.urandom(1))
        self.noise = gr.noise_source_c(gr.GR_GAUSSIAN, 1, seed)
        self.add = gr.add_cc()
        self.head = gr.head(gr.sizeof_gr_complex, nsamples)
        self.sink = gr.vector_sink_f(vlen=n_sinusoids)
        # wire it up ... 
        for s in range(n_sinusoids):
            self.connect(self.srcs[s], (self.add, s))
        # Additive noise
        self.connect(self.noise, (self.add, n_sinusoids))
        self.connect(self.add, self.head, self)
Пример #46
0
    def test_002_interp(self):          
        taps = random_floats(31)
        #src_data = random_floats(10000)  # FIXME the 10k case fails!
        src_data = random_floats(1000)
        interpolation = 3

        expected_result = reference_interp_filter(src_data, interpolation, taps)

	tb = gr.top_block()
        src = gr.vector_source_f(src_data)
        op = gr.rational_resampler_base_fff(interpolation, 1, taps)
        dst = gr.vector_sink_f()
        tb.connect(src, op)
        tb.connect(op, dst)
        tb.run()
        result_data = dst.data()

        L1 = len(result_data)
        L2 = len(expected_result)
        L = min(L1, L2)
        if False:
            sys.stderr.write('delta = %2d: ntaps = %d interp = %d ilen = %d\n' %
                             (L2 - L1, len(taps), interpolation, len(src_data)))
            sys.stderr.write('  len(result_data) = %d  len(expected_result) = %d\n' %
                             (len(result_data), len(expected_result)))
        #self.assertEqual(expected_result[0:L], result_data[0:L])
        # FIXME check first 3 answers
        self.assertEqual(expected_result[3:L], result_data[3:L])
Пример #47
0
   def __init__(self, dBm, pfa, pfd, nTrials):
      gr.top_block.__init__(self)

      # Constants
      samp_rate = 2.4e6
      fft_size = 4096
      samples_per_band = 16
      tcme = 1.9528
      output_pfa = True
      debug_stats = False
      histogram = False
      primary_user_location = 0
      useless_bw = 200000.0
      src_data = [0+0j]*fft_size*nTrials
      voltage = self.powerToAmplitude(dBm);

		# Blocks
      src = gr.vector_source_c(src_data)
      noise = gr.noise_source_c(gr.GR_GAUSSIAN, voltage, 42)
      add = gr.add_vcc()
      s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
      fftb = fft.fft_vcc(fft_size, True, (window.blackmanharris(1024)), False, 1)
      ss = howto.spectrum_sensing_cf(samp_rate,fft_size,samples_per_band,pfd,pfa,tcme,output_pfa,debug_stats,primary_user_location,useless_bw,histogram)
      self.sink = gr.vector_sink_f()

		# Connections
      self.connect(src, add, s2v, fftb, ss, self.sink)
      self.connect(noise, (add, 1))
Пример #48
0
    def xtest_005_interp_random_vals(self):
        MAX_TAPS = 9
        MAX_INTERP = 7
        INPUT_LEN = 9

        random.seed(0)    # we want reproducibility

        for ntaps in xrange(1, MAX_TAPS + 1):
            for interp in xrange(1, MAX_INTERP+1):
                for ilen in xrange(ntaps, ntaps + INPUT_LEN):
                    src_data = random_floats(ilen)
                    taps = random_floats(ntaps)
                    expected_result = reference_interp_filter(src_data, interp, taps)

                    tb = gr.top_block()
                    src = gr.vector_source_f(src_data)
                    op = gr.rational_resampler_base_fff(interp, 1, taps)
                    dst = gr.vector_sink_f()
                    tb.connect(src, op, dst)
                    tb.run()
                    tb = None
                    result_data = dst.data()
                    L1 = len(result_data)
                    L2 = len(expected_result)
                    L = min(L1, L2)
                    #if True or abs(L1-L2) > 1:
                    if False:
                        sys.stderr.write('delta = %2d: ntaps = %d interp = %d ilen = %d\n' % (L2 - L1, ntaps, interp, ilen))
                        #sys.stderr.write('  len(result_data) = %d  len(expected_result) = %d\n' %
                        #                 (len(result_data), len(expected_result)))
                    #self.assertEqual(expected_result[0:L], result_data[0:L])
                    # FIXME check first ntaps+1 answers
                    self.assertEqual(expected_result[ntaps+1:L], result_data[ntaps+1:L])
Пример #49
0
    def test_001_t (self):
    	
    	src_data = [0]*10
	src_data1 = [1]*20
	src_data = src_data+src_data1
 	
	#expected_result = (-2.0, 0.0, 5.0, 8.0, 9.0, 11.0, 14.0, 18.0)
	
	src0 = gr.vector_source_f(src_data)
	sqr = dsim()
	sqr.set_parameters(2,0.5,1,.1,10,1)

	#Preload
	sqr.input_config(1).preload_items = 1
	dst = gr.vector_sink_f()
	
	self.tb.connect(src0, (sqr,0)) # src0(vector_source) -> sqr_input_0
	self.tb.connect((sqr,0), (sqr,1)) # sqr_output_0 -> sqr_input_1
	self.tb.connect(sqr,dst) # sqr_output_0 -> dst (vector_source)

	self.tb.run()

	result_data = dst.data()
	
	
	import  matplotlib.pyplot as plt
    	plt.plot(result_data)
	print "result", result_data
    	plt.show()
    def test_quad_demod_001(self):
        f = 1000.0
        fs = 8000.0

        src_data = []
        for i in xrange(200):
            ti = i/fs
            src_data.append(cmath.exp(2j*cmath.pi*f*ti))

        # f/fs is a quarter turn per sample.
        # Set the gain based on this to get 1 out.
        gain = 1.0/(cmath.pi/4)

        expected_result = [0,] + 199*[1.0]

        src = gr.vector_source_c(src_data)
        op = analog.quadrature_demod_cf(gain)
        dst = gr.vector_sink_f()
        
        self.tb.connect(src, op)
        self.tb.connect(op, dst)
        self.tb.run()
        
        result_data = dst.data()
        self.assertComplexTuplesAlmostEqual(expected_result, result_data, 5)
 def test_fir_filter_fff_001(self):
     src_data = 40*[1, 2, 3, 4]
     expected_data = (0.5, 1.5, 3.0, 5.0, 5.5, 6.5, 8.0, 10.0,
                      10.5, 11.5, 13.0, 15.0, 15.5, 16.5, 18.0,
                      20.0, 20.5, 21.5, 23.0, 25.0, 25.5, 26.5,
                      28.0, 30.0, 30.5, 31.5, 33.0, 35.0, 35.5,
                      36.5, 38.0, 40.0, 40.5, 41.5, 43.0, 45.0,
                      45.5, 46.5, 48.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0,
                      50.0, 50.0, 50.0, 50.0, 50.0)
     src = gr.vector_source_f(src_data)
     op  = filter.fir_filter_fff(1, 20*[0.5, 0.5])
     dst = gr.vector_sink_f()
     self.tb.connect(src, op, dst)
     self.tb.run()
     result_data = dst.data()
     self.assertFloatTuplesAlmostEqual(expected_data, result_data, 5)