Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / usrp / test_dft_synth.py
1 #!/usr/bin/env python
2
3 from gnuradio import gr, gru, blks
4 from gnuradio.wxgui import stdgui, fftsink
5 from gnuradio.eng_option import eng_option
6 from optparse import OptionParser
7 import wx
8 import random
9
10
11 def make_random_complex_tuple(L, gain=1):
12     result = []
13     for x in range(L):
14         result.append(gain * complex(random.gauss(0, 1),random.gauss(0, 1)))
15                       
16     return tuple(result)
17
18 def random_noise_c(gain=1):
19     src = gr.vector_source_c(make_random_complex_tuple(32*1024, gain), True)
20     return src
21
22
23 class test_graph (stdgui.gui_flow_graph):
24     def __init__(self, frame, panel, vbox, argv):
25         stdgui.gui_flow_graph.__init__(self, frame, panel, vbox, argv)
26
27         parser = OptionParser (option_class=eng_option)
28         (options, args) = parser.parse_args ()
29
30         sample_rate = 16e6
31         mpoints = 16
32         ampl = 1000
33         
34         enable = mpoints * [0]
35         enable[0] = 1
36
37         taps = gr.firdes.low_pass(1,   # gain
38                                   1,   # rate
39                                   1.0/mpoints * 0.4,  # cutoff
40                                   1.0/mpoints * 0.1,  # trans width
41                                   gr.firdes.WIN_HANN)
42
43         synth = blks.synthesis_filterbank(self, mpoints, taps)
44         
45         null_source = gr.null_source(gr.sizeof_gr_complex)
46         
47         if 0:
48             for i in range(mpoints):
49                 s = gr.sig_source_c(sample_rate/mpoints, gr.GR_SIN_WAVE,
50                                     300e3, ampl * enable[i], 0)
51                 self.connect(s, (synth, i))
52
53         else:
54             for i in range(mpoints):
55                 if i == 0:
56                     s = gr.sig_source_c(sample_rate/mpoints, gr.GR_SIN_WAVE,
57                                         300e3, ampl * enable[i], 0)
58                     #s = random_noise_c(ampl)
59                     self.connect(s, (synth, i))
60                 else:
61                     self.connect(null_source, (synth, i))
62             
63
64         # We add these throttle blocks so that this demo doesn't
65         # suck down all the CPU available.  Normally you wouldn't use these.
66         thr = gr.throttle(gr.sizeof_gr_complex, sample_rate)
67         fft = fftsink.fft_sink_c(self, frame, fft_size=1024,
68                                  sample_rate=sample_rate)
69         vbox.Add(fft.win, 1, wx.EXPAND)
70
71         self.connect(synth, thr, fft)
72
73
74 def main ():
75     app = stdgui.stdapp (test_graph, "Test DFT filterbank")
76     app.MainLoop ()
77
78 if __name__ == '__main__':
79     main ()