3 # Copyright 2007 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
25 from optparse import OptionParser
26 from scipy import fftpack
27 from math import log10
29 matplotlib.interactive(True)
30 matplotlib.use('TkAgg')
33 def __init__(self, filename, options):
34 self.hfile = open(filename, "r")
35 self.block_length = options.block
36 self.start = options.start
37 self.sample_rate = options.sample_rate
39 self.axis_font_size = 16
40 self.label_font_size = 18
41 self.title_font_size = 20
45 self.fig = figure(1, figsize=(16, 9), facecolor='w')
46 rcParams['xtick.labelsize'] = self.axis_font_size
47 rcParams['ytick.labelsize'] = self.axis_font_size
49 self.text_file = figtext(0.10, 0.94, ("File: %s" % filename), weight="heavy", size=self.text_size)
50 self.text_file_pos = figtext(0.10, 0.88, "File Position: ", weight="heavy", size=self.text_size)
51 self.text_block = figtext(0.40, 0.88, ("Block Size: %d" % self.block_length),
52 weight="heavy", size=self.text_size)
53 self.text_sr = figtext(0.60, 0.88, ("Sample Rate: %.2f" % self.sample_rate),
54 weight="heavy", size=self.text_size)
57 self.button_left_axes = self.fig.add_axes([0.45, 0.01, 0.05, 0.05], frameon=True)
58 self.button_left = Button(self.button_left_axes, "<")
59 self.button_left_callback = self.button_left.on_clicked(self.button_left_click)
61 self.button_right_axes = self.fig.add_axes([0.50, 0.01, 0.05, 0.05], frameon=True)
62 self.button_right = Button(self.button_right_axes, ">")
63 self.button_right_callback = self.button_right.on_clicked(self.button_right_click)
65 self.xlim = self.sp_f.get_xlim()
67 self.manager = get_current_fig_manager()
68 connect('draw_event', self.zoom)
69 connect('key_press_event', self.click)
73 self.text_file_pos.set_text("File Position: %d" % (self.hfile.tell()//4))
74 self.floats = scipy.fromfile(self.hfile, dtype=scipy.float32, count=self.block_length)
75 #print "Read in %d items" % len(self.floats)
76 if(len(self.floats) == 0):
79 self.f_fft = self.dofft(self.floats)
81 self.time = [i*(1/self.sample_rate) for i in range(len(self.floats))]
82 self.freq = self.calc_freq(self.time, self.sample_rate)
86 f_fft = fftpack.fftshift(scipy.fft(f)) # fft and shift axis
90 f_dB.append(20*log10(abs(f/N))) # convert to decibels, adjust power
91 except OverflowError: # protect against taking log(0)
92 f = 1e-14 # not sure if this is the best way to do this
93 f_dB.append(20*log10(abs(f/N)))
97 def calc_freq(self, time, sample_rate):
99 Fs = 1.0 / (max(time) - min(time))
100 Fn = 0.5 * sample_rate
101 freq = [-Fn + i*Fs for i in range(N)]
104 def make_plots(self):
105 # if specified on the command-line, set file pointer
106 self.hfile.seek(16*self.start, 1)
110 # Subplot for real and imaginary parts of signal
111 self.sp_f = self.fig.add_subplot(2,1,1, position=[0.075, 0.2, 0.4, 0.6])
112 self.sp_f.set_title(("Amplitude"), fontsize=self.title_font_size, fontweight="bold")
113 self.sp_f.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold")
114 self.sp_f.set_ylabel("Amplitude (V)", fontsize=self.label_font_size, fontweight="bold")
115 self.plot_f = plot(self.time, self.floats, 'bo-')
116 self.sp_f.set_ylim([1.5*min(self.floats),
117 1.5*max(self.floats)])
119 # Subplot for constellation plot
120 self.sp_fft = self.fig.add_subplot(2,2,1, position=[0.575, 0.2, 0.4, 0.6])
121 self.sp_fft.set_title(("FFT"), fontsize=self.title_font_size, fontweight="bold")
122 self.sp_fft.set_xlabel("Frequency (Hz)", fontsize=self.label_font_size, fontweight="bold")
123 self.sp_fft.set_ylabel("Power (dBm)", fontsize=self.label_font_size, fontweight="bold")
124 self.plot_fft = plot(self.freq, self.f_fft, '-bo')
125 self.sp_fft.set_ylim([min(self.f_fft)-10, max(self.f_fft)+10])
129 def update_plots(self):
130 self.plot_f[0].set_data([self.time, self.floats])
131 self.sp_f.set_ylim([1.5*min(self.floats),
132 1.5*max(self.floats)])
134 self.plot_fft[0].set_data([self.freq, self.f_fft])
135 self.sp_fft.set_ylim([min(self.f_fft)-10, max(self.f_fft)+10])
139 def zoom(self, event):
140 newxlim = self.sp_f.get_xlim()
141 if(newxlim != self.xlim):
143 xmin = max(0, int(ceil(self.sample_rate*self.xlim[0])))
144 xmax = min(int(ceil(self.sample_rate*self.xlim[1])), len(self.floats))
146 f = self.floats[xmin : xmax]
147 time = self.time[xmin : xmax]
149 f_fft = self.dofft(f)
150 freq = self.calc_freq(time, self.sample_rate)
152 self.plot_fft[0].set_data(freq, f_fft)
153 self.sp_fft.axis([min(freq), max(freq),
154 min(f_fft)-10, max(f_fft)+10])
158 def click(self, event):
159 forward_valid_keys = [" ", "down", "right"]
160 backward_valid_keys = ["up", "left"]
162 if(find(event.key, forward_valid_keys)):
165 elif(find(event.key, backward_valid_keys)):
168 def button_left_click(self, event):
171 def button_right_click(self, event):
174 def step_forward(self):
178 def step_backward(self):
179 # Step back in file position
180 if(self.hfile.tell() >= 8*self.block_length ):
181 self.hfile.seek(-8*self.block_length, 1)
183 self.hfile.seek(-self.hfile.tell(),1)
189 #FIXME: there must be a way to do this with a Python builtin
190 def find(item_in, list_search):
191 for l in list_search:
197 usage="%prog: [options] input_filename"
198 description = "Takes a GNU Radio floating point binary file and displays the sample data versus time as well as the frequency domain (FFT) plot. The y-axis values are plotted assuming volts as the amplitude of the I&Q streams and converted into dBm in the frequency domain (the 1/N power adjustment out of the FFT is performed internally). The script plots a certain block of data at a time, specified on the command line as -B or --block. This value defaults to 1000. The start position in the file can be set by specifying -s or --start and defaults to 0 (the start of the file). By default, the system assumes a sample rate of 1, so in time, each sample is plotted versus the sample number. To set a true time and frequency axis, set the sample rate (-R or --sample-rate) to the sample rate used when capturing the samples."
200 parser = OptionParser(conflict_handler="resolve", usage=usage, description=description)
201 parser.add_option("-B", "--block", type="int", default=1000,
202 help="Specify the block size [default=%default]")
203 parser.add_option("-s", "--start", type="int", default=0,
204 help="Specify where to start in the file [default=%default]")
205 parser.add_option("-R", "--sample-rate", type="float", default=1.0,
206 help="Set the sampler rate of the data [default=%default]")
208 (options, args) = parser.parse_args ()
214 dc = draw_fft_f(filename, options)
216 if __name__ == "__main__":
219 except KeyboardInterrupt: