2 # Copyright 2008,2009,2010 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 from __future__ import division
24 ##################################################
26 ##################################################
29 from gnuradio import gr, blks2
30 from pubsub import pubsub
31 from constants import *
34 ##################################################
35 # FFT sink block (wrapper for old wxgui)
36 ##################################################
37 class _fft_sink_base(gr.hier_block2, common.wxgui_hb):
39 An fft block with real/complex inputs and a gui window.
52 fft_rate=fft_window.DEFAULT_FRAME_RATE,
56 size=fft_window.DEFAULT_WIN_SIZE,
59 use_persistence=False,
61 **kwargs #do not end with a comma
64 if avg_alpha is None: avg_alpha = 2.0/fft_rate
66 if persist_alpha is None:
67 actual_fft_rate=float(sample_rate/fft_size)/float(max(1,int(float((sample_rate/fft_size)/fft_rate))))
68 #print "requested_fft_rate ",fft_rate
69 #print "actual_fft_rate ",actual_fft_rate
70 analog_cutoff_freq=0.5 # Hertz
71 #calculate alpha from wanted cutoff freq
72 persist_alpha = 1.0 - math.exp(-2.0*math.pi*analog_cutoff_freq/actual_fft_rate)
75 gr.hier_block2.__init__(
78 gr.io_signature(1, 1, self._item_size),
79 gr.io_signature(0, 0, 0),
82 fft = self._fft_chain(
83 sample_rate=sample_rate,
91 msgq = gr.msg_queue(2)
92 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
96 self.controller = pubsub()
97 self.controller.subscribe(AVERAGE_KEY, fft.set_average)
98 self.controller.publish(AVERAGE_KEY, fft.average)
99 self.controller.subscribe(AVG_ALPHA_KEY, fft.set_avg_alpha)
100 self.controller.publish(AVG_ALPHA_KEY, fft.avg_alpha)
101 self.controller.subscribe(SAMPLE_RATE_KEY, fft.set_sample_rate)
102 self.controller.publish(SAMPLE_RATE_KEY, fft.sample_rate)
104 common.input_watcher(msgq, self.controller, MSG_KEY)
106 self.win = fft_window.fft_window(
108 controller=self.controller,
113 baseband_freq=baseband_freq,
114 sample_rate_key=SAMPLE_RATE_KEY,
118 average_key=AVERAGE_KEY,
119 avg_alpha_key=AVG_ALPHA_KEY,
122 use_persistence=use_persistence,
123 persist_alpha=persist_alpha,
125 common.register_access_methods(self, self.win)
126 setattr(self.win, 'set_baseband_freq', getattr(self, 'set_baseband_freq')) #BACKWARDS
127 setattr(self.win, 'set_peak_hold', getattr(self, 'set_peak_hold')) #BACKWARDS
129 self.wxgui_connect(self, fft, sink)
131 class fft_sink_f(_fft_sink_base):
132 _fft_chain = blks2.logpwrfft_f
133 _item_size = gr.sizeof_float
136 class fft_sink_c(_fft_sink_base):
137 _fft_chain = blks2.logpwrfft_c
138 _item_size = gr.sizeof_gr_complex
141 # ----------------------------------------------------------------
142 # Standalone test app
143 # ----------------------------------------------------------------
146 from gnuradio.wxgui import stdgui2
148 class test_app_block (stdgui2.std_top_block):
149 def __init__(self, frame, panel, vbox, argv):
150 stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
154 # build our flow graph
155 input_rate = 2048.0e3
158 noise =gr.noise_source_c(gr.GR_UNIFORM, 1.0/10)
160 # Generate a complex sinusoid
161 #src1 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
162 src1 = gr.sig_source_c (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
164 # We add these throttle blocks so that this demo doesn't
165 # suck down all the CPU available. Normally you wouldn't use these.
166 thr1 = gr.throttle(gr.sizeof_gr_complex, input_rate)
168 sink1 = fft_sink_c (panel, title="Complex Data", fft_size=fft_size,
169 sample_rate=input_rate, baseband_freq=100e3,
170 ref_level=0, y_per_div=20, y_divs=10)
171 vbox.Add (sink1.win, 1, wx.EXPAND)
174 self.connect(src1, (combine1,0))
175 self.connect(noise,(combine1,1))
176 self.connect(combine1,thr1, sink1)
178 #src2 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
179 src2 = gr.sig_source_f (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
180 thr2 = gr.throttle(gr.sizeof_float, input_rate)
181 sink2 = fft_sink_f (panel, title="Real Data", fft_size=fft_size*2,
182 sample_rate=input_rate, baseband_freq=100e3,
183 ref_level=0, y_per_div=20, y_divs=10)
184 vbox.Add (sink2.win, 1, wx.EXPAND)
187 c2f2=gr.complex_to_float()
189 self.connect(src2, (combine2,0))
190 self.connect(noise,c2f2,(combine2,1))
191 self.connect(combine2, thr2,sink2)
194 app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
197 if __name__ == '__main__':