def test_101_interp(self):
        taps = [1, 10, 100, 1000, 10000]
        src_data = (0, 2, 3, 5, 7, 11, 13, 17)
        interpolation = 3
        xr = (0,0,0,0,2,20,200,2003,20030,300,3005,30050,500,5007,50070,700,7011,70110,1100,11013,110130,1300,13017,130170,1700.0,17000.0,170000.0)
        expected_result = tuple([float(x) for x in xr])

        src = gr.vector_source_f(src_data)
        op = blks.rational_resampler_fff(self.fg, interpolation, 1, taps=taps)
        dst = gr.vector_sink_f()
        self.fg.connect(src, op)
        self.fg.connect(op, dst)
        self.fg.run()
        result_data = dst.data()
        self.assertEqual(expected_result, result_data)
示例#2
0
def main():
    parser = OptionParser(option_class=eng_option)
    parser.add_option("-f",
                      "--freq",
                      type="eng_float",
                      default=144.800e6,
                      help="set frequency to FREQ",
                      metavar="FREQ")
    parser.add_option("-m",
                      "--message",
                      type="string",
                      default=":ALL      :this is a test",
                      help="message to send",
                      metavar="MESSAGE")
    parser.add_option("-c",
                      "--mycall",
                      type="string",
                      default="MYCALL",
                      help="source callsign",
                      metavar="CALL")
    parser.add_option("-t",
                      "--tocall",
                      type="string",
                      default="CQ",
                      help="recipient callsign",
                      metavar="CALL")
    parser.add_option("-v",
                      "--via",
                      type="string",
                      default="RELAY",
                      help="digipeater callsign",
                      metavar="CALL")
    parser.add_option("-d",
                      "--do-logging",
                      action="store_true",
                      default=False,
                      help="enable logging on datafiles")
    parser.add_option("-s",
                      "--use-datafile",
                      action="store_true",
                      default=False,
                      help="use usrp.dat (256kbps) as output")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.print_help()
        sys.exit(1)

    bitrate = 9600
    dac_rate = 128e6
    usrp_interp = 500
    cordic_freq = options.freq - dac_rate
    sf = 153600
    syminterp = sf / bitrate  #16
    nbfmdev = 3e3
    fmsens = 2 * pi * nbfmdev / (sf * 5 / 3)
    bit_oversampling = 8
    sw_interp = int(sf / bitrate / bit_oversampling)  #2

    fg = gr.flow_graph()

    p = buildpacket(options.mycall, 0, options.tocall, 0, options.via, 0, 0x03,
                    0xf0, options.message)
    if options.do_logging:
        dumppackettofile(p, "packet.dat")
    v = bits2syms(nrziencode(scrambler(hdlcpacket(p, 100, 1000))))
    src = gr.vector_source_f(v)
    gaussian_taps = gr.firdes.gaussian(
        1,  # gain
        bit_oversampling,  # symbol_rate
        0.3,  # bandwidth * symbol time
        4 * bit_oversampling  # number of taps
    )
    sqwave = (1, ) * syminterp  #rectangular window
    taps = Numeric.convolve(Numeric.array(gaussian_taps),
                            Numeric.array(sqwave))
    gaussian = gr.interp_fir_filter_fff(syminterp, taps)  #9600*16=153600

    res_taps = blks.design_filter(5, 3, 0.4)
    res = blks.rational_resampler_fff(fg, 5, 3, res_taps)  #153600*5/3=256000
    fmmod = gr.frequency_modulator_fc(fmsens)
    amp = gr.multiply_const_cc(32000)

    if options.use_datafile:
        dst = gr.file_sink(gr.sizeof_gr_complex, "usrp.dat")
    else:
        u = usrp.sink_c(0, usrp_interp)  #256000*500=128000000
        tx_subdev_spec = usrp.pick_tx_subdevice(u)
        m = usrp.determine_tx_mux_value(u, tx_subdev_spec)
        print "mux = %#04x" % (m, )
        u.set_mux(m)
        subdev = usrp.selected_subdev(u, tx_subdev_spec)
        print "Using TX d'board %s" % (subdev.side_and_name(), )
        u.set_tx_freq(0, cordic_freq)
        u.set_pga(0, 0)
        print "Actual frequency: ", u.tx_freq(0)
        dst = u

    fg.connect(src, gaussian, res, fmmod, amp, dst)

    fg.start()
    fg.wait()
    def __init__(self, frame, panel, vbox, argv):
        stdgui.gui_flow_graph.__init__(self)

        self.frame = frame
        self.panel = panel
        
        parser = OptionParser(option_class=eng_option)
        parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
                          help="select USRP Rx side A or B (default=A)")
        parser.add_option("-d", "--decim", type="int", default=16,
                          help="set fgpa decimation rate to DECIM [default=%default]")
        parser.add_option("-f", "--freq", type="eng_float", default=None,
                          help="set frequency to FREQ", metavar="FREQ")
        parser.add_option("-Q", "--observing", type="eng_float", default=0.0,
                          help="set observing frequency to FREQ")
        parser.add_option("-a", "--avg", type="eng_float", default=1.0,
		help="set spectral averaging alpha")
        parser.add_option("-V", "--favg", type="eng_float", default=2.0,
                help="set folder averaging alpha")
        parser.add_option("-g", "--gain", type="eng_float", default=None,
                          help="set gain in dB (default is midpoint)")
        parser.add_option("-l", "--reflevel", type="eng_float", default=30.0,
                          help="Set pulse display reference level")
        parser.add_option("-L", "--lowest", type="eng_float", default=1.5,
                          help="Lowest valid frequency bin")
        parser.add_option("-e", "--longitude", type="eng_float", default=-76.02,                          help="Set Observer Longitude")
        parser.add_option("-c", "--latitude", type="eng_float", default=44.85,                          help="Set Observer Latitude")
        parser.add_option("-F", "--fft_size", type="eng_float", default=1024, help="Size of FFT")

        parser.add_option ("-t", "--threshold", type="eng_float", default=2.5, help="pulsar threshold")
        parser.add_option("-p", "--lowpass", type="eng_float", default=100, help="Pulse spectra cutoff freq")
        parser.add_option("-P", "--prefix", default="./", help="File prefix")
        parser.add_option("-u", "--pulsefreq", type="eng_float", default=0.748, help="Observation pulse rate")
        parser.add_option("-D", "--dm", type="eng_float", default=1.0e-5, help="Dispersion Measure")
        parser.add_option("-O", "--doppler", type="eng_float", default=1.0, help="Doppler ratio")
        parser.add_option("-B", "--divbase", type="eng_float", default=20, help="Y/Div menu base")
        parser.add_option("-I", "--division", type="eng_float", default=100, help="Y/Div")
        parser.add_option("-A", "--audio_source", default="plughw:0,0", help="Audio input device spec")
        (options, args) = parser.parse_args()
        if len(args) != 0:
            parser.print_help()
            sys.exit(1)

        self.show_debug_info = True

        self.reflevel = options.reflevel
        self.divbase = options.divbase
        self.division = options.division
        self.audiodev = options.audio_source

        # Low-pass cutoff for post-detector filter
        # Set to 100Hz usually, since lots of pulsars fit in this
        #   range
        self.lowpass = options.lowpass

        # What is lowest valid frequency bin in post-detector FFT?
        # There's some pollution very close to DC
        self.lowest_freq = options.lowest

        # What (dB) threshold to use in determining spectral candidates
        self.threshold = options.threshold

        # Filename prefix for recording file
        self.prefix = options.prefix

        # Dispersion Measure (DM)
        self.dm = options.dm

        # Doppler shift, as a ratio
        #  1.0 == no doppler shift
        #  1.005 == a little negative shift
        #  0.995 == a little positive shift
        self.doppler = options.doppler

        #
        # Input frequency and observing frequency--not necessarily the
        #   same thing, if we're looking at the IF of some downconverter
        #   that's ahead of the USRP and daughtercard.  This distinction
        #   is important in computing the correct de-dispersion filter.
        #
        self.frequency = options.freq
        if options.observing <= 0:
            self.observing_freq = options.freq
        else:
            self.observing_freq = options.observing
        
        # build the graph
        self.u = usrp.source_c(decim_rate=options.decim)
        self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))

        #
        # Recording file, in case we ever need to record baseband data
        #
        self.recording = gr.file_sink(gr.sizeof_char, "/dev/null")
        self.recording_state = False

        self.pulse_recording = gr.file_sink(gr.sizeof_short, "/dev/null")
        self.pulse_recording_state = False

        #
        # We come up with recording turned off, but the user may
        #  request recording later on
        self.recording.close()
        self.pulse_recording.close()

        #
        # Need these two for converting 12-bit baseband signals to 8-bit
        #
        self.tofloat = gr.complex_to_float()
        self.tochar = gr.float_to_char()

        # Need this for recording pulses (post-detector)
        self.toshort = gr.float_to_short()


        #
        # The spectral measurer sets this when it has a valid
        #   average spectral peak-to-peak distance
        # We can then use this to program the parameters for the epoch folder
        #
        # We set a sentimental value here
        self.pulse_freq = options.pulsefreq

        # Folder runs at this raw sample rate
        self.folder_input_rate = 20000

        # Each pulse in the epoch folder is sampled at 128 times the nominal
        #  pulse rate
        self.folding = 128

 
        #
        # Try to find candidate parameters for rational resampler
        #
        save_i = 0
        candidates = []
        for i in range(20,300):
            input_rate = self.folder_input_rate
            output_rate = int(self.pulse_freq * i)
            interp = gru.lcm(input_rate, output_rate) / input_rate
            decim = gru.lcm(input_rate, output_rate) / output_rate
            if (interp < 500 and decim < 250000):
                 candidates.append(i)

        # We didn't find anything, bail!
        if (len(candidates) < 1):
            print "Couldn't converge on resampler parameters"
            sys.exit(1)

        #
        # Now try to find candidate with the least sampling error
        #
        mindiff = 999.999
        for i in candidates:
            diff = self.pulse_freq * i
            diff = diff - int(diff)
            if (diff < mindiff):
                mindiff = diff
                save_i = i

        # Recompute rates
        input_rate = self.folder_input_rate
        output_rate = int(self.pulse_freq * save_i)

        # Compute new interp and decim, based on best candidate
        interp = gru.lcm(input_rate, output_rate) / input_rate
        decim = gru.lcm(input_rate, output_rate) / output_rate

        # Save optimized folding parameters, used later
        self.folding = save_i
        self.interp = int(interp)
        self.decim = int(decim)

        # So that we can view 4 pulses in the pulse viewer window
        FOLD_MULT=1

        # determine the daughterboard subdevice we're using
        self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
        self.cardtype = self.u.daughterboard_id(0)

        # Compute raw input rate
        input_rate = self.u.adc_freq() / self.u.decim_rate()

        # BW==input_rate for complex data
        self.bw = input_rate

        #
        # Set baseband filter bandwidth if DBS_RX:
        #
        if self.cardtype == usrp_dbid.DBS_RX:
            lbw = input_rate / 2
            if lbw < 1.0e6:
                lbw = 1.0e6
            self.subdev.set_bw(lbw)

        #
        # We use this as a crude volume control for the audio output
        #
        self.volume = gr.multiply_const_ff(10**(-1))
        

        #
        # Create location data for ephem package
        #
        self.locality = ephem.Observer()
        self.locality.long = str(options.longitude)
        self.locality.lat = str(options.latitude)

        #
        # What is the post-detector LPF cutoff for the FFT?
        #
        PULSAR_MAX_FREQ=int(options.lowpass)

        # First low-pass filters down to input_rate/FIRST_FACTOR
        #   and decimates appropriately
        FIRST_FACTOR=int(input_rate/(self.folder_input_rate/2))
        first_filter = gr.firdes.low_pass (1.0,
                                          input_rate,
                                          input_rate/FIRST_FACTOR,
                                          input_rate/(FIRST_FACTOR*20),         
                                          gr.firdes.WIN_HAMMING)

        # Second filter runs at the output rate of the first filter,
        #  And low-pass filters down to PULSAR_MAX_FREQ*10
        #
        second_input_rate =  int(input_rate/(FIRST_FACTOR/2))
        second_filter = gr.firdes.band_pass(1.0, second_input_rate,
                                          0.10,
                                          PULSAR_MAX_FREQ*10,
                                          PULSAR_MAX_FREQ*1.5,
                                          gr.firdes.WIN_HAMMING)

        # Third filter runs at PULSAR_MAX_FREQ*20
        #   and filters down to PULSAR_MAX_FREQ
        #
        third_input_rate = PULSAR_MAX_FREQ*20
        third_filter = gr.firdes_band_pass(1.0, third_input_rate,
                                           0.10, PULSAR_MAX_FREQ,
                                           PULSAR_MAX_FREQ/10.0,
                                           gr.firdes.WIN_HAMMING)


        #
        # Create the appropriate FFT scope
        #
        self.scope = ra_fftsink.ra_fft_sink_f (self, panel, 
           fft_size=int(options.fft_size), sample_rate=PULSAR_MAX_FREQ*2,
           title="Post-detector spectrum",  
           ofunc=self.pulsarfunc, xydfunc=self.xydfunc, fft_rate=200)

        #
        # Tell scope we're looking from DC to PULSAR_MAX_FREQ
        #
        self.scope.set_baseband_freq (0.0)


        #
        # Setup stripchart for showing pulse profiles
        #
        hz = "%5.3fHz " % self.pulse_freq
        per = "(%5.3f sec)" % (1.0/self.pulse_freq)
        sr = "%d sps" % (int(self.pulse_freq*self.folding))
        self.chart = ra_stripchartsink.stripchart_sink_f (self, panel,
               sample_rate=1,
               stripsize=self.folding*FOLD_MULT, parallel=True, title="Pulse Profiles: "+hz+per, 
               xlabel="Seconds @ "+sr, ylabel="Level", autoscale=True,
               divbase=self.divbase, scaling=1.0/(self.folding*self.pulse_freq))
        self.chart.set_ref_level(self.reflevel)
        self.chart.set_y_per_div(self.division)

        # De-dispersion filter setup
        #
        # Do this here, just before creating the filter
        #  that will use the taps.
        #
        ntaps = self.compute_disp_ntaps(self.dm,self.bw,self.observing_freq)

        # Taps for the de-dispersion filter
        self.disp_taps = Numeric.zeros(ntaps,Numeric.Complex64)

        # Compute the de-dispersion filter now
        self.compute_dispfilter(self.dm,self.doppler,
            self.bw,self.observing_freq)

        #
        # Call constructors for receive chains
        #

        #
        # Now create the FFT filter using the computed taps
        self.dispfilt = gr.fft_filter_ccc(1, self.disp_taps)

        #
        # Audio sink
        #
        self.audio = audio.sink(second_input_rate, self.audiodev)

        #
        # The three post-detector filters
        # Done this way to allow an audio path (up to 10Khz)
        # ...and also because going from xMhz down to ~100Hz
        # In a single filter doesn't seem to work.
        #
        self.first = gr.fir_filter_fff (FIRST_FACTOR/2, first_filter)

        p = second_input_rate / (PULSAR_MAX_FREQ*20)
        self.second = gr.fir_filter_fff (int(p), second_filter)
        self.third = gr.fir_filter_fff (10, third_filter)

        # Detector
        self.detector = gr.complex_to_mag_squared()

        self.enable_comb_filter = False
        # Epoch folder comb filter
        if self.enable_comb_filter == True:
            bogtaps = Numeric.zeros(512, Numeric.Float64)
            self.folder_comb = gr.fft_filter_ccc(1,bogtaps)

        # Rational resampler
        self.folder_rr = blks.rational_resampler_fff(self, self.interp, self.decim)

        # Epoch folder bandpass
        bogtaps = Numeric.zeros(1, Numeric.Float64)
        self.folder_bandpass = gr.fir_filter_fff (1, bogtaps)

        # Epoch folder F2C/C2F
        self.folder_f2c = gr.float_to_complex()
        self.folder_c2f = gr.complex_to_float()

        # Epoch folder S2P
        self.folder_s2p = gr.serial_to_parallel (gr.sizeof_float, 
             self.folding*FOLD_MULT)

        # Epoch folder IIR Filter (produces average pulse profiles)
        self.folder_iir = gr.single_pole_iir_filter_ff(1.0/options.favg,
             self.folding*FOLD_MULT)

        #
        # Set all the epoch-folder goop up
        #
        self.set_folding_params()

        # 
        # Start connecting configured modules in the receive chain
        #

        # Connect raw USRP to de-dispersion filter, detector
        self.connect(self.u, self.dispfilt, self.detector)

        # Connect detector output to FIR LPF
        #  in two stages, followed by the FFT scope
        self.connect(self.detector, self.first,
            self.second, self.third, self.scope)

        # Connect audio output
        self.connect(self.first, self.volume)
        self.connect(self.volume, (self.audio, 0))
        self.connect(self.volume, (self.audio, 1))

        # Connect epoch folder
        if self.enable_comb_filter == True:
            self.connect (self.first, self.folder_bandpass, self.folder_rr,
                self.folder_f2c,
                self.folder_comb, self.folder_c2f,
                self.folder_s2p, self.folder_iir,
                self.chart)

        else:
            self.connect (self.first, self.folder_bandpass, self.folder_rr,
                self.folder_s2p, self.folder_iir, self.chart)

        # Connect baseband recording file (initially /dev/null)
        self.connect(self.u, self.tofloat, self.tochar, self.recording)

        # Connect pulse recording file (initially /dev/null)
        self.connect(self.first, self.toshort, self.pulse_recording)

        #
        # Build the GUI elements
        #
        self._build_gui(vbox)

        # Make GUI agree with command-line
        self.myform['average'].set_value(int(options.avg))
        self.myform['foldavg'].set_value(int(options.favg))


        # Make spectral averager agree with command line
        if options.avg != 1.0:
            self.scope.set_avg_alpha(float(1.0/options.avg))
            self.scope.set_average(True)


        # set initial values

        if options.gain is None:
            # if no gain was specified, use the mid-point in dB
            g = self.subdev.gain_range()
            options.gain = float(g[0]+g[1])/2

        if options.freq is None:
            # if no freq was specified, use the mid-point
            r = self.subdev.freq_range()
            options.freq = float(r[0]+r[1])/2

        self.set_gain(options.gain)
        self.set_volume(-10.0)

        if not(self.set_freq(options.freq)):
            self._set_status_msg("Failed to set initial frequency")

        self.myform['decim'].set_value(self.u.decim_rate())
        self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
        self.myform['dbname'].set_value(self.subdev.name())
        self.myform['DM'].set_value(self.dm)
        self.myform['Doppler'].set_value(self.doppler)

        #
        # Start the timer that shows current LMST on the GUI
        #
        self.lmst_timer.Start(1000)
示例#4
0
def main():
    parser = OptionParser(option_class=eng_option)
    parser.add_option("-R",
                      "--rx-subdev-spec",
                      type="subdev",
                      default=None,
                      help="select USRP Rx side A or B (default=A)")
    parser.add_option("-f",
                      "--freq",
                      type="eng_float",
                      default=144.800e6,
                      help="set frequency to FREQ",
                      metavar="FREQ")
    parser.add_option("-g",
                      "--gain",
                      type="eng_float",
                      default=None,
                      help="set gain in dB (default is midpoint)")
    parser.add_option("-d",
                      "--do-logging",
                      action="store_true",
                      default=False,
                      help="enable logging on datafiles")
    parser.add_option("-s",
                      "--use-datafile",
                      action="store_true",
                      default=False,
                      help="use usrp.dat (256kbps) as input")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.print_help()
        sys.exit(1)

    markfreq = 2200
    spacefreq = 1200
    bitrate = 1200
    usrp_decim = 250
    if_rate = 64e6 / usrp_decim  #256e3
    sf = (if_rate * 3) / 5  #153600
    bit_oversampling = 8
    sw_decim = int(sf / bitrate / bit_oversampling)  #8
    bf = sf / sw_decim

    symdev = abs(markfreq - spacefreq) / 2
    symcf = min(markfreq, spacefreq) + symdev
    nbfmdev = 3e3
    nbfmk = if_rate / (2 * pi * nbfmdev)
    symk = bf / (2 * pi * symdev)

    fg = gr.flow_graph()

    if options.do_logging:
        logger1 = gr.file_sink(gr.sizeof_gr_complex, "usrpout.dat")
        logger2 = gr.file_sink(gr.sizeof_float, "demod.dat")
        logger3 = gr.file_sink(gr.sizeof_float, "clkrec.dat")
        logger4 = gr.file_sink(gr.sizeof_char, "slicer.dat")

    if options.use_datafile:
        src = gr.file_source(gr.sizeof_gr_complex, "usrp.dat")
    else:
        u = usrp.source_c()
        u.set_decim_rate(usrp_decim)
        if options.rx_subdev_spec is None:
            subdev_spec = usrp.pick_rx_subdevice(u)
        else:
            subdev_spec = options.rx_subdev_spec
        subdev = usrp.selected_subdev(u, subdev_spec)
        print "Using RX d'board %s" % (subdev.side_and_name(), )
        u.set_mux(usrp.determine_rx_mux_value(u, subdev_spec))
        print "MUX:%x" % (usrp.determine_rx_mux_value(u, subdev_spec))
        if options.gain is None:
            g = subdev.gain_range()
            gain = float(g[0] + g[1]) / 2
        else:
            gain = options.gain
        subdev.set_gain(gain)
        print "Gain set to", str(gain)
        r = usrp.tune(u, 0, subdev, options.freq)
        if r:
            print "Frequency set to", options.freq
        else:
            print "Frequency set to", options.freq, "failed"
        src = u

    chan_taps = gr.firdes.low_pass(1, if_rate, 13e3, 4e3, gr.firdes.WIN_HANN)
    chan = gr.fir_filter_ccf(1, chan_taps)  #256e3

    dee = blks.fm_deemph(fg, if_rate, 75e-6)

    fmdem = gr.quadrature_demod_cf(nbfmk)

    res_taps = blks.design_filter(3, 5, 0.4)
    res = blks.rational_resampler_fff(fg, 3, 5, res_taps)  #153600

    lo = gr.sig_source_c(sf, gr.GR_SIN_WAVE, -symcf, 1)
    mix = gr.multiply_cc()
    r2c = gr.float_to_complex()
    lp_taps = gr.firdes.low_pass(sw_decim, sf, 600, 2e3, gr.firdes.WIN_HANN)
    lp = gr.fir_filter_ccf(sw_decim, lp_taps)

    dem = gr.quadrature_demod_cf(symk)

    alpha = 0.0001
    freqoff = gr.single_pole_iir_filter_ff(alpha)
    sub = gr.sub_ff()

    _def_gain_mu = 0.05
    _def_mu = 0.5
    _def_freq_error = 0.00
    _def_omega_relative_limit = 0.005

    _omega = bit_oversampling * (1 + _def_freq_error)
    _gain_omega = .25 * _def_gain_mu * _def_gain_mu

    clkrec = gr.clock_recovery_mm_ff(_omega, _gain_omega, _def_mu,
                                     _def_gain_mu, _def_omega_relative_limit)

    slicer = gr.binary_slicer_fb()

    pktq = gr.msg_queue()
    sink = packetradio.hdlc_framer(pktq, 0)
    watcher = queue_watcher_thread(pktq, rx_callback)

    fg.connect(src, chan, fmdem, dee, res, r2c, (mix, 0))
    fg.connect(lo, (mix, 1))
    fg.connect(mix, lp, dem)
    fg.connect(dem, (sub, 0))
    fg.connect(dem, freqoff, (sub, 1))
    fg.connect(sub, clkrec, slicer)
    fg.connect(slicer, sink)

    if options.do_logging:
        fg.connect(src, logger1)
        fg.connect(sub, logger2)
        fg.connect(clkrec, logger3)
        fg.connect(slicer, logger4)

    fg.start()
    fg.wait()