Statistics
| Branch: | Tag: | Revision:

root / gr-qtgui / apps / gr_time_plot_f @ eb4305b9

History | View | Annotate | Download (4.9 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_float, 0)
48
    data = scipy.fromfile(fhandle, dtype=scipy.float32, 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):
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._nsigs = len(self._filelist)
67
68
        if(nsamples is None):
69
            self._nsamps = max_nsamples
70
        else:
71
            self._nsamps = nsamples
72
73
        self.qapp = QtGui.QApplication(sys.argv)
74
75
        self.skip = gr.skiphead(gr.sizeof_float, self._start)
76
        self.gui_snk = qtgui.time_sink_f(self._nsamps, self._samp_rate,
77
                                         "GNU Radio Time Plot", self._nsigs)
78
        n = 0
79
        self.srcs = list()
80
        for f in filelist:
81
            data = read_samples(f, self._start, self._nsamps)
82
            self.srcs.append(gr.vector_source_f(data))
83
84
            # Set default labels based on file names
85
            self.gui_snk.set_title(n, "{0}".format(f))
86
            n += 1
87
88
        self.connect(self.srcs[0], self.skip)
89
        self.connect(self.skip, (self.gui_snk, 0))
90
91
        for i,s in enumerate(self.srcs[1:]):
92
            self.connect(s, (self.gui_snk, i+1))
93
94
        self.gui_snk.set_update_time(0);
95
96
        # Get Python Qt references
97
        pyQt  = self.gui_snk.pyqwidget()
98
        self.pyWin = sip.wrapinstance(pyQt, QtGui.QWidget)
99
100
    def get_gui(self):
101
        return self.pyWin
102
103
    def reset(self, newstart, newnsamps):
104
        self.stop()
105
        self.wait()
106
107
        self._start = newstart
108
109
        for s,f in zip(self.srcs, self._filelist):
110
            data = read_samples(f, self._start, newnsamps)
111
            s.set_data(data)
112
            if(len(data) < newnsamps):
113
                newnsamps = len(data)
114
115
        self._nsamps = newnsamps
116
        self.gui_snk.set_nsamps(self._nsamps)
117
118
        self.start()
119
120
def main():
121
    description = "Plots a list of files on a scope plot. Files are a binary list of floats."
122
    parser = OptionParser(option_class=eng_option, description=description,
123
                          conflict_handler="resolve")
124
    parser.add_option("-N", "--nsamples", type="int", default=None,
125
                      help="Set the number of samples to display [default=prints entire file]")
126
    parser.add_option("-S", "--start", type="int", default=0,
127
                      help="Starting sample number [default=%default]")
128
    parser.add_option("-r", "--sample-rate", type="eng_float", default=1.0,
129
                      help="Set the sample rate of the signal [default=%default]")
130
    (options, args) = parser.parse_args()
131
132
    if(len(args) < 1):
133
        parser.print_help()
134
        sys.exit(0)
135
136
    filelist = list(args)
137
138
    nsamples = options.nsamples
139
140
    # Find the smallest number of samples in all files and use that as
141
    # a maximum value possible.
142
    filesizes = []
143
    for f in filelist:
144
        if(os.path.exists(f)):
145
            filesizes.append(os.path.getsize(f) / gr.sizeof_float)
146
    max_nsamples = min(filesizes)
147
148
    tb = gr_time_plot_f(filelist, options.sample_rate,
149
                        options.start, nsamples, max_nsamples);
150
151
    main_box = dialog_box(tb, 'GNU Radio Time Plot')
152
    main_box.show()
153
154
    tb.run()
155
    tb.qapp.exec_()
156
157
if __name__ == "__main__":
158
    try:
159
        main()
160
    except KeyboardInterrupt:
161
        pass
162