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 *
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,
60 if avg_alpha is None: avg_alpha = 2.0/fft_rate
62 if analog_alpha is None:
63 actual_fft_rate=float(sample_rate/fft_size)/float(max(1,int(float((sample_rate/fft_size)/fft_rate))))
64 #print "requested_fft_rate ",fft_rate
65 #print "actual_fft_rate ",actual_fft_rate
66 analog_cutoff_freq=0.5 # Hertz
67 #calculate alpha from wanted cutoff freq
68 analog_alpha = 1.0 - math.exp(-2.0*math.pi*analog_cutoff_freq/actual_fft_rate)
71 gr.hier_block2.__init__(
74 gr.io_signature(1, 1, self._item_size),
75 gr.io_signature(0, 0, 0),
78 fft = self._fft_chain(
79 sample_rate=sample_rate,
86 msgq = gr.msg_queue(2)
87 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
91 self.controller = pubsub()
92 self.controller.subscribe(AVERAGE_KEY, fft.set_average)
93 self.controller.publish(AVERAGE_KEY, fft.average)
94 self.controller.subscribe(AVG_ALPHA_KEY, fft.set_avg_alpha)
95 self.controller.publish(AVG_ALPHA_KEY, fft.avg_alpha)
96 self.controller.subscribe(SAMPLE_RATE_KEY, fft.set_sample_rate)
97 self.controller.publish(SAMPLE_RATE_KEY, fft.sample_rate)
99 common.input_watcher(msgq, self.controller, MSG_KEY)
101 self.win = fft_window.fft_window(
103 controller=self.controller,
108 baseband_freq=baseband_freq,
109 sample_rate_key=SAMPLE_RATE_KEY,
113 average_key=AVERAGE_KEY,
114 avg_alpha_key=AVG_ALPHA_KEY,
117 emulate_analog=emulate_analog,
118 analog_alpha=analog_alpha,
120 common.register_access_methods(self, self.win)
121 setattr(self.win, 'set_baseband_freq', getattr(self, 'set_baseband_freq')) #BACKWARDS
122 setattr(self.win, 'set_peak_hold', getattr(self, 'set_peak_hold')) #BACKWARDS
124 self.wxgui_connect(self, fft, sink)
126 class fft_sink_f(_fft_sink_base):
127 _fft_chain = blks2.logpwrfft_f
128 _item_size = gr.sizeof_float
131 class fft_sink_c(_fft_sink_base):
132 _fft_chain = blks2.logpwrfft_c
133 _item_size = gr.sizeof_gr_complex
136 # ----------------------------------------------------------------
137 # Standalone test app
138 # ----------------------------------------------------------------
141 from gnuradio.wxgui import stdgui2
143 class test_app_block (stdgui2.std_top_block):
144 def __init__(self, frame, panel, vbox, argv):
145 stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
149 # build our flow graph
150 input_rate = 2048.0e3
153 noise =gr.noise_source_c(gr.GR_UNIFORM, 1.0/10)
155 # Generate a complex sinusoid
156 #src1 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
157 src1 = gr.sig_source_c (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
159 # We add these throttle blocks so that this demo doesn't
160 # suck down all the CPU available. Normally you wouldn't use these.
161 thr1 = gr.throttle(gr.sizeof_gr_complex, input_rate)
163 sink1 = fft_sink_c (panel, title="Complex Data", fft_size=fft_size,
164 sample_rate=input_rate, baseband_freq=100e3,
165 ref_level=0, y_per_div=20, y_divs=10)
166 vbox.Add (sink1.win, 1, wx.EXPAND)
169 self.connect(src1, (combine1,0))
170 self.connect(noise,(combine1,1))
171 self.connect(combine1,thr1, sink1)
173 #src2 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2e3, 1)
174 src2 = gr.sig_source_f (input_rate, gr.GR_CONST_WAVE, 57.50e3, 1)
175 thr2 = gr.throttle(gr.sizeof_float, input_rate)
176 sink2 = fft_sink_f (panel, title="Real Data", fft_size=fft_size*2,
177 sample_rate=input_rate, baseband_freq=100e3,
178 ref_level=0, y_per_div=20, y_divs=10)
179 vbox.Add (sink2.win, 1, wx.EXPAND)
182 c2f2=gr.complex_to_float()
184 self.connect(src2, (combine2,0))
185 self.connect(noise,c2f2,(combine2,1))
186 self.connect(combine2, thr2,sink2)
189 app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
192 if __name__ == '__main__':