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 ##################################################
24 ##################################################
27 from gnuradio import gr, blks2
28 from pubsub import pubsub
29 from constants import *
32 ##################################################
33 # FFT sink block (wrapper for old wxgui)
34 ##################################################
35 class _fft_sink_base(gr.hier_block2, common.wxgui_hb):
37 An fft block with real/complex inputs and a gui window.
50 fft_rate=fft_window.DEFAULT_FRAME_RATE,
54 size=fft_window.DEFAULT_WIN_SIZE,
57 use_persistence=False,
59 **kwargs #do not end with a comma
62 if avg_alpha is None: avg_alpha = 2.0/fft_rate
64 if persist_alpha is None:
65 actual_fft_rate=float(sample_rate/fft_size)/float(max(1,int(float((sample_rate/fft_size)/fft_rate))))
66 #print "requested_fft_rate ",fft_rate
67 #print "actual_fft_rate ",actual_fft_rate
68 analog_cutoff_freq=0.5 # Hertz
69 #calculate alpha from wanted cutoff freq
70 persist_alpha = 1.0 - math.exp(-2.0*math.pi*analog_cutoff_freq/actual_fft_rate)
73 gr.hier_block2.__init__(
76 gr.io_signature(1, 1, self._item_size),
77 gr.io_signature(0, 0, 0),
80 fft = self._fft_chain(
81 sample_rate=sample_rate,
89 msgq = gr.msg_queue(2)
90 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
94 self.controller = pubsub()
95 self.controller.subscribe(AVERAGE_KEY, fft.set_average)
96 self.controller.publish(AVERAGE_KEY, fft.average)
97 self.controller.subscribe(AVG_ALPHA_KEY, fft.set_avg_alpha)
98 self.controller.publish(AVG_ALPHA_KEY, fft.avg_alpha)
99 self.controller.subscribe(SAMPLE_RATE_KEY, fft.set_sample_rate)
100 self.controller.publish(SAMPLE_RATE_KEY, fft.sample_rate)
102 common.input_watcher(msgq, self.controller, MSG_KEY)
104 self.win = fft_window.fft_window(
106 controller=self.controller,
111 baseband_freq=baseband_freq,
112 sample_rate_key=SAMPLE_RATE_KEY,
116 average_key=AVERAGE_KEY,
117 avg_alpha_key=AVG_ALPHA_KEY,
120 use_persistence=use_persistence,
121 persist_alpha=persist_alpha,
123 common.register_access_methods(self, self.win)
124 setattr(self.win, 'set_baseband_freq', getattr(self, 'set_baseband_freq')) #BACKWARDS
125 setattr(self.win, 'set_peak_hold', getattr(self, 'set_peak_hold')) #BACKWARDS
127 self.wxgui_connect(self, fft, sink)
129 class fft_sink_f(_fft_sink_base):
130 _fft_chain = blks2.logpwrfft_f
131 _item_size = gr.sizeof_float
134 class fft_sink_c(_fft_sink_base):
135 _fft_chain = blks2.logpwrfft_c
136 _item_size = gr.sizeof_gr_complex
139 # ----------------------------------------------------------------
140 # Standalone test app
141 # ----------------------------------------------------------------
144 from gnuradio.wxgui import stdgui2
146 class test_app_block (stdgui2.std_top_block):
147 def __init__(self, frame, panel, vbox, argv):
148 stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
152 # build our flow graph
153 input_rate = 2048.0e3
156 noise =gr.noise_source_c(gr.GR_UNIFORM, 1.0/10)
158 # Generate a complex sinusoid
159 #src1 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
160 src1 = gr.sig_source_c (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
162 # We add these throttle blocks so that this demo doesn't
163 # suck down all the CPU available. Normally you wouldn't use these.
164 thr1 = gr.throttle(gr.sizeof_gr_complex, input_rate)
166 sink1 = fft_sink_c (panel, title="Complex Data", fft_size=fft_size,
167 sample_rate=input_rate, baseband_freq=100e3,
168 ref_level=0, y_per_div=20, y_divs=10)
169 vbox.Add (sink1.win, 1, wx.EXPAND)
172 self.connect(src1, (combine1,0))
173 self.connect(noise,(combine1,1))
174 self.connect(combine1,thr1, sink1)
176 #src2 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
177 src2 = gr.sig_source_f (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
178 thr2 = gr.throttle(gr.sizeof_float, input_rate)
179 sink2 = fft_sink_f (panel, title="Real Data", fft_size=fft_size*2,
180 sample_rate=input_rate, baseband_freq=100e3,
181 ref_level=0, y_per_div=20, y_divs=10)
182 vbox.Add (sink2.win, 1, wx.EXPAND)
185 c2f2=gr.complex_to_float()
187 self.connect(src2, (combine2,0))
188 self.connect(noise,c2f2,(combine2,1))
189 self.connect(combine2, thr2,sink2)
192 app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
195 if __name__ == '__main__':