Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_wfm_rcv_nogui.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, gru, eng_notation, optfir
4 from gnuradio import audio
5 from gnuradio import usrp
6 from gnuradio import blks
7 from gnuradio.eng_option import eng_option
8 from optparse import OptionParser
9 import usrp_dbid
10 import sys
11 import math
12
13 def pick_subdevice(u):
14     """
15     The user didn't specify a subdevice on the command line.
16     Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
17
18     @return a subdev_spec
19     """
20     return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
21                                 usrp_dbid.TV_RX_REV_2,
22                                 usrp_dbid.BASIC_RX))
23
24
25 class wfm_rx_graph (gr.flow_graph):
26
27     def __init__(self):
28         gr.flow_graph.__init__(self)
29
30         parser=OptionParser(option_class=eng_option)
31         parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
32                           help="select USRP Rx side A or B (default=A)")
33         parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
34                           help="set frequency to FREQ", metavar="FREQ")
35         parser.add_option("-g", "--gain", type="eng_float", default=None,
36                           help="set gain in dB (default is midpoint)")
37         parser.add_option("-O", "--audio-output", type="string", default="",
38                           help="pcm device name.  E.g., hw:0,0 or surround51 or /dev/dsp")
39
40         (options, args) = parser.parse_args()
41         if len(args) != 0:
42             parser.print_help()
43             sys.exit(1)
44         
45         self.vol = .1
46         self.state = "FREQ"
47         self.freq = 0
48
49         # build graph
50         
51         self.u = usrp.source_c()                    # usrp is data source
52
53         adc_rate = self.u.adc_rate()                # 64 MS/s
54         usrp_decim = 200
55         self.u.set_decim_rate(usrp_decim)
56         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
57         chanfilt_decim = 1
58         demod_rate = usrp_rate / chanfilt_decim
59         audio_decimation = 10
60         audio_rate = demod_rate / audio_decimation  # 32 kHz
61
62
63         if options.rx_subdev_spec is None:
64             options.rx_subdev_spec = pick_subdevice(self.u)
65
66         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
67         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
68         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
69
70
71         chan_filt_coeffs = optfir.low_pass (1,           # gain
72                                             usrp_rate,   # sampling rate
73                                             80e3,        # passband cutoff
74                                             115e3,       # stopband cutoff
75                                             0.1,         # passband ripple
76                                             60)          # stopband attenuation
77         #print len(chan_filt_coeffs)
78         chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
79
80         self.guts = blks.wfm_rcv (self, demod_rate, audio_decimation)
81
82         self.volume_control = gr.multiply_const_ff(self.vol)
83
84         # sound card as final sink
85         audio_sink = audio.sink(int(audio_rate),
86                                 options.audio_output,
87                                 False)  # ok_to_block
88         
89         # now wire it all together
90         self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
91
92
93         if options.gain is None:
94             # if no gain was specified, use the mid-point in dB
95             g = self.subdev.gain_range()
96             options.gain = float(g[0]+g[1])/2
97
98         if abs(options.freq) < 1e6:
99             options.freq *= 1e6
100
101         # set initial values
102
103         self.set_gain(options.gain)
104
105         if not(self.set_freq(options.freq)):
106             self._set_status_msg("Failed to set initial frequency")
107
108     def set_vol (self, vol):
109         self.vol = vol
110         self.volume_control.set_k(self.vol)
111         self.update_status_bar ()
112
113     def set_freq(self, target_freq):
114         """
115         Set the center frequency we're interested in.
116
117         @param target_freq: frequency in Hz
118         @rypte: bool
119
120         Tuning is a two step process.  First we ask the front-end to
121         tune as close to the desired frequency as it can.  Then we use
122         the result of that operation and our target_frequency to
123         determine the value for the digital down converter.
124         """
125         r = self.u.tune(0, self.subdev, target_freq)
126         
127         if r:
128             self.freq = target_freq
129             self.update_status_bar()
130             self._set_status_msg("OK", 0)
131             return True
132
133         self._set_status_msg("Failed", 0)
134         return False
135
136     def set_gain(self, gain):
137         self.subdev.set_gain(gain)
138
139     def update_status_bar (self):
140         msg = "Freq: %s  Volume:%f  Setting:%s" % (
141             eng_notation.num_to_str(self.freq), self.vol, self.state)
142         self._set_status_msg(msg, 1)
143         
144     def _set_status_msg(self, msg, which=0):
145         print msg
146
147     
148 if __name__ == '__main__':
149     fg = wfm_rx_graph()
150     try:
151         fg.run()
152     except KeyboardInterrupt:
153         pass