Merge branch 'dfsg-orig'
[debian/gnuradio] / gr-msdd6000 / src / python-examples / msdd_rs_spec_an.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2009 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
24 from gnuradio import msdd_rs
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.qtgui import qtgui
28 from optparse import OptionParser
29 import sys,time
30
31 try:
32     from gnuradio.qtgui import qtgui
33     from PyQt4 import QtGui, QtCore
34     import sip
35 except ImportError:
36     print "Please install gr-qtgui."
37     sys.exit(1)
38
39 try:
40     from msdd_display_qtgui import Ui_MainWindow
41 except ImportError:
42     print "Error: could not find msdd_display_qtgui.py:"
43     print "\t\"pyuic4 msdd_display_qtgui.ui -o msdd_display_qtgui.py\""
44     sys.exit(1)
45
46
47 # ////////////////////////////////////////////////////////////////////
48 #        Define the QT Interface and Control Dialog
49 # ////////////////////////////////////////////////////////////////////
50
51
52 class main_window(QtGui.QMainWindow):
53     def __init__(self, snk, fg, parent=None):
54
55         QtGui.QWidget.__init__(self, parent)
56         self.gui = Ui_MainWindow()
57         self.gui.setupUi(self)
58
59         self.fg = fg
60
61         # Add the qtsnk widgets to the layout box
62         self.gui.sinkLayout.addWidget(snk)
63
64         self.gui.dcGainEdit.setText(QtCore.QString("%1").arg(0.001))
65
66         # Connect up some signals
67         self.connect(self.gui.pauseButton, QtCore.SIGNAL("clicked()"),
68                      self.pauseFg)
69         self.connect(self.gui.frequencyEdit, QtCore.SIGNAL("editingFinished()"),
70                      self.frequencyEditText)
71         self.connect(self.gui.gainEdit, QtCore.SIGNAL("editingFinished()"),
72                      self.gainEditText)
73         self.connect(self.gui.bandwidthEdit, QtCore.SIGNAL("editingFinished()"),
74                      self.bandwidthEditText)
75         self.connect(self.gui.amplifierEdit, QtCore.SIGNAL("editingFinished()"),
76                      self.amplifierEditText)
77
78         self.connect(self.gui.actionSaveData, QtCore.SIGNAL("activated()"),
79                      self.saveData)
80         self.gui.actionSaveData.setShortcut(QtGui.QKeySequence.Save)
81
82         self.connect(self.gui.dcGainEdit, QtCore.SIGNAL("editingFinished()"),
83                      self.dcGainEditText)
84         self.connect(self.gui.dcCancelCheckBox, QtCore.SIGNAL("clicked(bool)"),
85                      self.dcCancelClicked)
86
87     def pauseFg(self):
88         if(self.gui.pauseButton.text() == "Pause"):
89             self.fg.stop()
90             self.fg.wait()
91             self.fg.stop_data()
92             self.gui.pauseButton.setText("Unpause")
93         else:
94             self.fg.start()
95             self.fg.start_data()
96             self.gui.pauseButton.setText("Pause")
97       
98
99     # Functions to set the values in the GUI
100     def set_frequency(self, freq):
101         self.freq = freq
102         sfreq = eng_notation.num_to_str(self.freq)
103         self.gui.frequencyEdit.setText(QtCore.QString("%1").arg(sfreq))
104         
105     def set_gain(self, gain):
106         self.gain = gain
107         self.gui.gainEdit.setText(QtCore.QString("%1").arg(self.gain))
108
109     def set_bandwidth(self, bw):
110         self.bw = bw
111         sbw = eng_notation.num_to_str(self.bw)
112         self.gui.bandwidthEdit.setText(QtCore.QString("%1").arg(sbw))
113
114     def set_amplifier(self, amp):
115         self.amp = amp
116         self.gui.amplifierEdit.setText(QtCore.QString("%1").arg(self.amp))
117
118
119     # Functions called when signals are triggered in the GUI
120     def frequencyEditText(self):
121         try:
122             freq = eng_notation.str_to_num(self.gui.frequencyEdit.text().toAscii()) 
123             self.fg.set_frequency(freq)
124             self.freq = freq
125         except RuntimeError:
126             pass
127
128     def gainEditText(self):
129         try:
130             gain = float(self.gui.gainEdit.text())
131             self.fg.set_gain(gain)
132             self.gain = gain
133         except ValueError:
134             pass
135                 
136     def bandwidthEditText(self):
137         try:
138             bw = eng_notation.str_to_num(self.gui.bandwidthEdit.text().toAscii())
139             self.fg.set_bandwidth(bw)
140             self.bw = bw
141         except ValueError:
142             pass
143         
144     def amplifierEditText(self):
145         try:
146             amp = float(self.gui.amplifierEdit.text())
147             self.fg.set_amplifier_gain(amp)
148             self.amp = amp
149         except ValueError:
150             pass
151
152     def saveData(self):
153         fileName = QtGui.QFileDialog.getSaveFileName(self, "Save data to file", ".");
154         if(len(fileName)):
155             self.fg.save_to_file(str(fileName))
156
157     def dcGainEditText(self):
158         gain = float(self.gui.dcGainEdit.text())
159         self.fg.set_dc_gain(gain)
160         
161     def dcCancelClicked(self, state):
162         self.dcGainEditText()
163         self.fg.cancel_dc(state)
164         
165
166         
167 class my_top_block(gr.top_block):
168     def __init__(self):
169         gr.top_block.__init__(self)
170
171         parser = OptionParser(option_class=eng_option)
172         parser.add_option("-e", "--interface", type="string", default="eth0",
173                           help="select Ethernet interface, default is eth0")
174         parser.add_option("-m", "--mac-addr", type="string", default="",
175                           help="select USRP by MAC address, default is auto-select")
176         parser.add_option("-W", "--bw", type="float", default=1e6,
177                           help="set bandwidth of receiver [default=%default]")
178         parser.add_option("-f", "--freq", type="eng_float", default="2.4G",
179                           help="set frequency to FREQ", metavar="FREQ")
180         parser.add_option("-g", "--gain", type="eng_float", default=None,
181                           help="set gain in dB (default is midpoint)")
182         parser.add_option("--fft-size", type="int", default=2048,
183                           help="Set number of FFT bins [default=%default]")
184         (options, args) = parser.parse_args()
185
186         if len(args) != 0:
187             parser.print_help()
188             sys.exit(1)
189         self.options = options
190         self.show_debug_info = True
191         
192         self.qapp = QtGui.QApplication(sys.argv)
193
194 #        self.u = usrp2.source_32fc(options.interface, options.mac_addr)
195         self.u = msdd_rs.source_simple("192.168.1.20", 10000);
196         self.conv = gr.interleaved_short_to_complex();
197         self._adc_rate = self.u.pull_adc_freq()
198         self.set_bandwidth(options.bw)
199
200         if options.gain is None:
201             # if no gain was specified, use the mid-point in dB
202 #            g = self.u.gain_range()    
203             g = [0, 10]
204             #options.gain = float(g[0]+g[1])/2
205             options.gain = float(0)
206         self.set_gain(options.gain)
207
208         if options.freq is None:
209                 options.freq = 2.4e9;
210 #            # if no frequency was specified, use the mid-point of the subdev
211 #            f = self.u.freq_range()
212 #            options.freq = float(f[0]+f[1])/2
213
214         self.set_frequency(options.freq)
215
216         self._fftsize = options.fft_size
217
218
219         self._freq = options.freq;
220         self._bandwidth = 400;
221         
222         self.set_bandwidth(self._bandwidth);
223
224         self.snk = qtgui.sink_c(options.fft_size, gr.firdes.WIN_BLACKMAN_hARRIS,
225                                 self._freq, self._bandwidth,
226                                 "USRP2 Display",
227                                 True, True, False, True, False)
228
229         # Set up internal amplifier
230         self.amp = gr.multiply_const_cc(0.0)
231         self.set_amplifier_gain(0.01)
232
233         # Create a single-pole IIR filter to remove DC
234         #   but don't connect it yet
235         self.dc_gain = 0.001
236         self.dc = gr.single_pole_iir_filter_cc(self.dc_gain)
237         self.dc_sub = gr.sub_cc()
238
239         self.agc = gr.agc2_cc(1e-3, 1e-5, 0.01, 0.01, 10);
240
241         self.connect(self.u, self.conv, self.snk)
242         #self.connect(self.u, self.conv, self.amp, self.snk)
243
244         if self.show_debug_info:
245             print "Decimation rate: ", self._decim
246             print "Bandwidth: ", self._bandwidth
247 #            print "D'board: ", self.u.daughterboard_id()
248
249         # Get the reference pointer to the SpectrumDisplayForm QWidget
250         # Wrap the pointer as a PyQt SIP object
251         #     This can now be manipulated as a PyQt4.QtGui.QWidget
252         self.pysink = sip.wrapinstance(self.snk.pyqwidget(), QtGui.QWidget)
253
254         self.main_win = main_window(self.pysink, self)
255
256         self.main_win.set_frequency(self._freq)
257         self.main_win.set_gain(self._gain)
258         self.main_win.set_bandwidth(self._bandwidth)
259         self.main_win.set_amplifier(self._amp_value)
260
261         self.main_win.show()
262
263
264     def save_to_file(self, name):
265         self.lock()
266
267         # Add file sink to save data
268         self.file_sink = gr.file_sink(gr.sizeof_gr_complex, name)
269         self.connect(self.conv, self.file_sink)
270
271         self.unlock()
272
273     def set_gain(self, gain):
274         self._gain = gain
275         self.u.set_ddc_gain(self._gain)
276
277     def set_frequency(self, freq):
278         self._freq = freq
279         r = self.u.set_rx_freq(freq)
280
281         try:
282             self.snk.set_frequency_range(self._freq, self._bandwidth)
283         except:
284             pass
285
286     def set_bandwidth(self, bw):
287         self._bandwidth = bw
288         self._decim = int(self._adc_rate / self._bandwidth)
289 #        self.u.set_decim_rate(self._decim)
290         r1 = self.u.set_ddc_samp_rate( bw );
291         r2 = self.u.set_ddc_bw( bw );
292         self.u.start_data();
293
294         print r1
295         print r2;
296         
297         time.sleep(0.05);
298         bw = self.u.pull_ddc_bw();
299         sr = self.u.pull_ddc_samp_rate();
300         fc = self.u.pull_rx_freq();
301         
302         #self.snk.d_bandwidth = sr;
303
304         print bw;
305         print sr;
306         print fc;
307
308 #       sys.exit(-1);
309
310         try:
311             self.snk.set_frequency_range(self._freq, self._bandwidth)
312         except:
313             pass
314
315     def set_amplifier_gain(self, amp):
316         self._amp_value = amp
317         self.amp.set_k(self._amp_value)
318
319     def set_dc_gain(self, gain):
320         self.dc.set_taps(gain)
321         
322     def cancel_dc(self, state):
323         self.lock()
324
325         if(state):
326             self.disconnect(self.u, self.amp)
327             self.connect(self.u, (self.dc_sub,0))
328             self.connect(self.u, self.dc, (self.dc_sub,1))
329             self.connect(self.dc_sub, self.amp)
330         else:
331             self.disconnect(self.dc_sub, self.amp)
332             self.disconnect(self.dc, (self.dc_sub,1))
333             self.disconnect(self.u, self.dc)
334             self.disconnect(self.u, (self.dc_sub,0))
335             self.connect(self.u, self.amp)
336
337         self.unlock()
338
339 def main ():
340     tb = my_top_block()
341     tb.start()
342     tb.u.start_data();
343     tb.snk.exec_();
344
345 if __name__ == '__main__':
346     try:
347         main ()
348     except KeyboardInterrupt:
349         pass
350