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