File: gr_spectrogram_plot

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 (206 lines) | stat: -rwxr-xr-x 8,197 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
#
# Copyright 2012,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
from gnuradio import blocks
from gnuradio import filter
import pmt
from gnuradio.blocks import parse_file_metadata
import scipy
import sys
import os

try:
    import gnuradio.qtgui.plot_spectrogram_base as plot_base
except ImportError:
    import plot_spectrogram_base as plot_base

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)

class spectrogram_plot_c(plot_base.plot_base):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        plot_base.plot_base.__init__(self, filelist, fc, samp_rate,
                                     psdsize, start, nsamples,
                                     max_nsamples, avg)
        self.read_samples = plot_base.read_samples_c
        self.dsize = gr.sizeof_gr_complex
        self.src_type = blocks.vector_source_c
        self.gui_snk = qtgui.waterfall_sink_c(self._psd_size,
                       filter.firdes.WIN_BLACKMAN_hARRIS,self._center_freq,
                       self._samp_rate,"Complex Float",
                       self._nsigs)
        self.setup()

class spectrogram_plot_f(plot_base.plot_base):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        plot_base.plot_base.__init__(self, filelist, fc, samp_rate,
                                     psdsize, start, nsamples,
                                     max_nsamples, avg)
        self.read_samples = plot_base.read_samples_f
        self.dsize = gr.sizeof_float
        self.src_type = blocks.vector_source_f
        self.gui_snk = qtgui.waterfall_sink_f(self._psd_size,
                       filter.firdes.WIN_BLACKMAN_hARRIS,self._center_freq,
                       self._samp_rate,
                       "Float",
                       self._nsigs)
        self.setup()

class spectrogram_plot_b(plot_base.plot_base):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        plot_base.plot_base.__init__(self, filelist, fc, samp_rate,
                                     psdsize, start, nsamples,
                                     max_nsamples, avg)
        self.read_samples = plot_base.read_samples_b
        self.dsize = gr.sizeof_float
        self.src_type = plot_base.source_chars_to_float
        self.gui_snk = qtgui.waterfall_sink_f(self._psd_size,
                       filter.firdes.WIN_BLACKMAN_hARRIS,self._center_freq,
                       self._samp_rate, "Bytes",
                       self._nsigs)
        self.setup()

class spectrogram_plot_i(plot_base.plot_base):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        plot_base.plot_base.__init__(self, filelist, fc, samp_rate,
                                     psdsize, start, nsamples,
                                     max_nsamples, avg)
        self.read_samples = plot_base.read_samples_i
        self.dsize = gr.sizeof_float
        self.src_type = plot_base.source_ints_to_float
        self.gui_snk = qtgui.waterfall_sink_f(self._psd_size,
                       filter.firdes.WIN_BLACKMAN_hARRIS,
                       self._center_freq, self._samp_rate,
                       "Integers",
                       self._nsigs)
        self.setup()

class spectrogram_plot_s(plot_base.plot_base):
    def __init__(self, filelist, fc, samp_rate, psdsize, start,
                 nsamples, max_nsamples, avg=1.0):
        plot_base.plot_base.__init__(self, filelist, fc, samp_rate,
                                     psdsize, start, nsamples,
                                     max_nsamples, avg)
        self.read_samples = plot_base.read_samples_s
        self.dsize = gr.sizeof_float
        self.src_type = plot_base.source_shorts_to_float
        self.gui_snk = qtgui.waterfall_sink_f(self._psd_size,
                       filter.firdes.WIN_BLACKMAN_hARRIS,
                       self._center_freq, self._samp_rate,
                       "Shorts",
                       self._nsigs)
        self.setup()

def read_header(filelist):
   filename = filelist[0] + '.hdr'
   try:
      handle = open(filename,"rb")
   except IOError:
      return
   hdr_start = handle.tell()
   header_str = handle.read(parse_file_metadata.HEADER_LENGTH)
   if( len(header_str) == 0 ):
      return
   # String to PMT Conversion
   try:
      header = pmt.deserialize_str(header_str)
   except RuntimeError:
      sys.stderr.write("Can't deserialize header: invalid/corrupt data\n")
      sys.exit(1)
   info = parse_file_metadata.parse_header(header,False)
   return info

def main():
   description = 'Plots the spectrogram (waterfall) of a file with detached header.'
   description += ' Assumes header is <input_filename>.hdr'
   (options, args) = plot_base.setup_options(description)
   filelist = list(args)
   # Attempt to read the header information
   info = read_header(filelist)
   # If no header, quit
   if not info:
      sys.stderr.write('Header not found\n')
      sys.exit(1)

   max_nsamples = plot_base.find_max_nsamples(filelist)
   srate = info["rx_rate"]

   # Dispatch the proper function
   # Complex Types
   if(info["cplx"] == True):
      if( info["type"] == "float" ):
         tb = spectrogram_plot_c(filelist,
                            options.center_frequency,srate,
                            options.psd_size,
                            options.start, options.nsamples, max_nsamples,
                            options.average);
      else:
         sys.stderr.write("Complex File Type " + info["type"]+ " not supported.\n")
   # Real Types
   else:
      if( info["type"] == "bytes" ):
         tb = spectrogram_plot_b(filelist,
                            options.center_frequency,srate,
                            options.psd_size,
                            options.start, options.nsamples, max_nsamples,
                            options.average);
      elif( info["type"] == "int" ):
         tb = spectrogram_plot_i(filelist,
                            options.center_frequency,srate,
                            options.psd_size,
                            options.start, options.nsamples, max_nsamples,
                            options.average);
      elif( info["type"] == "float" ):
         tb = spectrogram_plot_f(filelist,
                            options.center_frequency,srate,
                            options.psd_size,
                            options.start, options.nsamples, max_nsamples,
                            options.average);
      elif( info["type"] == "short" ):
         tb = spectrogram_plot_s(filelist,
                            options.center_frequency,srate,
                            options.psd_size,
                            options.start, options.nsamples, max_nsamples,
                            options.average);
      else:
        sys.stderr.write("Real File Type " + info["type"] + " not supported\n")
   main_box = plot_base.plot_spectrogram_form(tb, 'GNU Radio Spectrogram Plot')
   main_box.show()

   tb.run()
   tb.qapp.exec_()

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass