PFB channelizer can be specified without external taps. Uses optfir to generate an...
[debian/gnuradio] / gnuradio-examples / python / pfb / channelize.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2009 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 os, time
25 import scipy, pylab
26 from scipy import fftpack
27 from pylab import mlab
28
29 class pfb_top_block(gr.top_block):
30     def __init__(self):
31         gr.top_block.__init__(self)
32
33         self._N = 2000000        # number of samples to use
34         self._fs = 9000          # initial sampling rate
35         self._M = 9              # Number of channels to channelize
36
37         # Create a set of taps for the PFB channelizer
38         self._taps = gr.firdes.low_pass_2(1, self._fs, 475.50, 50, 
39                                           attenuation_dB=100, window=gr.firdes.WIN_BLACKMAN_hARRIS)
40
41         # Calculate the number of taps per channel for our own information
42         tpc = scipy.ceil(float(len(self._taps)) /  float(self._M))
43         print "Number of taps:     ", len(self._taps)
44         print "Number of channels: ", self._M
45         print "Taps per channel:   ", tpc
46         
47         # Create a set of signals at different frequencies
48         #   freqs lists the frequencies of the signals that get stored 
49         #   in the list "signals", which then get summed together
50         self.signals = list()
51         self.add = gr.add_cc()
52         freqs = [-4070, -3050, -2030, -1010, 10, 1020, 2040, 3060, 4080]
53         for i in xrange(len(freqs)):
54             self.signals.append(gr.sig_source_c(self._fs, gr.GR_SIN_WAVE, freqs[i], 1))
55             self.connect(self.signals[i], (self.add,i))
56
57         self.head = gr.head(gr.sizeof_gr_complex, self._N)
58
59         # Construct the channelizer filter
60         self.pfb = blks2.pfb_channelizer_ccf(self._M, self._taps)
61
62         # Construct a vector sink for the input signal to the channelizer
63         self.snk_i = gr.vector_sink_c()
64
65         # Connect the blocks
66         self.connect(self.add, self.head, self.pfb)
67         self.connect(self.add, self.snk_i)
68
69         # Create a vector sink for each of M output channels of the filter and connect it
70         self.snks = list()
71         for i in xrange(self._M):
72             self.snks.append(gr.vector_sink_c())
73             self.connect((self.pfb, i), self.snks[i])
74                              
75
76 def main():
77     tstart = time.time()
78     
79     tb = pfb_top_block()
80     tb.run()
81
82     tend = time.time()
83     print "Run time: %f" % (tend - tstart)
84
85     if 1:
86         fig_in = pylab.figure(1, figsize=(16,9), facecolor="w")
87         fig1 = pylab.figure(2, figsize=(16,9), facecolor="w")
88         fig2 = pylab.figure(3, figsize=(16,9), facecolor="w")
89         
90         Ns = 1000
91         Ne = 10000
92
93         fftlen = 8192
94         winfunc = scipy.blackman
95         fs = tb._fs
96
97         # Plot the input signal on its own figure
98         d = tb.snk_i.data()[Ns:Ne]
99         spin_f = fig_in.add_subplot(2, 1, 1)
100
101         X,freq = mlab.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs,
102                           window = lambda d: d*winfunc(fftlen),
103                           scale_by_freq=True)
104         X_in = 10.0*scipy.log10(abs(X))
105         f_in = scipy.arange(-fs/2.0, fs/2.0, fs/float(X_in.size))
106         pin_f = spin_f.plot(f_in, X_in, "b")
107         spin_f.set_xlim([min(f_in), max(f_in)+1]) 
108         spin_f.set_ylim([-200.0, 50.0]) 
109
110         spin_f.set_title("Input Signal", weight="bold")
111         spin_f.set_xlabel("Frequency (Hz)")
112         spin_f.set_ylabel("Power (dBW)")
113
114
115         Ts = 1.0/fs
116         Tmax = len(d)*Ts
117         
118         t_in = scipy.arange(0, Tmax, Ts)
119         x_in = scipy.array(d)
120         spin_t = fig_in.add_subplot(2, 1, 2)
121         pin_t = spin_t.plot(t_in, x_in.real, "b")
122         pin_t = spin_t.plot(t_in, x_in.imag, "r")
123
124         spin_t.set_xlabel("Time (s)")
125         spin_t.set_ylabel("Amplitude")
126
127         Ncols = int(scipy.floor(scipy.sqrt(tb._M)))
128         Nrows = int(scipy.floor(tb._M / Ncols))
129         if(tb._M % Ncols != 0):
130             Nrows += 1
131
132         # Plot each of the channels outputs. Frequencies on Figure 2 and
133         # time signals on Figure 3
134         fs_o = tb._fs / tb._M
135         Ts_o = 1.0/fs_o
136         Tmax_o = len(d)*Ts_o
137         for i in xrange(len(tb.snks)):
138             # remove issues with the transients at the beginning
139             # also remove some corruption at the end of the stream
140             #    this is a bug, probably due to the corner cases
141             d = tb.snks[i].data()[Ns:Ne]
142
143             sp1_f = fig1.add_subplot(Nrows, Ncols, 1+i)
144             X,freq = mlab.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs_o,
145                               window = lambda d: d*winfunc(fftlen),
146                               scale_by_freq=True)
147             X_o = 10.0*scipy.log10(abs(X))
148             f_o = scipy.arange(-fs_o/2.0, fs_o/2.0, fs_o/float(X_o.size))
149             p2_f = sp1_f.plot(f_o, X_o, "b")
150             sp1_f.set_xlim([min(f_o), max(f_o)+1]) 
151             sp1_f.set_ylim([-200.0, 50.0]) 
152
153             sp1_f.set_title(("Channel %d" % i), weight="bold")
154             sp1_f.set_xlabel("Frequency (Hz)")
155             sp1_f.set_ylabel("Power (dBW)")
156
157             x_o = scipy.array(d)
158             t_o = scipy.arange(0, Tmax_o, Ts_o)
159             sp2_o = fig2.add_subplot(Nrows, Ncols, 1+i)
160             p2_o = sp2_o.plot(t_o, x_o.real, "b")
161             p2_o = sp2_o.plot(t_o, x_o.imag, "r")
162             sp2_o.set_xlim([min(t_o), max(t_o)+1]) 
163             sp2_o.set_ylim([-2, 2]) 
164
165             sp2_o.set_title(("Channel %d" % i), weight="bold")
166             sp2_o.set_xlabel("Time (s)")
167             sp2_o.set_ylabel("Amplitude")
168
169         pylab.show()
170
171
172 if __name__ == "__main__":
173     try:
174         main()
175     except KeyboardInterrupt:
176         pass
177