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