Statistics
| Branch: | Tag: | Revision:

root / gr-qtgui / apps / gr_psd_plot @ 4af111d3

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