Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / utils / gr_plot_const.py
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 pylab, math
24 from pylab import *
25 import struct, sys
26 from optparse import OptionParser
27
28 import gr_read_binary
29
30 class zoom:
31     def __init__(self, xdata, reals, imags, sp_iq, sp_const, plot_const, manager):
32         self.sp_iq = sp_iq
33         self.sp_const = sp_const
34         self.xaxis = xdata
35         self.reals = reals
36         self.imags = imags
37         self.plot_const = plot_const
38         self.manager = manager
39
40         self.xlim = self.sp_iq.get_xlim()
41            
42     def __call__(self, event):
43         newxlim = self.sp_iq.get_xlim()
44
45         if(newxlim != self.xlim):
46             self.xlim = newxlim
47             r = self.reals[int(self.xlim[0]) : int(self.xlim[1])]
48             i = self.imags[int(self.xlim[0]) : int(self.xlim[1])]
49
50             self.plot_const[0].set_data(r, i)
51             self.sp_const.axis([-2, 2, -2, 2])
52             self.manager.canvas.draw()
53             
54 def main():
55     usage="%prog: [options] output_filename"
56     parser = OptionParser(conflict_handler="resolve", usage=usage)
57     parser.add_option("-s", "--size", type="int", default=None,
58                       help="Specify the number of points to plot [default=%default]")
59     parser.add_option("", "--skip", type="int", default=None,
60                       help="Specify the number of points to skip [default=%default]")
61
62     (options, args) = parser.parse_args ()
63     if len(args) != 1:
64         parser.print_help()
65         raise SystemExit, 1
66     filename = args[0]
67
68     iq = gr_read_binary.read_complex_binary(filename)
69
70     if(options.skip is None):
71         options.skip = 0
72     
73     if((options.size is None) or ((options.skip+options.size) > len(iq[0]))):
74         options.size = len(iq[0]) - options.skip
75
76     reals = iq[0][options.skip : options.skip + options.size]
77     imags = iq[1][options.skip : options.skip + options.size]
78     x = range(options.skip, options.skip + options.size)
79     
80     # PLOT
81     f = figure(1, figsize=(16, 12), facecolor='w')
82     rcParams['xtick.labelsize'] = 16
83     rcParams['ytick.labelsize'] = 16
84
85     # Subplot for real and imaginary parts of signal
86     sp1 = f.add_subplot(2,1,1)
87     sp1.set_title(("I&Q"), fontsize=26, fontweight="bold")
88     sp1.set_xlabel("Time (s)", fontsize=20, fontweight="bold")
89     sp1.set_ylabel("Amplitude (V)", fontsize=20, fontweight="bold")
90     plot(x, reals, 'bo-', x, imags, 'ro-')
91
92     # Subplot for constellation plot
93     sp2 = f.add_subplot(2,1,2)
94     sp2.set_title(("Constellation"), fontsize=26, fontweight="bold")
95     sp2.set_xlabel("Inphase", fontsize=20, fontweight="bold")
96     sp2.set_ylabel("Qaudrature", fontsize=20, fontweight="bold")
97     p2 = plot(reals, imags, 'bo')
98     sp2.axis([-2, 2, -2, 2])
99     
100     manager = get_current_fig_manager()
101     zm = zoom(x, reals, imags, sp1, sp2, p2, manager)
102     connect('draw_event', zm)
103     
104     show()
105
106 if __name__ == "__main__":
107     try:
108         main()
109     except KeyboardInterrupt:
110         pass
111     
112
113