Update revision to 3.3.0-rc0
[debian/gnuradio] / gr-wxgui / src / python / fftsink_gl.py
1 #
2 # Copyright 2008,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 fft_window
26 import common
27 from gnuradio import gr, blks2
28 from pubsub import pubsub
29 from constants import *
30
31 ##################################################
32 # FFT sink block (wrapper for old wxgui)
33 ##################################################
34 class _fft_sink_base(gr.hier_block2, common.wxgui_hb):
35         """
36         An fft block with real/complex inputs and a gui window.
37         """
38
39         def __init__(
40                 self,
41                 parent,
42                 baseband_freq=0,
43                 ref_scale=2.0,
44                 y_per_div=10,
45                 y_divs=8,
46                 ref_level=50,
47                 sample_rate=1,
48                 fft_size=512,
49                 fft_rate=fft_window.DEFAULT_FRAME_RATE,
50                 average=False,
51                 avg_alpha=None,
52                 title='',
53                 size=fft_window.DEFAULT_WIN_SIZE,
54                 peak_hold=False,
55                 win=None,
56                 **kwargs #do not end with a comma
57         ):
58                 #ensure avg alpha
59                 if avg_alpha is None: avg_alpha = 2.0/fft_rate
60                 #init
61                 gr.hier_block2.__init__(
62                         self,
63                         "fft_sink",
64                         gr.io_signature(1, 1, self._item_size),
65                         gr.io_signature(0, 0, 0),
66                 )
67                 #blocks
68                 fft = self._fft_chain(
69                         sample_rate=sample_rate,
70                         fft_size=fft_size,
71                         frame_rate=fft_rate,
72                         ref_scale=ref_scale,
73                         avg_alpha=avg_alpha,
74                         average=average,
75                         win=win,
76                 )
77                 msgq = gr.msg_queue(2)
78                 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
79                 #controller
80                 self.controller = pubsub()
81                 self.controller.subscribe(AVERAGE_KEY, fft.set_average)
82                 self.controller.publish(AVERAGE_KEY, fft.average)
83                 self.controller.subscribe(AVG_ALPHA_KEY, fft.set_avg_alpha)
84                 self.controller.publish(AVG_ALPHA_KEY, fft.avg_alpha)
85                 self.controller.subscribe(SAMPLE_RATE_KEY, fft.set_sample_rate)
86                 self.controller.publish(SAMPLE_RATE_KEY, fft.sample_rate)
87                 #start input watcher
88                 common.input_watcher(msgq, self.controller, MSG_KEY)
89                 #create window
90                 self.win = fft_window.fft_window(
91                         parent=parent,
92                         controller=self.controller,
93                         size=size,
94                         title=title,
95                         real=self._real,
96                         fft_size=fft_size,
97                         baseband_freq=baseband_freq,
98                         sample_rate_key=SAMPLE_RATE_KEY,
99                         y_per_div=y_per_div,
100                         y_divs=y_divs,
101                         ref_level=ref_level,
102                         average_key=AVERAGE_KEY,
103                         avg_alpha_key=AVG_ALPHA_KEY,
104                         peak_hold=peak_hold,
105                         msg_key=MSG_KEY,
106                 )
107                 common.register_access_methods(self, self.win)
108                 setattr(self.win, 'set_baseband_freq', getattr(self, 'set_baseband_freq')) #BACKWARDS
109                 setattr(self.win, 'set_peak_hold', getattr(self, 'set_peak_hold')) #BACKWARDS
110                 #connect
111                 self.wxgui_connect(self, fft, sink)
112
113 class fft_sink_f(_fft_sink_base):
114         _fft_chain = blks2.logpwrfft_f
115         _item_size = gr.sizeof_float
116         _real = True
117
118 class fft_sink_c(_fft_sink_base):
119         _fft_chain = blks2.logpwrfft_c
120         _item_size = gr.sizeof_gr_complex
121         _real = False
122
123 # ----------------------------------------------------------------
124 # Standalone test app
125 # ----------------------------------------------------------------
126
127 import wx
128 from gnuradio.wxgui import stdgui2
129
130 class test_app_block (stdgui2.std_top_block):
131     def __init__(self, frame, panel, vbox, argv):
132         stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
133
134         fft_size = 256
135
136         # build our flow graph
137         input_rate = 20.48e3
138
139         # Generate a complex sinusoid
140         #src1 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
141         src1 = gr.sig_source_c (input_rate, gr.GR_CONST_WAVE, 5.75e3, 1)
142
143         # We add these throttle blocks so that this demo doesn't
144         # suck down all the CPU available.  Normally you wouldn't use these.
145         thr1 = gr.throttle(gr.sizeof_gr_complex, input_rate)
146
147         sink1 = fft_sink_c (panel, title="Complex Data", fft_size=fft_size,
148                             sample_rate=input_rate, baseband_freq=100e3,
149                             ref_level=0, y_per_div=20, y_divs=10)
150         vbox.Add (sink1.win, 1, wx.EXPAND)
151
152         self.connect(src1, thr1, sink1)
153
154         #src2 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
155         src2 = gr.sig_source_f (input_rate, gr.GR_CONST_WAVE, 5.75e3, 1)
156         thr2 = gr.throttle(gr.sizeof_float, input_rate)
157         sink2 = fft_sink_f (panel, title="Real Data", fft_size=fft_size*2,
158                             sample_rate=input_rate, baseband_freq=100e3,
159                             ref_level=0, y_per_div=20, y_divs=10)
160         vbox.Add (sink2.win, 1, wx.EXPAND)
161
162         self.connect(src2, thr2, sink2)
163
164 def main ():
165     app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
166     app.MainLoop ()
167
168 if __name__ == '__main__':
169     main ()