root / gr-qtgui / apps / gr_time_plot_s @ eb4305b9
History | View | Annotate | Download (5.3 kB)
| 1 | #!/usr/bin/env python |
|---|---|
| 2 | # |
| 3 | # Copyright 2012 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.eng_option import eng_option |
| 25 | from optparse import OptionParser |
| 26 | import os, sys |
| 27 | |
| 28 | try: |
| 29 | from gnuradio import qtgui |
| 30 | from PyQt4 import QtGui, QtCore |
| 31 | import sip |
| 32 | except ImportError: |
| 33 | print "Error: Program requires PyQt4 and gr-qtgui." |
| 34 | sys.exit(1) |
| 35 | |
| 36 | try: |
| 37 | import scipy |
| 38 | except ImportError: |
| 39 | print "Error: Scipy required (www.scipy.org)." |
| 40 | sys.exit(1) |
| 41 | |
| 42 | from plot_form import * |
| 43 | |
| 44 | def read_samples(filename, start, in_size): |
| 45 | # Read in_size number of samples from file |
| 46 | fhandle = open(filename, 'r') |
| 47 | fhandle.seek(start*gr.sizeof_short, 0) |
| 48 | data = scipy.fromfile(fhandle, dtype=scipy.int16, count=in_size) |
| 49 | data = data.tolist() |
| 50 | fhandle.close() |
| 51 | |
| 52 | if(len(data) < in_size): |
| 53 | print "Warning: read in {0} samples but asked for {1} samples.".format(
|
| 54 | len(data), in_size) |
| 55 | |
| 56 | return data |
| 57 | |
| 58 | class gr_time_plot_f(gr.top_block): |
| 59 | def __init__(self, filelist, samp_rate, start, nsamples, max_nsamples, scale): |
| 60 | gr.top_block.__init__(self) |
| 61 | |
| 62 | self._filelist = filelist |
| 63 | self._samp_rate = samp_rate |
| 64 | self._start = start |
| 65 | self._max_nsamps = max_nsamples |
| 66 | self._scale = scale |
| 67 | self._nsigs = len(self._filelist) |
| 68 | |
| 69 | if(nsamples is None): |
| 70 | self._nsamps = max_nsamples |
| 71 | else: |
| 72 | self._nsamps = nsamples |
| 73 | |
| 74 | self.qapp = QtGui.QApplication(sys.argv) |
| 75 | |
| 76 | self.skip = gr.skiphead(gr.sizeof_float, self._start) |
| 77 | self.gui_snk = qtgui.time_sink_f(self._nsamps, self._samp_rate, |
| 78 | "GNU Radio Time Plot", self._nsigs) |
| 79 | n = 0 |
| 80 | self.srcs = list() |
| 81 | self.cnvrt = list() |
| 82 | for f in filelist: |
| 83 | data = read_samples(f, self._start, self._nsamps) |
| 84 | self.srcs.append(gr.vector_source_s(data)) |
| 85 | self.cnvrt.append(gr.short_to_float(1, self._scale)) |
| 86 | |
| 87 | # Set default labels based on file names |
| 88 | self.gui_snk.set_title(n, "{0}".format(f))
|
| 89 | n += 1 |
| 90 | |
| 91 | self.connect(self.srcs[0], self.cnvrt[0], self.skip) |
| 92 | self.connect(self.skip, (self.gui_snk, 0)) |
| 93 | |
| 94 | for i,s in enumerate(self.srcs[1:]): |
| 95 | self.connect(s, self.cnvrt[i], (self.gui_snk, i+1)) |
| 96 | |
| 97 | self.gui_snk.set_update_time(0); |
| 98 | |
| 99 | # Get Python Qt references |
| 100 | pyQt = self.gui_snk.pyqwidget() |
| 101 | self.pyWin = sip.wrapinstance(pyQt, QtGui.QWidget) |
| 102 | |
| 103 | def get_gui(self): |
| 104 | return self.pyWin |
| 105 | |
| 106 | def reset(self, newstart, newnsamps): |
| 107 | self.stop() |
| 108 | self.wait() |
| 109 | |
| 110 | self._start = newstart |
| 111 | |
| 112 | for s,f in zip(self.srcs, self._filelist): |
| 113 | data = read_samples(f, self._start, newnsamps) |
| 114 | s.set_data(data) |
| 115 | if(len(data) < newnsamps): |
| 116 | newnsamps = len(data) |
| 117 | |
| 118 | self._nsamps = newnsamps |
| 119 | self.gui_snk.set_nsamps(self._nsamps) |
| 120 | |
| 121 | self.start() |
| 122 | |
| 123 | def main(): |
| 124 | description = "Plots a list of files on a scope plot. Files are a binary list of shorts." |
| 125 | parser = OptionParser(option_class=eng_option, description=description, |
| 126 | conflict_handler="resolve") |
| 127 | parser.add_option("-N", "--nsamples", type="int", default=None,
|
| 128 | help="Set the number of samples to display [default=prints entire file]") |
| 129 | parser.add_option("-S", "--start", type="int", default=0,
|
| 130 | help="Starting sample number [default=%default]") |
| 131 | parser.add_option("-r", "--sample-rate", type="eng_float", default=1.0,
|
| 132 | help="Set the sample rate of the signal [default=%default]") |
| 133 | parser.add_option("-s", "--scale", type="eng_float", default=2**(16-1)-1,
|
| 134 | help="Set a scaling factor for the short->float conversion [default=%default]") |
| 135 | (options, args) = parser.parse_args() |
| 136 | |
| 137 | if(len(args) < 1): |
| 138 | parser.print_help() |
| 139 | sys.exit(0) |
| 140 | |
| 141 | filelist = list(args) |
| 142 | |
| 143 | nsamples = options.nsamples |
| 144 | |
| 145 | # Find the smallest number of samples in all files and use that as |
| 146 | # a maximum value possible. |
| 147 | filesizes = [] |
| 148 | for f in filelist: |
| 149 | if(os.path.exists(f)): |
| 150 | filesizes.append(os.path.getsize(f) / gr.sizeof_short) |
| 151 | max_nsamples = min(filesizes) |
| 152 | |
| 153 | tb = gr_time_plot_f(filelist, options.sample_rate, |
| 154 | options.start, nsamples, max_nsamples, |
| 155 | options.scale); |
| 156 | |
| 157 | main_box = dialog_box(tb, 'GNU Radio Time Plot') |
| 158 | main_box.show() |
| 159 | |
| 160 | tb.run() |
| 161 | tb.qapp.exec_() |
| 162 | |
| 163 | if __name__ == "__main__": |
| 164 | try: |
| 165 | main() |
| 166 | except KeyboardInterrupt: |
| 167 | pass |
| 168 |