3 # Copyright 2004,2005,2007,2008 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
23 from gnuradio import gr, gru
24 from gnuradio import eng_notation
25 from gnuradio.eng_option import eng_option
26 from gnuradio.wxgui import stdgui2, fftsink2, waterfallsink2, scopesink2, form, slider
27 from optparse import OptionParser
33 class app_top_block(stdgui2.std_top_block):
34 def __init__(self, frame, panel, vbox, argv):
35 stdgui2.std_top_block.__init__(self, frame, panel, vbox, argv)
40 parser = OptionParser(option_class=eng_option)
41 parser.add_option("-d", "--decim", type="int", default=16,
42 help="set fgpa decimation rate to DECIM [default=%default]")
43 parser.add_option("-f", "--freq", type="eng_float", default=None,
44 help="set frequency to FREQ", metavar="FREQ")
45 parser.add_option("-W", "--waterfall", action="store_true", default=False,
46 help="Enable waterfall display")
47 parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
48 help="Enable oscilloscope display")
49 (options, args) = parser.parse_args()
53 self.options = options
54 self.options.gain = 1.0
55 self.show_debug_info = True
58 input_rate = 100e6 / options.decim
60 self.src = gr.file_descriptor_source(gr.sizeof_short, 0, False);
61 self.s2c = gr.interleaved_short_to_complex()
65 waterfallsink2.waterfall_sink_c (panel, fft_size=1024, sample_rate=input_rate)
66 elif options.oscilloscope:
67 self.scope = scopesink2.scope_sink_c(panel, sample_rate=input_rate)
69 self.scope = fftsink2.fft_sink_c (panel, fft_size=1024, y_divs=12, sample_rate=input_rate,ref_level=110,fft_rate=20)
71 self.connect(self.src, self.s2c, self.scope)
78 if options.freq is None:
79 # if no freq was specified, use the mid-point
82 if self.show_debug_info:
83 self.myform['decim'].set_value(self.options.decim)
86 def _set_status_msg(self, msg):
87 self.frame.GetStatusBar().SetStatusText(msg, 0)
89 def _build_gui(self, vbox):
91 def _form_set_freq(kv):
92 return self.set_freq(kv['freq'])
94 vbox.Add(self.scope.win, 10, wx.EXPAND)
96 # add control area at the bottom
97 self.myform = myform = form.form()
98 hbox = wx.BoxSizer(wx.HORIZONTAL)
100 myform['freq'] = form.float_field(
101 parent=self.panel, sizer=hbox, label="Center freq", weight=1,
102 callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
104 hbox.Add((5,0), 0, 0)
105 vbox.Add(hbox, 0, wx.EXPAND)
107 self._build_subpanel(vbox)
109 def _build_subpanel(self, vbox_arg):
110 # build a secondary information panel (sometimes hidden)
112 # FIXME figure out how to have this be a subpanel that is always
113 # created, but has its visibility controlled by foo.Show(True/False)
115 def _form_set_decim(kv):
116 return self.set_decim(kv['decim'])
118 if not(self.show_debug_info):
125 #panel = wx.Panel(self.panel, -1)
126 #vbox = wx.BoxSizer(wx.VERTICAL)
128 hbox = wx.BoxSizer(wx.HORIZONTAL)
131 myform['decim'] = form.int_field(
132 parent=panel, sizer=hbox, label="Decim",
133 callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
136 vbox.Add(hbox, 0, wx.EXPAND)
139 def set_freq(self, target_freq):
141 Set the center frequency we're interested in.
143 @param target_freq: frequency in Hz
146 Tuning is a two step process. First we ask the front-end to
147 tune as close to the desired frequency as it can. Then we use
148 the result of that operation and our target_frequency to
149 determine the value for the digital down converter.
153 self.myform['freq'].set_value(target_freq) # update displayed value
154 if not self.options.waterfall and not self.options.oscilloscope:
155 self.scope.win.set_baseband_freq(target_freq)
160 def set_gain(self, gain):
161 self.myform['gain'].set_value(gain) # update displayed value
163 def set_decim(self, decim):
164 input_rate = 100e6 / self.options.decim
165 self.scope.set_sample_rate(input_rate)
166 if self.show_debug_info: # update displayed values
167 self.myform['decim'].set_value(self.u.decim_rate())
170 def _setup_events(self):
171 if not self.options.waterfall and not self.options.oscilloscope:
172 self.scope.win.Bind(wx.EVT_LEFT_DCLICK, self.evt_left_dclick)
174 def evt_left_dclick(self, event):
175 (ux, uy) = self.scope.win.GetXY(event)
177 # Re-center on maximum power
178 points = self.scope.win._points
179 if self.scope.win.peak_hold:
180 if self.scope.win.peak_vals is not None:
181 ind = numpy.argmax(self.scope.win.peak_vals)
183 ind = int(points.shape()[0]/2)
185 ind = numpy.argmax(points[:,1])
186 (freq, pwr) = points[ind]
187 target_freq = freq/self.scope.win._scale_factor
189 self.set_freq(target_freq)
191 # Re-center on clicked frequency
192 target_freq = ux/self.scope.win._scale_factor
193 self.set_freq(target_freq)
197 app = stdgui2.stdapp(app_top_block, "USRP FFT", nstatus=1)
200 if __name__ == '__main__':