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