Statistics
| Branch: | Tag: | Revision:

root / gnuradio-core / src / utils / gr_plot_iq.py @ f52a5932

History | View | Annotate | Download (6.8 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2007 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
import scipy
24
from pylab import *
25
from optparse import OptionParser
26
27
matplotlib.interactive(True)
28
matplotlib.use('TkAgg')
29
30
class draw_fft:
31
    def __init__(self, filename, options):
32
        self.hfile = open(filename, "r")
33
        self.block_length = options.block
34
        self.start = options.start
35
        self.sample_rate = options.sample_rate
36
37
        self.datatype = scipy.complex64
38
        self.sizeof_data = self.datatype().nbytes    # number of bytes per sample in file
39
40
        self.axis_font_size = 16
41
        self.label_font_size = 18
42
        self.title_font_size = 20
43
        self.text_size = 22
44
45
        # Setup PLOT
46
        self.fig = figure(1, figsize=(16, 9), facecolor='w')
47
        rcParams['xtick.labelsize'] = self.axis_font_size
48
        rcParams['ytick.labelsize'] = self.axis_font_size
49
        
50
        self.text_file     = figtext(0.10, 0.94, ("File: %s" % filename), weight="heavy", size=self.text_size)
51
        self.text_file_pos = figtext(0.10, 0.88, "File Position: ", weight="heavy", size=self.text_size)
52
        self.text_block    = figtext(0.40, 0.88, ("Block Size: %d" % self.block_length),
53
                                     weight="heavy", size=self.text_size)
54
        self.text_sr       = figtext(0.60, 0.88, ("Sample Rate: %.2f" % self.sample_rate),
55
                                     weight="heavy", size=self.text_size)
56
        self.make_plots()
57
58
        self.button_left_axes = self.fig.add_axes([0.45, 0.01, 0.05, 0.05], frameon=True)
59
        self.button_left = Button(self.button_left_axes, "<")
60
        self.button_left_callback = self.button_left.on_clicked(self.button_left_click)
61
62
        self.button_right_axes = self.fig.add_axes([0.50, 0.01, 0.05, 0.05], frameon=True)
63
        self.button_right = Button(self.button_right_axes, ">")
64
        self.button_right_callback = self.button_right.on_clicked(self.button_right_click)
65
66
        self.xlim = self.sp_iq.get_xlim()
67
68
        self.manager = get_current_fig_manager()
69
        connect('key_press_event', self.click)
70
        show()
71
72
    def get_data(self):
73
        self.text_file_pos.set_text("File Position: %d" % (self.hfile.tell()//self.sizeof_data))
74
        self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
75
        #print "Read in %d items" % len(self.iq)
76
        if(len(self.iq) == 0):
77
            print "End of File"
78
        else:
79
            self.reals = [r.real for r in self.iq]
80
            self.imags = [i.imag for i in self.iq]
81
            self.time = [i*(1/self.sample_rate) for i in range(len(self.reals))]
82
            
83
    def make_plots(self):
84
        # if specified on the command-line, set file pointer
85
        self.hfile.seek(self.sizeof_data*self.start, 1)
86
87
        self.get_data()
88
        
89
        # Subplot for real and imaginary parts of signal
90
        self.sp_iq = self.fig.add_subplot(2,1,1, position=[0.075, 0.14, 0.85, 0.67])
91
        self.sp_iq.set_title(("I&Q"), fontsize=self.title_font_size, fontweight="bold")
92
        self.sp_iq.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold")
93
        self.sp_iq.set_ylabel("Amplitude (V)", fontsize=self.label_font_size, fontweight="bold")
94
        self.plot_iq = plot(self.time, self.reals, 'bo-', self.time, self.imags, 'ro-')
95
        self.sp_iq.set_ylim([1.5*min([min(self.reals), min(self.imags)]),
96
                             1.5*max([max(self.reals), max(self.imags)])])
97
        
98
        draw()
99
100
    def update_plots(self):
101
        self.plot_iq[0].set_data([self.time, self.reals])
102
        self.plot_iq[1].set_data([self.time, self.imags])
103
        self.sp_iq.set_ylim([1.5*min([min(self.reals), min(self.imags)]),
104
                             1.5*max([max(self.reals), max(self.imags)])])
105
        draw()
106
        
107
    def click(self, event):
108
        forward_valid_keys = [" ", "down", "right"]
109
        backward_valid_keys = ["up", "left"]
110
111
        if(find(event.key, forward_valid_keys)):
112
            self.step_forward()
113
            
114
        elif(find(event.key, backward_valid_keys)):
115
            self.step_backward()
116
117
    def button_left_click(self, event):
118
        self.step_backward()
119
120
    def button_right_click(self, event):
121
        self.step_forward()
122
123
    def step_forward(self):
124
        self.get_data()
125
        self.update_plots()
126
127
    def step_backward(self):
128
        # Step back in file position
129
        if(self.hfile.tell() >= 2*self.sizeof_data*self.block_length ):
130
            self.hfile.seek(-2*self.sizeof_data*self.block_length, 1)
131
        else:
132
            self.hfile.seek(-self.hfile.tell(),1)
133
        self.get_data()
134
        self.update_plots()
135
        
136
            
137
138
#FIXME: there must be a way to do this with a Python builtin
139
def find(item_in, list_search):
140
    for l in list_search:
141
        if item_in == l:
142
            return True
143
    return False
144
145
def main():
146
    usage="%prog: [options] input_filename"
147
    description = "Takes a GNU Radio complex binary file and displays the I&Q data versus time. You can set the block size to specify how many points to read in at a time and the start position in 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 axis, set the sample rate (-R or --sample-rate) to the sample rate used when capturing the samples."
148
149
    parser = OptionParser(conflict_handler="resolve", usage=usage, description=description)
150
    parser.add_option("-B", "--block", type="int", default=1000,
151
                      help="Specify the block size [default=%default]")
152
    parser.add_option("-s", "--start", type="int", default=0,
153
                      help="Specify where to start in the file [default=%default]")
154
    parser.add_option("-R", "--sample-rate", type="float", default=1.0,
155
                      help="Set the sampler rate of the data [default=%default]")
156
    
157
    (options, args) = parser.parse_args ()
158
    if len(args) != 1:
159
        parser.print_help()
160
        raise SystemExit, 1
161
    filename = args[0]
162
163
    dc = draw_fft(filename, options)
164
165
if __name__ == "__main__":
166
    try:
167
        main()
168
    except KeyboardInterrupt:
169
        pass
170
    
171
172