Statistics
| Branch: | Tag: | Revision:

root / gr-qtgui / apps / gr_psd_plot_i @ eb4305b9

History | View | Annotate | Download (6.2 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
try:
43
    from gnuradio.qtgui.plot_form import *
44
except ImportError:
45
    from plot_form import *
46
47
def read_samples_and_pad(filename, start, in_size, min_size):
48
    # Read in_size number of samples from file
49
    fhandle = open(filename, 'r')
50
    fhandle.seek(start*gr.sizeof_int, 0)
51
    data = scipy.fromfile(fhandle, dtype=scipy.int32, count=in_size)
52
    data = data.tolist()
53
    fhandle.close()
54
55
    # If we have to, append 0's to create min_size samples of data
56
    if(len(data) < min_size):
57
        data += (min_size - len(data)) * [scipy.int32(0)]
58
59
    return data
60
61
class my_top_block(gr.top_block):
62
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
63
                 nsamples, max_nsamples, scale, avg=1.0):
64
        gr.top_block.__init__(self)
65
66
        self._filelist = filelist
67
        self._center_freq = fc
68
        self._samp_rate = samp_rate
69
        self._psd_size = psdsize
70
        self._start = start
71
        self._max_nsamps = max_nsamples
72
        self._scale = scale
73
        self._nsigs = len(self._filelist)
74
        self._avg = avg
75
76
        if(nsamples is None):
77
            self._nsamps = max_nsamples
78
        else:
79
            self._nsamps = nsamples
80
81
        self.qapp = QtGui.QApplication(sys.argv)
82
83
        self.skip = gr.skiphead(gr.sizeof_float, self._start)
84
        self.gui_snk = qtgui.freq_sink_f(self._psd_size, gr.firdes.WIN_BLACKMAN_hARRIS,
85
                                         self._center_freq, self._samp_rate,
86
                                         "GNU Radio PSD Plot", self._nsigs)
87
        n = 0
88
        self.srcs = list()
89
        self.cnvrt = list()
90
        for f in filelist:
91
            data = read_samples_and_pad(f, self._start,
92
                                        self._nsamps, self._psd_size)
93
            self.srcs.append(gr.vector_source_i(data))
94
            self.cnvrt.append(gr.int_to_float(1, self._scale))
95
96
            # Set default labels based on file names
97
            self.gui_snk.set_title(n, "{0}".format(f))
98
            n += 1
99
100
        self.connect(self.srcs[0], self.cnvrt[0], self.skip)
101
        self.connect(self.skip, (self.gui_snk, 0))
102
103
        for i,s in enumerate(self.srcs[1:]):
104
            self.connect(s, self.cnvrt[i], (self.gui_snk, i+1))
105
106
        self.gui_snk.set_update_time(0);
107
        self.gui_snk.set_fft_average(self._avg)
108
109
        # Get Python Qt references
110
        pyQt = self.gui_snk.pyqwidget()
111
        self.pyWin = sip.wrapinstance(pyQt, QtGui.QWidget)
112
113
    def get_gui(self):
114
        return self.pyWin
115
116
    def reset(self, newstart, newnsamps):
117
        self.stop()
118
        self.wait()
119
120
        self._start = newstart
121
        self._nsamps = newnsamps
122
123
        for s,f in zip(self.srcs, self._filelist):
124
            data = read_samples_and_pad(f, self._start,
125
                                        self._nsamps, self._psd_size)
126
            s.set_data(data)
127
128
        self.start()
129
130
def main():
131
    description = "Plots the PSDs of a list of files. Files are a binary list of integers."
132
    parser = OptionParser(option_class=eng_option, description=description,
133
                          conflict_handler="resolve")
134
    parser.add_option("-N", "--nsamples", type="int", default=None,
135
                      help="Set the number of samples to display [default=prints entire file]")
136
    parser.add_option("-S", "--start", type="int", default=0,
137
                      help="Starting sample number [default=%default]")
138
    parser.add_option("-L", "--psd-size", type="int", default=2048,
139
                      help="Set the FFT size of the PSD [default=%default]")
140
    parser.add_option("-f", "--center-frequency", type="eng_float", default=0.0,
141
                      help="Set the center frequency of the signal [default=%default]")
142
    parser.add_option("-r", "--sample-rate", type="eng_float", default=1.0,
143
                      help="Set the sample rate of the signal [default=%default]")
144
    parser.add_option("-a", "--average", type="float", default=1.0,
145
                      help="Set amount of averaging (smaller=more averaging) [default=%default]")
146
    parser.add_option("-s", "--scale", type="eng_float", default=2**(32-1)-1,
147
                      help="Set a scaling factor for the int->float conversion [default=%default]")
148
    (options, args) = parser.parse_args()
149
150
    if(len(args) < 1):
151
        parser.print_help()
152
        sys.exit(0)
153
154
    filelist = list(args)
155
156
    nsamples = options.nsamples
157
158
    # Find the smallest number of samples in all files and use that as
159
    # a maximum value possible.
160
    filesizes = []
161
    for f in filelist:
162
        if(os.path.exists(f)):
163
            filesizes.append(os.path.getsize(f) / gr.sizeof_int)
164
    max_nsamples = min(filesizes)
165
166
    tb = my_top_block(filelist,
167
                      options.center_frequency, options.sample_rate,
168
                      options.psd_size,
169
                      options.start, nsamples, max_nsamples,
170
                      options.scale, options.average)
171
172
    main_box = dialog_box(tb, 'GNU Radio PSD Plot')
173
    main_box.show()
174
175
    tb.run()
176
    tb.qapp.exec_()
177
178
if __name__ == "__main__":
179
    try:
180
        main()
181
    except KeyboardInterrupt:
182
        pass
183