Add analog CRT screen afterglow emulation for gr-wxgui
[debian/gnuradio] / gr-wxgui / src / python / fftsink_gl.py
1 #
2 # Copyright 2008 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                 emulate_analog=False,
57                 analog_alpha=None,
58         ):
59                 #ensure avg alpha
60                 if avg_alpha is None: avg_alpha = 2.0/fft_rate
61                 #ensure analog alpha
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)
69                   
70                 #init
71                 gr.hier_block2.__init__(
72                         self,
73                         "fft_sink",
74                         gr.io_signature(1, 1, self._item_size),
75                         gr.io_signature(0, 0, 0),
76                 )
77                 #blocks
78                 fft = self._fft_chain(
79                         sample_rate=sample_rate,
80                         fft_size=fft_size,
81                         frame_rate=fft_rate,
82                         ref_scale=ref_scale,
83                         avg_alpha=avg_alpha,
84                         average=average,
85                 )
86                 msgq = gr.msg_queue(2)
87                 sink = gr.message_sink(gr.sizeof_float*fft_size, msgq, True)
88
89
90                 #controller
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)
98                 #start input watcher
99                 common.input_watcher(msgq, self.controller, MSG_KEY)
100                 #create window
101                 self.win = fft_window.fft_window(
102                         parent=parent,
103                         controller=self.controller,
104                         size=size,
105                         title=title,
106                         real=self._real,
107                         fft_size=fft_size,
108                         baseband_freq=baseband_freq,
109                         sample_rate_key=SAMPLE_RATE_KEY,
110                         y_per_div=y_per_div,
111                         y_divs=y_divs,
112                         ref_level=ref_level,
113                         average_key=AVERAGE_KEY,
114                         avg_alpha_key=AVG_ALPHA_KEY,
115                         peak_hold=peak_hold,
116                         msg_key=MSG_KEY,
117                         emulate_analog=emulate_analog,
118                         analog_alpha=analog_alpha,
119                 )
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
123                 #connect
124                 self.wxgui_connect(self, fft, sink)
125
126 class fft_sink_f(_fft_sink_base):
127         _fft_chain = blks2.logpwrfft_f
128         _item_size = gr.sizeof_float
129         _real = True
130
131 class fft_sink_c(_fft_sink_base):
132         _fft_chain = blks2.logpwrfft_c
133         _item_size = gr.sizeof_gr_complex
134         _real = False
135
136 # ----------------------------------------------------------------
137 # Standalone test app
138 # ----------------------------------------------------------------
139
140 import wx
141 from gnuradio.wxgui import stdgui2
142
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)
146
147         fft_size = 256
148
149         # build our flow graph
150         input_rate = 2048.0e3
151
152         #Generate some noise
153         noise =gr.noise_source_c(gr.GR_UNIFORM, 1.0/10)
154
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)
158
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)
162
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)
167
168         combine1=gr.add_cc()
169         self.connect(src1, (combine1,0))
170         self.connect(noise,(combine1,1))
171         self.connect(combine1,thr1, sink1)
172
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)
180
181         combine2=gr.add_ff()
182         c2f2=gr.complex_to_float()
183
184         self.connect(src2, (combine2,0))
185         self.connect(noise,c2f2,(combine2,1))
186         self.connect(combine2, thr2,sink2)
187
188 def main ():
189     app = stdgui2.stdapp (test_app_block, "FFT Sink Test App")
190     app.MainLoop ()
191
192 if __name__ == '__main__':
193     main ()