5cd5c148220fe06b36ff1b3a4f3c4488060c5615
[debian/gnuradio] / gnuradio-examples / python / hier / usrp / usrp_fft.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,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 2, 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
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.wxgui import stdgui2, fftsink2, waterfallsink2, scopesink2, form, slider
28 from optparse import OptionParser
29 import wx
30 import sys
31
32
33 def pick_subdevice(u):
34     """
35     The user didn't specify a subdevice on the command line.
36     If there's a daughterboard on A, select A.
37     If there's a daughterboard on B, select B.
38     Otherwise, select A.
39     """
40     if u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
41         return (0, 0)
42     if u.db[1][0].dbid() >= 0:
43         return (1, 0)
44     return (0, 0)
45
46
47 class app_top_block(stdgui2.std_top_block):
48     def __init__(self, frame, panel, vbox, argv):
49         stdgui2.std_top_block.__init__(self, frame, panel, vbox, argv)
50
51         self.frame = frame
52         self.panel = panel
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=first one with a daughterboard)")
57         parser.add_option("-d", "--decim", type="int", default=16,
58                           help="set fgpa decimation rate to DECIM [default=%default]")
59         parser.add_option("-f", "--freq", type="eng_float", default=None,
60                           help="set frequency to FREQ", metavar="FREQ")
61         parser.add_option("-g", "--gain", type="eng_float", default=None,
62                           help="set gain in dB (default is midpoint)")
63         parser.add_option("-W", "--waterfall", action="store_true", default=False,
64                           help="Enable waterfall display")
65         parser.add_option("-8", "--width-8", action="store_true", default=False,
66                           help="Enable 8-bit samples across USB")
67         parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
68                           help="Enable oscilloscope display")
69         (options, args) = parser.parse_args()
70         if len(args) != 0:
71             parser.print_help()
72             sys.exit(1)
73
74         self.show_debug_info = True
75         
76         self.u = usrp.source_c(decim_rate=options.decim)
77         self.define_component("usrp", self.u)
78         if options.rx_subdev_spec is None:
79             options.rx_subdev_spec = pick_subdevice(self.u)
80         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
81
82         if options.width_8:
83             width = 8
84             shift = 8
85             format = self.u.make_format(width, shift)
86             print "format =", hex(format)
87             r = self.u.set_format(format)
88             print "set_format =", r
89             
90         # determine the daughterboard subdevice we're using
91         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
92
93         input_rate = self.u.adc_freq() / self.u.decim_rate()
94
95         
96         if options.waterfall:
97             self.scope = waterfallsink2.waterfall_sink_c (panel, fft_size=1024, sample_rate=input_rate)
98         elif options.oscilloscope:
99             self.scope = scopesink2.scope_sink_c(panel, sample_rate=input_rate)
100         else:
101             self.scope = fftsink2.fft_sink_c (panel, fft_size=1024, sample_rate=input_rate)
102         self.define_component("scope", self.scope)
103
104         # Ultimately this will be
105         # self.connect("usrp scope")
106         self.connect("usrp", 0, "scope", 0)
107
108         self._build_gui(vbox)
109
110         # set initial values
111
112         if options.gain is None:
113             # if no gain was specified, use the mid-point in dB
114             g = self.subdev.gain_range()
115             options.gain = float(g[0]+g[1])/2
116
117         if options.freq is None:
118             # if no freq was specified, use the mid-point
119             r = self.subdev.freq_range()
120             options.freq = float(r[0]+r[1])/2
121
122         self.set_gain(options.gain)
123
124         if self.show_debug_info:
125             self.myform['decim'].set_value(self.u.decim_rate())
126             self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
127             self.myform['dbname'].set_value(self.subdev.name())
128             self.myform['baseband'].set_value(0)
129             self.myform['ddc'].set_value(0)
130
131         if not(self.set_freq(options.freq)):
132             self._set_status_msg("Failed to set initial frequency")
133
134     def _set_status_msg(self, msg):
135         self.frame.GetStatusBar().SetStatusText(msg, 0)
136
137     def _build_gui(self, vbox):
138
139         def _form_set_freq(kv):
140             return self.set_freq(kv['freq'])
141             
142         vbox.Add(self.scope.win, 10, wx.EXPAND)
143         
144         # add control area at the bottom
145         self.myform = myform = form.form()
146         hbox = wx.BoxSizer(wx.HORIZONTAL)
147         hbox.Add((5,0), 0, 0)
148         myform['freq'] = form.float_field(
149             parent=self.panel, sizer=hbox, label="Center freq", weight=1,
150             callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
151
152         hbox.Add((5,0), 0, 0)
153         g = self.subdev.gain_range()
154         myform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
155                                            weight=3,
156                                            min=int(g[0]), max=int(g[1]),
157                                            callback=self.set_gain)
158
159         hbox.Add((5,0), 0, 0)
160         vbox.Add(hbox, 0, wx.EXPAND)
161
162         self._build_subpanel(vbox)
163
164     def _build_subpanel(self, vbox_arg):
165         # build a secondary information panel (sometimes hidden)
166
167         # FIXME figure out how to have this be a subpanel that is always
168         # created, but has its visibility controlled by foo.Show(True/False)
169         
170         def _form_set_decim(kv):
171             return self.set_decim(kv['decim'])
172
173         if not(self.show_debug_info):
174             return
175
176         panel = self.panel
177         vbox = vbox_arg
178         myform = self.myform
179
180         #panel = wx.Panel(self.panel, -1)
181         #vbox = wx.BoxSizer(wx.VERTICAL)
182
183         hbox = wx.BoxSizer(wx.HORIZONTAL)
184         hbox.Add((5,0), 0)
185
186         myform['decim'] = form.int_field(
187             parent=panel, sizer=hbox, label="Decim",
188             callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
189
190         hbox.Add((5,0), 1)
191         myform['fs@usb'] = form.static_float_field(
192             parent=panel, sizer=hbox, label="Fs@USB")
193
194         hbox.Add((5,0), 1)
195         myform['dbname'] = form.static_text_field(
196             parent=panel, sizer=hbox)
197
198         hbox.Add((5,0), 1)
199         myform['baseband'] = form.static_float_field(
200             parent=panel, sizer=hbox, label="Analog BB")
201
202         hbox.Add((5,0), 1)
203         myform['ddc'] = form.static_float_field(
204             parent=panel, sizer=hbox, label="DDC")
205
206         hbox.Add((5,0), 0)
207         vbox.Add(hbox, 0, wx.EXPAND)
208
209         
210     def set_freq(self, target_freq):
211         """
212         Set the center frequency we're interested in.
213
214         @param target_freq: frequency in Hz
215         @rypte: bool
216
217         Tuning is a two step process.  First we ask the front-end to
218         tune as close to the desired frequency as it can.  Then we use
219         the result of that operation and our target_frequency to
220         determine the value for the digital down converter.
221         """
222         r = self.u.tune(0, self.subdev, target_freq)
223         
224         if r:
225             self.myform['freq'].set_value(target_freq)     # update displayed value
226             if self.show_debug_info:
227                 self.myform['baseband'].set_value(r.baseband_freq)
228                 self.myform['ddc'].set_value(r.dxc_freq)
229             return True
230
231         return False
232
233     def set_gain(self, gain):
234         self.myform['gain'].set_value(gain)     # update displayed value
235         self.subdev.set_gain(gain)
236
237     def set_decim(self, decim):
238         ok = self.u.set_decim_rate(decim)
239         if not ok:
240             print "set_decim failed"
241         input_rate = self.u.adc_freq() / self.u.decim_rate()
242         self.scope.set_sample_rate(input_rate)
243         if self.show_debug_info:  # update displayed values
244             self.myform['decim'].set_value(self.u.decim_rate())
245             self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
246         return ok
247
248 def main ():
249     app = stdgui2.stdapp(app_top_block, "USRP FFT", nstatus=1)
250     app.MainLoop()
251
252 if __name__ == '__main__':
253     main ()