switch source package format to 3.0 quilt
[debian/gnuradio] / gr-wxgui / src / python / histo_window.py
1 #
2 # Copyright 2009 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20 #
21
22 ##################################################
23 # Imports
24 ##################################################
25 import plotter
26 import common
27 import wx
28 import numpy
29 import math
30 import pubsub
31 from constants import *
32 from gnuradio import gr #for gr.prefs
33 import forms
34
35 ##################################################
36 # Constants
37 ##################################################
38 DEFAULT_WIN_SIZE = (600, 300)
39
40 ##################################################
41 # histo window control panel
42 ##################################################
43 class control_panel(wx.Panel):
44         """
45         A control panel with wx widgits to control the plotter and histo sink.
46         """
47
48         def __init__(self, parent):
49                 """
50                 Create a new control panel.
51                 @param parent the wx parent window
52                 """
53                 self.parent = parent
54                 wx.Panel.__init__(self, parent, style=wx.SUNKEN_BORDER)
55                 parent[SHOW_CONTROL_PANEL_KEY] = True
56                 parent.subscribe(SHOW_CONTROL_PANEL_KEY, self.Show)
57                 control_box = wx.BoxSizer(wx.VERTICAL)
58                 SIZE = (100, -1)
59                 control_box = forms.static_box_sizer(
60                         parent=self, label='Options',
61                         bold=True, orient=wx.VERTICAL,
62                 )
63                 #num bins
64                 control_box.AddStretchSpacer()
65                 forms.text_box(
66                         sizer=control_box, parent=self, label='Num Bins',
67                         converter=forms.int_converter(),
68                         ps=parent, key=NUM_BINS_KEY,
69                 )
70                 #frame size
71                 control_box.AddStretchSpacer()
72                 forms.text_box(
73                         sizer=control_box, parent=self, label='Frame Size',
74                         converter=forms.int_converter(),
75                         ps=parent, key=FRAME_SIZE_KEY,
76                 )
77                 #run/stop
78                 control_box.AddStretchSpacer()
79                 forms.toggle_button(
80                         sizer=control_box, parent=self,
81                         true_label='Stop', false_label='Run',
82                         ps=parent, key=RUNNING_KEY,
83                 )
84                 #set sizer
85                 self.SetSizerAndFit(control_box)
86
87 ##################################################
88 # histo window with plotter and control panel
89 ##################################################
90 class histo_window(wx.Panel, pubsub.pubsub):
91         def __init__(
92                 self,
93                 parent,
94                 controller,
95                 size,
96                 title,
97                 maximum_key,
98                 minimum_key,
99                 num_bins_key,
100                 frame_size_key,
101                 msg_key,
102         ):
103                 pubsub.pubsub.__init__(self)
104                 #setup
105                 self.samples = list()
106                 #proxy the keys
107                 self.proxy(MAXIMUM_KEY, controller, maximum_key)
108                 self.proxy(MINIMUM_KEY, controller, minimum_key)
109                 self.proxy(NUM_BINS_KEY, controller, num_bins_key)
110                 self.proxy(FRAME_SIZE_KEY, controller, frame_size_key)
111                 self.proxy(MSG_KEY, controller, msg_key)
112                 #initialize values
113                 self[RUNNING_KEY] = True
114                 self[X_DIVS_KEY] = 8
115                 self[Y_DIVS_KEY] = 4
116                 #init panel and plot
117                 wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER)
118                 self.plotter = plotter.bar_plotter(self)
119                 self.plotter.SetSize(wx.Size(*size))
120                 self.plotter.set_title(title)
121                 self.plotter.enable_point_label(True)
122                 self.plotter.enable_grid_lines(False)
123                 #setup the box with plot and controls
124                 self.control_panel = control_panel(self)
125                 main_box = wx.BoxSizer(wx.HORIZONTAL)
126                 main_box.Add(self.plotter, 1, wx.EXPAND)
127                 main_box.Add(self.control_panel, 0, wx.EXPAND)
128                 self.SetSizerAndFit(main_box)
129                 #register events
130                 self.subscribe(MSG_KEY, self.handle_msg)
131                 self.subscribe(X_DIVS_KEY, self.update_grid)
132                 self.subscribe(Y_DIVS_KEY, self.update_grid)
133
134         def handle_msg(self, msg):
135                 """
136                 Handle the message from the fft sink message queue.
137                 @param msg the frame as a character array
138                 """
139                 if not self[RUNNING_KEY]: return
140                 #convert to floating point numbers
141                 self.samples = 100*numpy.fromstring(msg, numpy.float32)[:self[NUM_BINS_KEY]] #only take first frame
142                 self.plotter.set_bars(
143                         bars=self.samples,
144                         bar_width=0.6,
145                         color_spec=(0, 0, 1),
146                 )
147                 self.update_grid()
148
149         def update_grid(self):
150                 if not len(self.samples): return
151                 #calculate the maximum y value
152                 y_off = math.ceil(numpy.max(self.samples))
153                 y_off = min(max(y_off, 1.0), 100.0) #between 1% and 100%
154                 #update the x grid
155                 self.plotter.set_x_grid(
156                         self[MINIMUM_KEY], self[MAXIMUM_KEY],
157                         common.get_clean_num((self[MAXIMUM_KEY] - self[MINIMUM_KEY])/self[X_DIVS_KEY]),
158                 )
159                 self.plotter.set_x_label('Counts')
160                 #update the y grid
161                 self.plotter.set_y_grid(0, y_off, y_off/self[Y_DIVS_KEY])
162                 self.plotter.set_y_label('Frequency', '%')
163                 self.plotter.update()