Work on examples for the synthesize filterbank block. The cleans up the simple exampl...
[debian/gnuradio] / gnuradio-examples / python / pfb / synth_to_chan.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     fmtx = list()
35     for fi in freqs:
36         s = gr.sig_source_f(fs, gr.GR_SIN_WAVE, fi, 1)
37         fm = blks2.nbfm_tx (fs, 4*fs, max_dev=10000, tau=75e-6)
38         sigs.append(s)
39         fmtx.append(fm)
40
41     syntaps = gr.firdes.low_pass_2(len(freqs), fs, fs/float(nchans)/2, 100, 100)
42     print "Synthesis Num. Taps = %d (taps per filter = %d)" % (len(syntaps), 
43                                                                len(syntaps)/nchans)
44     chtaps = gr.firdes.low_pass_2(len(freqs), fs, fs/float(nchans)/2, 100, 100)
45     print "Channelizer Num. Taps = %d (taps per filter = %d)" % (len(chtaps), 
46                                                                  len(chtaps)/nchans)
47     filtbank = gr.pfb_synthesis_filterbank_ccf(nchans, syntaps)
48     channelizer = blks2.pfb_channelizer_ccf(nchans, chtaps)
49
50     noise_level = 0.01
51     head = gr.head(gr.sizeof_gr_complex, N)
52     noise = gr.noise_source_c(gr.GR_GAUSSIAN, noise_level)
53     addnoise = gr.add_cc()
54     snk_synth = gr.vector_sink_c()
55
56     tb = gr.top_block()
57
58     tb.connect(noise, (addnoise,0))
59     tb.connect(filtbank, head, (addnoise, 1))
60     tb.connect(addnoise, channelizer)
61     tb.connect(addnoise, snk_synth)
62
63     snk = list()
64     for i,si in enumerate(sigs):
65         tb.connect(si, fmtx[i], (filtbank, i))
66
67     for i in xrange(nchans):
68         snk.append(gr.vector_sink_c())
69         tb.connect((channelizer, i), snk[i])
70     
71     tb.run()
72
73     if 1:
74         channel = 1
75         data = snk[channel].data()[1000:]
76
77         f1 = pylab.figure(1)
78         s1 = f1.add_subplot(1,1,1)
79         s1.plot(data[10000:10200] )
80         s1.set_title(("Output Signal from Channel %d" % channel))
81         
82         fftlen = 2048
83         winfunc = scipy.blackman
84         #winfunc = scipy.hamming
85
86         f2 = pylab.figure(2)
87         s2 = f2.add_subplot(1,1,1)
88         s2.psd(data, NFFT=fftlen,
89                Fs = nchans*fs,
90                noverlap=fftlen/4,
91                window = lambda d: d*winfunc(fftlen))
92         s2.set_title(("Output PSD from Channel %d" % channel))
93
94         f3 = pylab.figure(3)
95         s3 = f3.add_subplot(1,1,1)
96         s3.psd(snk_synth.data()[1000:], NFFT=fftlen,
97                Fs = nchans*fs,
98                noverlap=fftlen/4,
99                window = lambda d: d*winfunc(fftlen))
100         s3.set_title("Output of Synthesis Filter")
101
102         pylab.show()
103
104 if __name__ == "__main__":
105     main()