]> git.gag.com Git - debian/gnuradio/blob - gr-wxgui/src/python/fftsink_gl.py
6cfaeff606b67f83ebc611a62fb82157f2c31f65
[debian/gnuradio] / gr-wxgui / src / python / fftsink_gl.py
1 #
2 # Copyright 2008,2009,2010 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 from __future__ import division
23
24 ##################################################
25 # Imports
26 ##################################################
27 import fft_window
28 import common
29 from gnuradio import gr, blks2
30 from pubsub import pubsub
31 from constants import *
32 import math
33
34 ##################################################
35 # FFT sink block (wrapper for old wxgui)
36 ##################################################
37 class _fft_sink_base(gr.hier_block2, common.wxgui_hb):
38         """
39         An fft block with real/complex inputs and a gui window.
40         """
41
42         def __init__(
43                 self,
44                 parent,
45                 baseband_freq=0,
46                 ref_scale=2.0,
47                 y_per_div=10,
48                 y_divs=8,
49                 ref_level=50,
50                 sample_rate=1,
51                 fft_size=512,
52                 fft_rate=fft_window.DEFAULT_FRAME_RATE,
53                 average=False,
54                 avg_alpha=None,
55                 title='',
56                 size=fft_window.DEFAULT_WIN_SIZE,
57                 peak_hold=False,
58                 win=None,
59                 use_persistence=False,
60                 persist_alpha=None,
61                 **kwargs #do not end with a comma
62         ):
63                 #ensure avg alpha
64                 if avg_alpha is None: avg_alpha = 2.0/fft_rate
65                 #ensure analog alpha
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)
73                   
74                 #init
75                 gr.hier_block2.__init__(
76                         self,
77                         "fft_sink",
78                         gr.io_signature(1, 1, self._item_size),
79                         gr.io_signature(0, 0, 0),
80                 )
81                 #blocks
82                 fft = self._fft_chain(
83                         sample_rate=sample_rate,
84                         fft_size=fft_size,
85                         frame_rate=fft_rate,
86                         ref_scale=ref_scale,
87                         avg_alpha=avg_alpha,
88                         average=average,
89                         win=win,
90                 )
91                 msgq = gr.msg_queue(2)
92                 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
93
94
95                 #controller
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)
103                 #start input watcher
104                 common.input_watcher(msgq, self.controller, MSG_KEY)
105                 #create window
106                 self.win = fft_window.fft_window(
107                         parent=parent,
108                         controller=self.controller,
109                         size=size,
110                         title=title,
111                         real=self._real,
112                         fft_size=fft_size,
113                         baseband_freq=baseband_freq,
114                         sample_rate_key=SAMPLE_RATE_KEY,
115                         y_per_div=y_per_div,
116                         y_divs=y_divs,
117                         ref_level=ref_level,
118                         average_key=AVERAGE_KEY,
119                         avg_alpha_key=AVG_ALPHA_KEY,
120                         peak_hold=peak_hold,
121                         msg_key=MSG_KEY,
122                         use_persistence=use_persistence,
123                         persist_alpha=persist_alpha,
124                 )
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
128                 #connect
129                 self.wxgui_connect(self, fft, sink)
130
131 class fft_sink_f(_fft_sink_base):
132         _fft_chain = blks2.logpwrfft_f
133         _item_size = gr.sizeof_float
134         _real = True
135
136 class fft_sink_c(_fft_sink_base):
137         _fft_chain = blks2.logpwrfft_c
138         _item_size = gr.sizeof_gr_complex
139         _real = False
140
141 # ----------------------------------------------------------------
142 # Standalone test app
143 # ----------------------------------------------------------------
144
145 import wx
146 from gnuradio.wxgui import stdgui2
147
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)
151
152         fft_size = 256
153
154         # build our flow graph
155         input_rate = 2048.0e3
156
157         #Generate some noise
158         noise =gr.noise_source_c(gr.GR_UNIFORM, 1.0/10)
159
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)
163
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)
167
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)
172
173         combine1=gr.add_cc()
174         self.connect(src1, (combine1,0))
175         self.connect(noise,(combine1,1))
176         self.connect(combine1,thr1, sink1)
177
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)
185
186         combine2=gr.add_ff()
187         c2f2=gr.complex_to_float()
188
189         self.connect(src2, (combine2,0))
190         self.connect(noise,c2f2,(combine2,1))
191         self.connect(combine2, thr2,sink2)
192
193 def main ():
194     app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
195     app.MainLoop ()
196
197 if __name__ == '__main__':
198     main ()