Work on examples for the synthesize filterbank block. The cleans up the simple exampl...
[debian/gnuradio] / gnuradio-examples / python / pfb / synth_filter.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2010 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, blks2
24 import scipy, pylab
25
26 def main():
27     N = 1000000
28     fs = 8000
29
30     freqs = [100, 200, 300, 400, 500]
31     nchans = 7
32
33     sigs = list()
34     for fi in freqs:
35         s = gr.sig_source_c(fs, gr.GR_SIN_WAVE, fi, 1)
36         sigs.append(s)
37
38     taps = gr.firdes.low_pass_2(len(freqs), fs, fs/float(nchans)/2, 100, 100)
39     print "Num. Taps = %d (taps per filter = %d)" % (len(taps), 
40                                                      len(taps)/nchans)
41     filtbank = gr.pfb_synthesis_filterbank_ccf(nchans, taps)
42
43     head = gr.head(gr.sizeof_gr_complex, N)
44     snk = gr.vector_sink_c()
45
46     tb = gr.top_block()
47     tb.connect(filtbank, head, snk)
48
49     for i,si in enumerate(sigs):
50         tb.connect(si, (filtbank, i))
51     
52     tb.run()
53
54     if 1:
55         f1 = pylab.figure(1)
56         s1 = f1.add_subplot(1,1,1)
57         s1.plot(snk.data()[1000:])
58         
59         fftlen = 2048
60         f2 = pylab.figure(2)
61         s2 = f2.add_subplot(1,1,1)
62         winfunc = scipy.blackman
63         s2.psd(snk.data()[10000:], NFFT=fftlen,
64                Fs = nchans*fs,
65                noverlap=fftlen/4,
66                window = lambda d: d*winfunc(fftlen))
67
68         pylab.show()
69
70 if __name__ == "__main__":
71     main()