Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_wfm_rcv2_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 calc_dxc_freq(target_freq, baseband_freq, fs):
34    dxc_temp = (target_freq - baseband_freq) % fs
35
36    if dxc_temp < fs/2.0:
37        dxc_freq = - dxc_temp
38        inverted = False
39    else:
40        dxc_freq = fs - dxc_temp
41        inverted = True
42
43    return (dxc_freq, inverted)
44
45 def pick_subdevice(u):
46     """
47     The user didn't specify a subdevice on the command line.
48     Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
49
50     @return a subdev_spec
51     """
52     return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
53                                 usrp_dbid.TV_RX_REV_2,
54                                 usrp_dbid.BASIC_RX))
55
56
57 class wfm_rx_block (gr.top_block):
58
59     def __init__(self):
60         gr.top_block.__init__(self)
61
62         parser=OptionParser(option_class=eng_option)
63         parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
64                           help="select USRP Rx side A or B (default=A)")
65         parser.add_option("", "--f1", type="eng_float", default=100.7e6,
66                           help="set 1st station frequency to FREQ", metavar="FREQ")
67         parser.add_option("", "--f2", type="eng_float", default=102.5e6,
68                           help="set 2nd station freq to FREQ", metavar="FREQ")
69         parser.add_option("-g", "--gain", type="eng_float", default=40,
70                           help="set gain in dB (default is midpoint)")
71         parser.add_option("-O", "--audio-output", type="string", default="",
72                           help="pcm device name.  E.g., hw:0,0 or surround51 or /dev/dsp")
73
74         (options, args) = parser.parse_args()
75         if len(args) != 0:
76             parser.print_help()
77             sys.exit(1)
78         
79         if abs(options.f1) < 1e6:
80             options.f1 *= 1e6
81
82         if abs(options.f2) < 1e6:
83             options.f2 *= 1e6
84
85         if abs(options.f1 - options.f2) > 5.5e6:
86             print "Sorry, two stations must be within 5.5MHz of each other"
87             raise SystemExit
88
89         f = (options.f1, options.f2)
90         
91         self.vol = .1
92         self.state = "FREQ"
93
94         # build graph
95         
96         self.u = usrp.source_c(0, nchan=2)          # usrp is data source
97
98         adc_rate = self.u.adc_rate()                # 64 MS/s
99         usrp_decim = 200
100         self.u.set_decim_rate(usrp_decim)
101         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
102         chanfilt_decim = 1
103         demod_rate = usrp_rate / chanfilt_decim
104         audio_decimation = 10
105         audio_rate = demod_rate / audio_decimation  # 32 kHz
106
107
108         if options.rx_subdev_spec is None:
109             options.rx_subdev_spec = pick_subdevice(self.u)
110
111         mv = usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec)
112         mv |= (mv << 8) & 0xff00   # both DDC inputs setup same way
113         self.u.set_mux(mv)
114         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
115         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
116
117
118         # deinterleave two channels from FPGA
119         di = gr.deinterleave(gr.sizeof_gr_complex)
120         
121         # wire up the head of the chain
122         self.connect(self.u, di)
123         
124         # sound card as final sink
125         audio_sink = audio.sink(int(audio_rate), options.audio_output)
126
127         # taps for channel filter
128         chan_filt_coeffs = optfir.low_pass (1,           # gain
129                                             usrp_rate,   # sampling rate
130                                             80e3,        # passband cutoff
131                                             115e3,       # stopband cutoff
132                                             0.1,         # passband ripple
133                                             60)          # stopband attenuation
134         #print len(chan_filt_coeffs)
135
136         mid_freq = (f[0] + f[1]) / 2
137         # set front end PLL to middle frequency
138         tune_result = self.subdev.set_freq(mid_freq)
139
140         for n in range(2):
141             chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
142             guts = blks2.wfm_rcv (demod_rate, audio_decimation)
143             volume_control = gr.multiply_const_ff(self.vol)
144             self.connect((di, n), chan_filt)
145             self.connect(chan_filt, guts, volume_control)
146             self.connect(volume_control, (audio_sink, n))
147             dxc_freq, inverted = calc_dxc_freq(f[n], tune_result.baseband_freq,
148                                                self.u.converter_rate())
149             self.u.set_rx_freq(n, dxc_freq)
150         
151
152         if options.gain is None:
153             # if no gain was specified, use the mid-point in dB
154             g = self.subdev.gain_range()
155             options.gain = float(g[0]+g[1])/2
156
157
158         # set initial values
159         self.set_gain(options.gain)
160
161
162     def set_vol (self, vol):
163         self.vol = vol
164         self.volume_control.set_k(self.vol)
165
166
167     def set_gain(self, gain):
168         self.subdev.set_gain(gain)
169
170     def __del__(self):
171         # Avoid weak-reference error
172         del self.subdev
173     
174 if __name__ == '__main__':
175     tb = wfm_rx_block()
176     try:
177         tb.run()
178     except KeyboardInterrupt:
179         pass