Statistics
| Branch: | Tag: | Revision:

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

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