File: plot_spectrogram_base.py

package info (click to toggle)
gnuradio 3.7.13.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 45,992 kB
  • sloc: cpp: 155,723; python: 88,934; xml: 42,165; ansic: 41,036; fortran: 927; asm: 803; sh: 224; lisp: 31; makefile: 17
file content (170 lines) | stat: -rw-r--r-- 5,648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
#
# Copyright 2013,2018 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

from gnuradio import gr, blocks
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import os, sys

try:
    from gnuradio import qtgui
    from PyQt5 import QtWidgets, Qt
    import sip
except ImportError:
    sys.stderr.write("Error: Program requires PyQt5 and gr-qtgui.\n")
    sys.exit(1)

try:
    import scipy
except ImportError:
    sys.stderr.write("Error: Scipy required (www.scipy.org).\n")
    sys.exit(1)

try:
    from gnuradio.qtgui.plot_form import *
    from gnuradio.qtgui.plot_base import *
except ImportError:
    from plot_form import *
    from plot_base import *

class plot_base(gr.top_block):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        gr.top_block.__init__(self)

        self._filelist = filelist
        self._center_freq = fc
        self._samp_rate = samp_rate
        self._psd_size = psdsize
        self._start = start
        self._max_nsamps = max_nsamples
        self._nsigs = len(self._filelist)
        self._avg = avg
        self._nsamps = nsamples
        self._auto_scale = False

        self._y_min = -200
        self._y_max = 400
        self._y_range = 130
        self._y_value = 10

        self._is_setup = False

        self.qapp = QtWidgets.QApplication(sys.argv)

    def setup(self):
        self.skip = blocks.skiphead(self.dsize, self._start)

        n = 0
        self.srcs = list()
        self._data_min = sys.maxint
        self._data_max = -sys.maxint - 1
        for f in self._filelist:
            data,_min,_max = self.read_samples(f, self._start,
                                               self._nsamps, self._psd_size)
            if(_min < self._data_min):
                self._data_min = _min
            if(_max > self._data_max):
                self._data_max = _max

            self.srcs.append(self.src_type(data))

            # Set default labels based on file names
            fname = f.split("/")[-1]
            self.gui_snk.set_line_label(n, "{0}".format(fname))
            n += 1

        self.connect(self.srcs[0], self.skip)
        self.connect(self.skip, (self.gui_snk, 0))

        for i,s in enumerate(self.srcs[1:]):
            self.connect(s, (self.gui_snk, i+1))

        self.gui_snk.set_update_time(0);
        self.gui_snk.set_time_per_fft(self._psd_size/self._samp_rate)
        self.gui_snk.enable_menu(False)
        self.gui_snk.set_fft_average(self._avg)

        # Get Python Qt references
        pyQt = self.gui_snk.pyqwidget()
        self.pyWin = sip.wrapinstance(pyQt, QtWidgets.QWidget)

        self._is_setup = True

    def is_setup(self):
        return self._is_setup

    def set_y_axis(self, y_min, y_max):
        self.gui_snk.set_intensity_range(y_min, y_max)
        return y_min, y_max

    def get_gui(self):
        if(self.is_setup()):
            return self.pyWin
        else:
            return None

    def reset(self, newstart, newnsamps):
        self.stop()
        self.wait()
        self.gui_snk.clear_data()
        self.gui_snk.set_time_per_fft(self._psd_size/self._samp_rate)

        self._start = newstart
        self._nsamps = newnsamps

        self._data_min = sys.maxint
        self._data_max = -sys.maxint - 1
        for s,f in zip(self.srcs, self._filelist):
            data,_min,_max = self.read_samples(f, self._start, newnsamps, self._psd_size)
            if(_min < self._data_min):
                self._data_min = _min
            if(_max > self._data_max):
                self._data_max = _max

            s.set_data(data)

        self.start()

def setup_options(desc):
    parser = OptionParser(option_class=eng_option, description=desc,
                          conflict_handler="resolve")
    parser.add_option("-N", "--nsamples", type="int", default=1000000,
                      help="Set the number of samples to display [default=%default]")
    parser.add_option("-S", "--start", type="int", default=0,
                      help="Starting sample number [default=%default]")
    parser.add_option("-L", "--psd-size", type="int", default=2048,
                      help="Set the FFT size of the PSD [default=%default]")
    parser.add_option("-f", "--center-frequency", type="eng_float", default=0.0,
                      help="Set the center frequency of the signal [default=%default]")
    parser.add_option("-r", "--sample-rate", type="eng_float", default=1.0,
                      help="Set the sample rate of the signal [default=%default]")
    parser.add_option("-a", "--average", type="float", default=1.0,
                      help="Set amount of averaging (smaller=more averaging) [default=%default]")
    (options, args) = parser.parse_args()

    if(len(args) < 1):
        parser.print_help()
        sys.exit(0)

    return (options, args)