Statistics
| Branch: | Tag: | Revision:

root / gr-pager / apps / usrp_flex_band @ 689699fb

History | View | Annotate | Download (5.2 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2006,2007,2009,2011 Free Software Foundation, Inc.
4
# 
5
# This file is part of GNU Radio
6
# 
7
# GNU Radio is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3, or (at your option)
10
# any later version.
11
# 
12
# GNU Radio is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
# 
17
# You should have received a copy of the GNU General Public License
18
# along with GNU Radio; see the file COPYING.  If not, write to
19
# the Free Software Foundation, Inc., 51 Franklin Street,
20
# Boston, MA 02110-1301, USA.
21
# 
22
23
from gnuradio import gr, gru, uhd, optfir, eng_notation, blks2, pager
24
from gnuradio.eng_option import eng_option
25
from optparse import OptionParser
26
import sys
27
28
class app_top_block(gr.top_block):
29
    def __init__(self, options, queue):
30
	gr.top_block.__init__(self, "usrp_flex_all")
31
32
        if options.from_file is not None:
33
            self.u = gr.file_source(gr.sizeof_gr_complex, options.from_file)
34
            if options.verbose:
35
                print "Reading samples from file", options.from_file
36
        else:
37
            # Set up USRP source
38
            self.u = uhd.usrp_source(device_addr=options.address, stream_args=uhd.stream_args('fc32'))
39
40
            # Grab 1 MHz of spectrum
41
            # (A UHD facility to get sample rate range and granularity would be useful)
42
            self.u.set_samp_rate(1e6)
43
            rate = self.u.get_samp_rate()
44
            if rate != 1e6:
45
                print "Unable to set required sample rate of 1 Msps (got %f)" % rate
46
                sys.exit(1)
47
                
48
            # Tune daughterboard
49
            r = self.u.set_center_freq(options.freq+options.calibration, 0)
50
            if not r:
51
                frange = self.u.get_freq_range()
52
                sys.stderr.write(("\nRequested frequency (%f) out or range [%f, %f]\n") % \
53
                                     (freq, frange.start(), frange.stop()))
54
                sys.exit(1)
55
56
            # if no gain was specified, use the mid-point in dB
57
            if options.rx_gain is None:
58
                grange = self.u.get_gain_range()
59
                options.rx_gain = float(grange.start()+grange.stop())/2.0
60
                print "\nNo gain specified."
61
                print "Setting gain to %f (from [%f, %f])" % \
62
                    (options.rx_gain, grange.start(), grange.stop())
63
        
64
            self.u.set_gain(options.rx_gain, 0)
65
66
67
        taps = gr.firdes.low_pass(1.0,
68
                                  1.0,
69
                                  1.0/40.0*0.4,
70
                                  1.0/40.0*0.1,
71
                                  gr.firdes.WIN_HANN)
72
73
        if options.verbose:
74
            print "Channel filter has", len(taps), "taps"
75
76
        bank = blks2.analysis_filterbank(40, taps)
77
        self.connect(self.u, bank)
78
79
        if options.log and options.from_file == None:
80
            src_sink = gr.file_sink(gr.sizeof_gr_complex, 'usrp.dat')
81
            self.connect(self.u, src_sink)
82
83
        for i in range(40):
84
	    if i < 20:
85
		freq = options.freq+i*25e3
86
	    else:
87
		freq = options.freq-0.5e6+(i-20)*25e3
88
89
	    self.connect((bank, i), pager.flex_demod(queue, freq, options.verbose, options.log))
90
	    if options.log:
91
		self.connect((bank, i), gr.file_sink(gr.sizeof_gr_complex, 'chan_'+'%3.3f'%(freq/1e6)+'.dat'))
92
93
94
def get_options():
95
    parser = OptionParser(option_class=eng_option)
96
97
    parser.add_option('-f', '--freq', type="eng_float", default=None,
98
                      help="Set receive frequency to FREQ [default=%default]",
99
                      metavar="FREQ")
100
    parser.add_option("-a", "--address", type="string", default="addr=192.168.10.2",
101
                      help="Address of UHD device, [default=%default]")
102
    parser.add_option("-A", "--antenna", type="string", default=None,
103
                      help="select Rx Antenna where appropriate")
104
    parser.add_option("", "--rx-gain", type="eng_float", default=None,
105
                      help="set receive gain in dB (default is midpoint)")
106
    parser.add_option("-c",   "--calibration", type="eng_float", default=0.0,
107
                      help="set frequency offset to Hz", metavar="Hz")
108
    parser.add_option("-v", "--verbose", action="store_true", default=False)
109
    parser.add_option("-l", "--log", action="store_true", default=False,
110
                      help="log flowgraph to files (LOTS of data)")
111
    parser.add_option("-F", "--from-file", default=None,
112
                      help="read samples from file instead of USRP")
113
114
    (options, args) = parser.parse_args()
115
116
    if len(args) > 0:
117
	print "Run 'usrp_flex_band.py -h' for options."
118
	sys.exit(1)
119
120
    if (options.freq is None):
121
        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
122
        sys.exit(1)
123
        
124
    return (options, args)
125
126
    
127
if __name__ == "__main__":
128
129
    (options, args) = get_options()
130
131
    queue = gr.msg_queue()
132
    tb = app_top_block(options, queue)
133
    runner = pager.queue_runner(queue)
134
135
    try:
136
        tb.run()
137
    except KeyboardInterrupt:
138
        pass
139
140
    runner.end()
141
142