2 # Copyright 2008 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 ##################################################
24 ##################################################
27 from gnuradio import gr, blks2
28 from pubsub import pubsub
29 from constants import *
31 ##################################################
32 # FFT sink block (wrapper for old wxgui)
33 ##################################################
34 class _fft_sink_base(gr.hier_block2, common.prop_setter):
36 An fft block with real/complex inputs and a gui window.
49 fft_rate=gr.prefs().get_long('wxgui', 'fft_rate', fft_window.DEFAULT_FRAME_RATE),
53 size=fft_window.DEFAULT_WIN_SIZE,
57 if avg_alpha is None: avg_alpha = 2.0/fft_rate
59 gr.hier_block2.__init__(
62 gr.io_signature(1, 1, self._item_size),
63 gr.io_signature(0, 0, 0),
66 copy = gr.kludge_copy(self._item_size)
67 fft = self._fft_chain(
68 sample_rate=sample_rate,
75 msgq = gr.msg_queue(2)
76 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
78 self.connect(self, copy, fft, sink)
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)
88 def setter(p, k, x): # lambdas can't have assignments :(
90 common.input_watcher(msgq, lambda x: setter(self.controller, MSG_KEY, x))
92 self.win = fft_window.fft_window(
94 controller=self.controller,
99 baseband_freq=baseband_freq,
100 sample_rate_key=SAMPLE_RATE_KEY,
104 average_key=AVERAGE_KEY,
105 avg_alpha_key=AVG_ALPHA_KEY,
109 #register callbacks from window for external use
110 for attr in filter(lambda a: a.startswith('set_'), dir(self.win)):
111 setattr(self, attr, getattr(self.win, attr))
112 self._register_set_prop(self.controller, SAMPLE_RATE_KEY)
113 self._register_set_prop(self.controller, AVERAGE_KEY)
114 self._register_set_prop(self.controller, AVG_ALPHA_KEY)
116 class fft_sink_f(_fft_sink_base):
117 _fft_chain = blks2.logpwrfft_f
118 _item_size = gr.sizeof_float
121 class fft_sink_c(_fft_sink_base):
122 _fft_chain = blks2.logpwrfft_c
123 _item_size = gr.sizeof_gr_complex
126 # ----------------------------------------------------------------
127 # Standalone test app
128 # ----------------------------------------------------------------
131 from gnuradio.wxgui import stdgui2
133 class test_app_block (stdgui2.std_top_block):
134 def __init__(self, frame, panel, vbox, argv):
135 stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
139 # build our flow graph
142 # Generate a complex sinusoid
143 #src1 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
144 src1 = gr.sig_source_c (input_rate, gr.GR_CONST_WAVE, 5.75e3, 1)
146 # We add these throttle blocks so that this demo doesn't
147 # suck down all the CPU available. Normally you wouldn't use these.
148 thr1 = gr.throttle(gr.sizeof_gr_complex, input_rate)
150 sink1 = fft_sink_c (panel, title="Complex Data", fft_size=fft_size,
151 sample_rate=input_rate, baseband_freq=100e3,
152 ref_level=0, y_per_div=20, y_divs=10)
153 vbox.Add (sink1.win, 1, wx.EXPAND)
155 self.connect(src1, thr1, sink1)
157 #src2 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
158 src2 = gr.sig_source_f (input_rate, gr.GR_CONST_WAVE, 5.75e3, 1)
159 thr2 = gr.throttle(gr.sizeof_float, input_rate)
160 sink2 = fft_sink_f (panel, title="Real Data", fft_size=fft_size*2,
161 sample_rate=input_rate, baseband_freq=100e3,
162 ref_level=0, y_per_div=20, y_divs=10)
163 vbox.Add (sink2.win, 1, wx.EXPAND)
165 self.connect(src2, thr2, sink2)
168 app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
171 if __name__ == '__main__':