Statistics
| Branch: | Tag: | Revision:

root / gr-qtgui / apps / gr_time_plot_f @ 1b71820a

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