Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gr-utils / src / python / gr_plot_qt.py
1 #!/usr/bin/env python
2
3 try:
4     import scipy
5     from scipy import fftpack
6 except ImportError:
7     print "Please install SciPy to run this script (http://www.scipy.org/)"
8     raise SystemExit, 1
9
10 try:
11     from matplotlib import mlab
12 except ImportError:
13     print "Please install Matplotlib to run this script (http://matplotlib.sourceforge.net)"
14     raise SystemExit, 1
15
16 try:
17     from PyQt4 import Qt, QtCore, QtGui
18 except ImportError:
19     print "Please install PyQt4 to run this script (http://www.riverbankcomputing.co.uk/software/pyqt/download)"
20     raise SystemExit, 1
21
22 try:
23     import PyQt4.Qwt5 as Qwt
24 except ImportError:
25     print "Please install PyQwt5 to run this script (http://pyqwt.sourceforge.net/)"
26     raise SystemExit, 1
27
28 try:
29     # FIXME: reenable this before committing
30     #from gnuradio.pyqt_plot import Ui_MainWindow
31     from pyqt_plot import Ui_MainWindow
32 except ImportError:
33     print "Could not import from pyqt_plot. Please build with \"pyuic4 pyqt_plot.ui -o pyqt_plot.py\""
34     raise SystemExit, 1
35
36 import sys, os
37 from optparse import OptionParser
38 from gnuradio import eng_notation
39
40
41 class SpectrogramData(Qwt.QwtRasterData):
42
43     def __init__(self, f, t):
44         Qwt.QwtArrayData.__init__(self, Qt.QRectF(0, 0, 0, 0))
45         self.sp = scipy.array([[0], [0]])
46
47     def set_data(self, xfreq, ytime, data):
48         self.sp = data
49         self.freq = xfreq
50         self.time = ytime
51         boundingBox = Qt.QRectF(self.freq.min(), self.time.min(),
52                                 self.freq.max() - self.freq.min(),
53                                 self.time.max() - self.time.min())
54         self.setBoundingRect(boundingBox)
55
56     def rasterHint(self, rect):
57         return Qt.QSize(self.sp.shape[0], self.sp.shape[1])
58         
59     def copy(self):
60         return self
61
62     def range(self):
63         
64         return Qwt.QwtDoubleInterval(self.sp.min(), self.sp.max())
65
66     def value(self, x, y):
67         try:
68             f = int(self.freq.searchsorted(x))
69             t = int(self.time.searchsorted(y))
70             return self.sp[f][t-1]
71         except AttributeError: # if no file loaded yet
72             return 0
73
74
75 class gr_plot_qt(QtGui.QMainWindow):
76     def __init__(self, qapp, filename, options, parent=None):
77         QtGui.QWidget.__init__(self, parent)
78         self.gui = Ui_MainWindow()
79         self.gui.setupUi(self)
80                        
81         self.filename = None
82         self.block_length = options.block_length
83         self.start = options.start
84         self.sample_rate = options.sample_rate
85         self.psdfftsize = options.psd_size
86         self.specfftsize = options.spec_size
87         self.winfunc = scipy.blackman
88         self.sizeof_data = 8
89         self.datatype = scipy.complex64
90         self.pen_width = 1
91         self.iq = list()
92         self.time = list()
93
94         # Set up basic plot attributes
95         self.gui.timePlot.setAxisTitle(self.gui.timePlot.xBottom, "Time (sec)")
96         self.gui.timePlot.setAxisTitle(self.gui.timePlot.yLeft, "Amplitude (V)")
97         self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.xBottom, "Frequency (Hz)")
98         self.gui.freqPlot.setAxisTitle(self.gui.freqPlot.yLeft, "Magnitude (dB)")
99         self.gui.specPlot.setAxisTitle(self.gui.specPlot.xBottom, "Frequency (Hz)")
100         self.gui.specPlot.setAxisTitle(self.gui.specPlot.yLeft, "Time (sec)")
101
102         # Set up FFT size combo box
103         self.fftsizes = ["128", "256", "512", "1024", "2048",
104                          "4096", "8192", "16384", "32768"]
105         self.gui.psdFFTComboBox.addItems(self.fftsizes)
106         self.gui.specFFTComboBox.addItems(self.fftsizes)
107         pos = self.gui.psdFFTComboBox.findText(Qt.QString("%1").arg(self.psdfftsize))
108         self.gui.psdFFTComboBox.setCurrentIndex(pos)
109         pos = self.gui.specFFTComboBox.findText(Qt.QString("%1").arg(self.specfftsize))
110         self.gui.specFFTComboBox.setCurrentIndex(pos)
111
112         self.connect(self.gui.psdFFTComboBox,
113                      Qt.SIGNAL("activated (const QString&)"),
114                      self.psdFFTComboBoxEdit)
115         self.connect(self.gui.specFFTComboBox,
116                      Qt.SIGNAL("activated (const QString&)"),
117                      self.specFFTComboBoxEdit)
118
119         # Set up color scheme box
120         self.color_modes = {"Black on White" : self.color_black_on_white,
121                             "White on Black" : self.color_white_on_black,
122                             "Blue on Black"  : self.color_blue_on_black,
123                             "Green on Black" : self.color_green_on_black}
124         self.gui.colorComboBox.addItems(self.color_modes.keys())
125         pos = self.gui.colorComboBox.findText("Blue on Black")
126         self.gui.colorComboBox.setCurrentIndex(pos)
127         self.connect(self.gui.colorComboBox,
128                      Qt.SIGNAL("activated (const QString&)"),
129                      self.colorComboBoxEdit)
130         
131         
132         # Set up line style combo box
133         self.line_styles = {"None" : Qwt.QwtSymbol.NoSymbol,
134                             "Circle" : Qwt.QwtSymbol.Ellipse,
135                             "Diamond"  : Qwt.QwtSymbol.Rect,
136                             "Triangle" : Qwt.QwtSymbol.Triangle}
137         self.gui.lineStyleComboBox.addItems(self.line_styles.keys())
138         pos = self.gui.lineStyleComboBox.findText("None")
139         self.gui.lineStyleComboBox.setCurrentIndex(pos)
140         self.connect(self.gui.lineStyleComboBox,
141                      Qt.SIGNAL("activated (const QString&)"),
142                      self.lineStyleComboBoxEdit)
143
144         # Create zoom functionality for the plots
145         self.timeZoomer = Qwt.QwtPlotZoomer(self.gui.timePlot.xBottom,
146                                             self.gui.timePlot.yLeft,
147                                             Qwt.QwtPicker.PointSelection,
148                                             Qwt.QwtPicker.AlwaysOn,
149                                             self.gui.timePlot.canvas())
150
151         self.freqZoomer = Qwt.QwtPlotZoomer(self.gui.freqPlot.xBottom,
152                                             self.gui.freqPlot.yLeft,
153                                             Qwt.QwtPicker.PointSelection,
154                                             Qwt.QwtPicker.AlwaysOn,
155                                             self.gui.freqPlot.canvas())
156
157         self.specZoomer = Qwt.QwtPlotZoomer(self.gui.specPlot.xBottom,
158                                             self.gui.specPlot.yLeft,
159                                             Qwt.QwtPicker.PointSelection,
160                                             Qwt.QwtPicker.AlwaysOn,
161                                             self.gui.specPlot.canvas())
162
163         # Set up action when tab is changed
164         self.connect(self.gui.tabGroup,
165                      Qt.SIGNAL("currentChanged (int)"),
166                      self.tabChanged)
167
168         # Add a legend to the Time plot
169         legend_real = Qwt.QwtLegend()
170         self.gui.timePlot.insertLegend(legend_real)
171
172         # Set up slider
173         self.gui.plotHBar.setSingleStep(1)
174         self.gui.plotHBar.setPageStep(self.block_length)
175         self.gui.plotHBar.setMinimum(0)
176         self.gui.plotHBar.setMaximum(self.block_length)
177         self.connect(self.gui.plotHBar,
178                      Qt.SIGNAL("valueChanged(int)"),
179                      self.sliderMoved)
180
181         # Connect Open action to Open Dialog box
182         self.connect(self.gui.action_open,
183                      Qt.SIGNAL("activated()"),
184                      self.open_file)
185
186         # Connect Reload action to reload the file
187         self.connect(self.gui.action_reload,
188                      Qt.SIGNAL("activated()"),
189                      self.reload_file)
190         self.gui.action_reload.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+R",
191                                                                         None, QtGui.QApplication.UnicodeUTF8))
192
193         # Set up file position boxes to update current figure
194         self.connect(self.gui.filePosStartLineEdit,
195                      Qt.SIGNAL("editingFinished()"),
196                      self.file_position_changed)
197         self.connect(self.gui.filePosStopLineEdit,
198                      Qt.SIGNAL("editingFinished()"),
199                      self.file_position_changed)
200         self.connect(self.gui.filePosLengthLineEdit,
201                      Qt.SIGNAL("editingFinished()"),
202                      self.file_length_changed)
203
204         self.connect(self.gui.fileTimeStartLineEdit,
205                      Qt.SIGNAL("editingFinished()"),
206                      self.file_time_changed)
207         self.connect(self.gui.fileTimeStopLineEdit,
208                      Qt.SIGNAL("editingFinished()"),
209                      self.file_time_changed)
210         self.connect(self.gui.fileTimeLengthLineEdit,
211                      Qt.SIGNAL("editingFinished()"),
212                      self.file_time_length_changed)
213
214         stylestr = str(self.gui.lineStyleComboBox.currentText().toAscii())
215         style = self.line_styles[stylestr]
216
217         self.rcurve = Qwt.QwtPlotCurve("Real")
218         self.icurve = Qwt.QwtPlotCurve("Imaginary")
219         self.rsym = Qwt.QwtSymbol()
220         self.rsym.setStyle(style)
221         self.rsym.setSize(10)
222         self.isym = Qwt.QwtSymbol()
223         self.isym.setStyle(style)
224         self.isym.setSize(10)
225         self.rcurve.setSymbol(self.rsym)
226         self.icurve.setSymbol(self.isym)
227
228
229         self.icurve.attach(self.gui.timePlot)
230         self.rcurve.attach(self.gui.timePlot)
231
232         self.psdcurve = Qwt.QwtPlotCurve("PSD")
233         self.psdcurve.attach(self.gui.freqPlot)
234
235         # Set up specTab plot as a spectrogram
236         self.specdata = SpectrogramData(range(0, 10), range(0, 10))
237
238         colorMap = Qwt.QwtLinearColorMap(Qt.Qt.darkCyan, Qt.Qt.red)
239         colorMap.addColorStop(0.1, Qt.Qt.cyan)
240         colorMap.addColorStop(0.6, Qt.Qt.green)
241         colorMap.addColorStop(0.95, Qt.Qt.yellow)
242
243         self.spec = Qwt.QwtPlotSpectrogram()
244         self.spec.setColorMap(colorMap)
245         self.spec.attach(self.gui.specPlot)
246         self.spec.setDisplayMode(Qwt.QwtPlotSpectrogram.ImageMode, True)
247         self.spec.setData(self.specdata)
248
249         self.rightAxis = self.gui.specPlot.axisWidget(Qwt.QwtPlot.yRight)
250         self.rightAxis.setTitle("Magnitude (dBm)")
251         self.rightAxis.setColorBarEnabled(True)
252         self.rightAxis.setColorMap(self.spec.data().range(),
253                                    self.spec.colorMap())
254         self.gui.specPlot.enableAxis(Qwt.QwtPlot.yRight)
255
256         # Set up initial color scheme
257         self.color_modes["Blue on Black"]()
258
259         # When line width spin box changes, update the pen size
260         self.connect(self.gui.lineWidthSpinBox,
261                      Qt.SIGNAL("valueChanged(int)"),
262                      self.change_pen_width)
263         self.gui.lineWidthSpinBox.setRange(1, 10)
264
265         # When style size spin box changes, update the pen size
266         self.connect(self.gui.styleSizeSpinBox,
267                      Qt.SIGNAL("valueChanged(int)"),
268                      self.change_style_size)
269         self.gui.styleSizeSpinBox.setRange(1, 20)
270         self.gui.styleSizeSpinBox.setValue(5)
271
272
273         # Connect a signal for when the sample rate changes
274         self.set_sample_rate(self.sample_rate)
275         self.connect(self.gui.sampleRateLineEdit,
276                      Qt.SIGNAL("editingFinished()"),
277                      self.sample_rate_changed)
278
279         if(filename is not None):
280             self.initialize(filename)
281
282         self.show()
283
284     def open_file(self):
285         filename = Qt.QFileDialog.getOpenFileName(self, "Open", ".")
286         if(filename != ""):
287             #print filename
288             self.initialize(filename)
289
290     def reload_file(self):
291         if(self.filename):
292             self.initialize(self.filename)
293         
294     def initialize(self, filename):
295         self.filename = filename
296         self.hfile = open(filename, "r")
297
298         self.setWindowTitle(("GNU Radio File Plot Utility: %s" % filename))
299
300         self.gui.filePosStartLineEdit.setText("0")
301         self.gui.filePosStopLineEdit.setText("0")
302         self.gui.fileTimeStartLineEdit.setText("0")
303         self.gui.fileTimeStopLineEdit.setText("0")
304
305         self.cur_start = 0
306         self.cur_stop = self.block_length
307
308         self.init_data_input()
309         self.get_data(self.cur_start, self.cur_stop)
310         self.get_psd()
311         self.get_specgram() 
312         self.gui.plotHBar.setSliderPosition(0)
313         self.gui.plotHBar.setMaximum(self.signal_size-self.block_length)
314
315
316         self.update_time_curves()
317         self.update_psd_curves()
318         self.update_specgram_curves()
319
320     def init_data_input(self):
321         self.hfile.seek(0, os.SEEK_END)
322         self.signal_size = self.hfile.tell()/self.sizeof_data
323         #print "Sizeof File: ", self.signal_size
324         self.hfile.seek(0, os.SEEK_SET)
325         
326     def get_data(self, start, end):
327         if(end > start):
328             self.hfile.seek(start*self.sizeof_data, os.SEEK_SET)
329             self.position = start
330             try:
331                 iq = scipy.fromfile(self.hfile, dtype=self.datatype,
332                                     count=end-start)
333
334                 if(len(iq) < (end-start)):
335                     end = start + len(iq)
336                     self.gui.filePosLengthLineEdit.setText(Qt.QString("%1").arg(len(iq)))
337                     self.file_length_changed()
338
339                 tstep = 1.0 / self.sample_rate
340                 self.iq = iq
341                 self.time = [tstep*(self.position + i) for i in xrange(len(self.iq))]
342
343                 self.set_file_pos_box(start, end)
344             except MemoryError:
345                 pass
346         else:
347             # Do we want to do anything about this?
348             pass
349
350     def get_psd(self):
351         winpoints = self.winfunc(self.psdfftsize)
352         iq_psd, freq = mlab.psd(self.iq, Fs=self.sample_rate,
353                                 NFFT=self.psdfftsize,
354                                 noverlap=self.psdfftsize/4.0,
355                                 window=winpoints,
356                                 scale_by_freq=False)
357
358         self.iq_psd = 10.0*scipy.log10(abs(fftpack.fftshift(iq_psd)))
359         self.freq = freq - self.sample_rate/2.0
360
361     def get_specgram(self):
362         winpoints = self.winfunc(self.specfftsize)
363         iq_spec, f, t = mlab.specgram(self.iq, Fs=self.sample_rate,
364                                       NFFT=self.specfftsize,
365                                       noverlap=self.specfftsize/4.0,
366                                       window=winpoints,
367                                       scale_by_freq=False)
368         
369         self.iq_spec = 10.0*scipy.log10(abs(iq_spec))
370         self.spec_f = f
371         self.spec_t = t
372
373     def psdFFTComboBoxEdit(self, fftSize):
374         self.psdfftsize = fftSize.toInt()[0]
375         self.get_psd()
376         self.update_psd_curves()
377
378     def specFFTComboBoxEdit(self, fftSize):
379         self.specfftsize = fftSize.toInt()[0]
380         self.get_specgram()
381         self.update_specgram_curves()
382         
383     def colorComboBoxEdit(self, colorSelection):
384         colorstr = str(colorSelection.toAscii())
385         color_func = self.color_modes[colorstr]
386         color_func()
387
388     def lineStyleComboBoxEdit(self, styleSelection):
389         stylestr = str(styleSelection.toAscii())
390         self.rsym.setStyle(self.line_styles[stylestr])
391         self.isym.setStyle(self.line_styles[stylestr])
392         self.rcurve.setSymbol(self.rsym)
393         self.icurve.setSymbol(self.isym)
394         self.gui.timePlot.replot()
395
396     def sliderMoved(self, value):
397         pos_start = value
398         pos_end = value + self.gui.plotHBar.pageStep()
399
400         self.get_data(pos_start, pos_end)
401         self.get_psd()
402         self.get_specgram()
403         self.update_time_curves()
404         self.update_psd_curves()
405         self.update_specgram_curves()
406
407     def set_sample_rate(self, sr):
408         self.sample_rate = sr
409         srstr = eng_notation.num_to_str(self.sample_rate)
410         self.gui.sampleRateLineEdit.setText(Qt.QString("%1").arg(srstr))
411
412     def sample_rate_changed(self):
413         srstr = self.gui.sampleRateLineEdit.text().toAscii()
414         self.sample_rate = eng_notation.str_to_num(srstr)
415         self.set_file_pos_box(self.cur_start, self.cur_stop)
416         self.get_data(self.cur_start, self.cur_stop)
417         self.get_psd()
418         self.get_specgram()
419         self.update_time_curves()
420         self.update_psd_curves()
421         self.update_specgram_curves()
422
423     def set_file_pos_box(self, start, end):
424         tstart = start / self.sample_rate
425         tend = end / self.sample_rate
426
427         self.gui.filePosStartLineEdit.setText(Qt.QString("%1").arg(start))
428         self.gui.filePosStopLineEdit.setText(Qt.QString("%1").arg(end))
429         self.gui.filePosLengthLineEdit.setText(Qt.QString("%1").arg(end-start))
430
431         self.gui.fileTimeStartLineEdit.setText(Qt.QString("%1").arg(tstart))
432         self.gui.fileTimeStopLineEdit.setText(Qt.QString("%1").arg(tend))
433         self.gui.fileTimeLengthLineEdit.setText(Qt.QString("%1").arg(tend-tstart))
434
435     def file_position_changed(self):
436         start  = self.gui.filePosStartLineEdit.text().toInt()
437         end    = self.gui.filePosStopLineEdit.text().toInt()
438         if((start[1] == True) and (end[1] == True)):
439             self.cur_start = start[0]
440             self.cur_stop = end[0]
441
442             tstart = self.cur_start / self.sample_rate
443             tend = self.cur_stop / self.sample_rate
444             self.gui.fileTimeStartLineEdit.setText(Qt.QString("%1").arg(tstart))
445             self.gui.fileTimeStopLineEdit.setText(Qt.QString("%1").arg(tend))
446             
447             self.get_data(self.cur_start, self.cur_stop)
448
449             self.update_time_curves()
450             self.update_psd_curves()
451             self.update_specgram_curves()
452
453         # If there's a non-digit character, reset box
454         else:
455             try:
456                 self.set_file_pos_box(self.cur_start, self.cur_stop)
457             except AttributeError:
458                 pass
459             
460
461     def file_time_changed(self):
462         tstart = self.gui.fileTimeStartLineEdit.text().toDouble()
463         tstop  = self.gui.fileTimeStopLineEdit.text().toDouble()
464         if((tstart[1] == True) and (tstop[1] == True)):
465             self.cur_start = int(tstart[0] * self.sample_rate)
466             self.cur_stop = int(tstop[0] * self.sample_rate)
467             self.get_data(self.cur_start, self.cur_stop)
468
469             self.gui.filePosStartLineEdit.setText(Qt.QString("%1").arg(self.cur_start))
470             self.gui.filePosStopLineEdit.setText(Qt.QString("%1").arg(self.cur_stop))
471
472             self.update_time_curves()
473             self.update_psd_curves()
474             self.update_specgram_curves()
475         # If there's a non-digit character, reset box
476         else:
477             self.set_file_pos_box(self.cur_start, self.cur_stop)
478
479     def file_length_changed(self):
480         start = self.gui.filePosStartLineEdit.text().toInt()
481         length = self.gui.filePosLengthLineEdit.text().toInt()
482
483         if((start[1] == True) and (length[1] == True)):
484             self.cur_start = start[0]
485             self.block_length = length[0]
486             self.cur_stop = self.cur_start + self.block_length
487
488             tstart = self.cur_start / self.sample_rate
489             tend = self.cur_stop / self.sample_rate
490             tlen = self.block_length / self.sample_rate
491             self.gui.fileTimeStartLineEdit.setText(Qt.QString("%1").arg(tstart))
492             self.gui.fileTimeStopLineEdit.setText(Qt.QString("%1").arg(tend))
493             self.gui.fileTimeLengthLineEdit.setText(Qt.QString("%1").arg(tlen))
494
495             self.gui.plotHBar.setPageStep(self.block_length)
496
497             self.get_data(self.cur_start, self.cur_stop)
498             self.get_psd()
499             self.get_specgram()
500
501             self.update_time_curves()
502             self.update_psd_curves()
503             self.update_specgram_curves()
504         # If there's a non-digit character, reset box
505         else:
506             self.set_file_pos_box(self.cur_start, self.cur_stop)
507
508     def file_time_length_changed(self):
509         tstart = self.gui.fileTimeStartLineEdit.text().toDouble()
510         tlength = self.gui.fileTimeLengthLineEdit.text().toDouble()
511         if((tstart[1] == True) and (tlength[1] == True)):
512             self.cur_start = int(tstart[0] * self.sample_rate)
513             self.block_length = int(tlength[0] * self.sample_rate)
514             self.cur_stop = self.cur_start + self.block_length
515
516             tstart = self.cur_start / self.sample_rate
517             tend = self.cur_stop / self.sample_rate
518             tlen = self.block_length / self.sample_rate
519             self.gui.fileTimeStartLineEdit.setText(Qt.QString("%1").arg(tstart))
520             self.gui.fileTimeStopLineEdit.setText(Qt.QString("%1").arg(tend))
521             self.gui.fileTimeLengthLineEdit.setText(Qt.QString("%1").arg(tlen))
522
523             self.get_data(self.cur_start, self.cur_stop)
524             self.get_psd()
525             self.get_specgram()
526
527             self.update_time_curves()
528             self.update_psd_curves()
529             self.update_specgram_curves()
530         # If there's a non-digit character, reset box
531         else:
532             self.set_file_pos_box(self.cur_start, self.cur_stop)
533
534
535     def update_time_curves(self):
536         self.icurve.setData(self.time, self.iq.imag)
537         self.rcurve.setData(self.time, self.iq.real)
538
539         # Reset the x-axis to the new time scale
540         iqmax = 1.5 * max(max(self.iq.real), max(self.iq.imag))
541         iqmin = 1.5 * min(min(self.iq.real), min(self.iq.imag))
542         self.gui.timePlot.setAxisScale(self.gui.timePlot.xBottom,
543                                        min(self.time),
544                                        max(self.time))
545         self.gui.timePlot.setAxisScale(self.gui.timePlot.yLeft,
546                                        iqmin,
547                                        iqmax)
548
549         # Set the zoomer base to unzoom to the new axis
550         self.timeZoomer.setZoomBase()
551     
552         self.gui.timePlot.replot()
553         
554     def update_psd_curves(self):
555         self.psdcurve.setData(self.freq, self.iq_psd)
556
557         self.gui.freqPlot.setAxisScale(self.gui.freqPlot.xBottom,
558                                        min(self.freq),
559                                        max(self.freq))
560                                        
561         # Set the zoomer base to unzoom to the new axis
562         self.freqZoomer.setZoomBase()
563
564         self.gui.freqPlot.replot()
565
566     def update_specgram_curves(self):
567         # We don't have to reset the data for the speccurve here
568         # since this is taken care of in the SpectrogramData class
569         self.specdata.set_data(self.spec_f, self.spec_t, self.iq_spec)
570
571         # Set the color map based on the new data
572         self.rightAxis.setColorMap(self.spec.data().range(),
573                                    self.spec.colorMap())
574
575         # Set the new axis base; include right axis for the intenisty color bar
576         self.gui.specPlot.setAxisScale(self.gui.specPlot.xBottom,
577                                        min(self.spec_f),
578                                        max(self.spec_f))
579         self.gui.specPlot.setAxisScale(self.gui.specPlot.yLeft,
580                                        min(self.spec_t),
581                                        max(self.spec_t))
582         self.gui.specPlot.setAxisScale(self.gui.specPlot.yRight, 
583                                        self.iq_spec.min(),
584                                        self.iq_spec.max())
585  
586         # Set the zoomer base to unzoom to the new axis
587         self.specZoomer.setZoomBase()
588
589         self.gui.specPlot.replot()
590
591     def tabChanged(self, index):
592         self.gui.timePlot.replot()
593         self.gui.freqPlot.replot()
594         self.gui.specPlot.replot()
595
596     def change_pen_width(self, width):
597         self.pen_width = width
598         colormode = str(self.gui.colorComboBox.currentText().toAscii())
599         color_func = self.color_modes[colormode]()
600
601     def change_style_size(self, size):
602         self.rsym.setSize(size)
603         self.isym.setSize(size)
604         self.rcurve.setSymbol(self.rsym)
605         self.icurve.setSymbol(self.isym)
606         self.gui.timePlot.replot()
607     
608     def color_black_on_white(self):
609         blue = QtGui.qRgb(0x00, 0x00, 0xFF)
610         red = QtGui.qRgb(0xFF, 0x00, 0x00)
611
612         blackPen = Qt.QPen(Qt.QBrush(Qt.QColor("black")), self.pen_width)
613         bluePen = Qt.QPen(Qt.QBrush(Qt.QColor(blue)), self.pen_width)
614         redPen = Qt.QPen(Qt.QBrush(Qt.QColor(red)), self.pen_width)
615
616         self.gui.timePlot.setCanvasBackground(Qt.QColor("white"))
617         self.gui.freqPlot.setCanvasBackground(Qt.QColor("white"))
618         self.timeZoomer.setTrackerPen(blackPen)
619         self.timeZoomer.setRubberBandPen(blackPen)
620         self.freqZoomer.setTrackerPen(blackPen)
621         self.freqZoomer.setRubberBandPen(blackPen)
622         self.psdcurve.setPen(bluePen)
623         self.rcurve.setPen(bluePen)
624         self.icurve.setPen(redPen)
625
626         self.rsym.setPen(bluePen)
627         self.isym.setPen(redPen)
628
629         self.gui.timePlot.replot()
630         self.gui.freqPlot.replot()
631
632     def color_white_on_black(self):
633         white = QtGui.qRgb(0xFF, 0xFF, 0xFF)
634         red = QtGui.qRgb(0xFF, 0x00, 0x00)
635
636         whiteBrush = Qt.QBrush(Qt.QColor("white"))
637         whiteBrush = Qt.QBrush(Qt.QColor(white))
638         redBrush = Qt.QBrush(Qt.QColor(red))
639         
640         self.gui.timePlot.setCanvasBackground(QtGui.QColor("black"))
641         self.gui.freqPlot.setCanvasBackground(QtGui.QColor("black"))
642         self.timeZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
643         self.timeZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
644         self.freqZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
645         self.freqZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
646         self.psdcurve.setPen(Qt.QPen(whiteBrush, self.pen_width))
647         self.rcurve.setPen(Qt.QPen(whiteBrush, self.pen_width))
648         self.icurve.setPen(Qt.QPen(redBrush, self.pen_width))
649
650         self.gui.timePlot.replot()
651         self.gui.freqPlot.replot()
652
653
654     def color_green_on_black(self):
655         green = QtGui.qRgb(0x00, 0xFF, 0x00)
656         red = QtGui.qRgb(0xFF, 0x00, 0x50)
657
658         whiteBrush = Qt.QBrush(Qt.QColor("white"))
659         greenBrush = Qt.QBrush(Qt.QColor(green))
660         redBrush = Qt.QBrush(Qt.QColor(red))
661         
662         self.gui.timePlot.setCanvasBackground(QtGui.QColor("black"))
663         self.gui.freqPlot.setCanvasBackground(QtGui.QColor("black"))
664         self.timeZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
665         self.timeZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
666         self.freqZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
667         self.freqZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
668         self.psdcurve.setPen(Qt.QPen(greenBrush, self.pen_width))
669         self.rcurve.setPen(Qt.QPen(greenBrush, self.pen_width))
670         self.icurve.setPen(Qt.QPen(redBrush, self.pen_width))
671
672         self.gui.timePlot.replot()
673         self.gui.freqPlot.replot()
674
675     def color_blue_on_black(self):
676         blue = QtGui.qRgb(0x00, 0x00, 0xFF)
677         red = QtGui.qRgb(0xFF, 0x00, 0x00)
678
679         whiteBrush = Qt.QBrush(Qt.QColor("white"))
680         blueBrush = Qt.QBrush(Qt.QColor(blue))
681         redBrush = Qt.QBrush(Qt.QColor(red))
682         
683         self.gui.timePlot.setCanvasBackground(QtGui.QColor("black"))
684         self.gui.freqPlot.setCanvasBackground(QtGui.QColor("black"))
685         self.timeZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
686         self.timeZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
687         self.freqZoomer.setTrackerPen(Qt.QPen(whiteBrush, self.pen_width))
688         self.freqZoomer.setRubberBandPen(Qt.QPen(whiteBrush, self.pen_width))
689         self.psdcurve.setPen(Qt.QPen(blueBrush, self.pen_width))
690         self.rcurve.setPen(Qt.QPen(blueBrush, self.pen_width))
691         self.icurve.setPen(Qt.QPen(redBrush, self.pen_width))
692
693         self.gui.timePlot.replot()
694         self.gui.freqPlot.replot()
695
696 def setup_options():
697     usage="%prog: [options] (input_filename)"
698     description = ""
699
700     parser = OptionParser(conflict_handler="resolve", usage=usage, description=description)
701     parser.add_option("-B", "--block-length", type="int", default=8192,
702                       help="Specify the block size [default=%default]")
703     parser.add_option("-s", "--start", type="int", default=0,
704                       help="Specify where to start in the file [default=%default]")
705     parser.add_option("-R", "--sample-rate", type="float", default=1.0,
706                       help="Set the sampler rate of the data [default=%default]")
707     parser.add_option("", "--psd-size", type="int", default=2048,
708                       help="Set the size of the PSD FFT [default=%default]")
709     parser.add_option("", "--spec-size", type="int", default=2048,
710                       help="Set the size of the spectrogram FFT [default=%default]")
711
712     return parser
713
714 def main(args):
715     parser = setup_options()
716     (options, args) = parser.parse_args ()
717
718     if(len(args) == 1):
719         filename = args[0]
720     else:
721         filename = None
722
723     app = Qt.QApplication(args)
724     gplt = gr_plot_qt(app, filename, options)
725     app.exec_()
726
727 if __name__ == '__main__':
728     main(sys.argv)
729