Merging trondeau/pfb r11249:11581 into trunk. This adds a few polyphase filterbank...
[debian/gnuradio] / gnuradio-examples / python / pfb / chirp_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 = 200000         # 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, 500, 20, 
39                                           attenuation_dB=10, 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         repeated = True
48         if(repeated):
49             self.vco_input = gr.sig_source_f(self._fs, gr.GR_SIN_WAVE, 0.25, 110)
50         else:
51             amp = 100
52             data = scipy.arange(0, amp, amp/float(self._N))
53             self.vco_input = gr.vector_source_f(data, False)
54             
55         # Build a VCO controlled by either the sinusoid or single chirp tone
56         # Then convert this to a complex signal
57         self.vco = gr.vco_f(self._fs, 225, 1)
58         self.f2c = gr.float_to_complex()
59
60         self.head = gr.head(gr.sizeof_gr_complex, self._N)
61
62         # Construct the channelizer filter
63         self.pfb = blks2.pfb_channelizer_ccf(self._M, self._taps)
64
65         # Construct a vector sink for the input signal to the channelizer
66         self.snk_i = gr.vector_sink_c()
67
68         # Connect the blocks
69         self.connect(self.vco_input, self.vco, self.f2c)
70         self.connect(self.f2c, self.head, self.pfb)
71         self.connect(self.f2c, self.snk_i)
72
73         # Create a vector sink for each of M output channels of the filter and connect it
74         self.snks = list()
75         for i in xrange(self._M):
76             self.snks.append(gr.vector_sink_c())
77             self.connect((self.pfb, i), self.snks[i])
78                              
79
80 def main():
81     tstart = time.time()
82     
83     tb = pfb_top_block()
84     tb.run()
85
86     tend = time.time()
87     print "Run time: %f" % (tend - tstart)
88
89     if 1:
90         fig_in = pylab.figure(1, figsize=(16,9), facecolor="w")
91         fig1 = pylab.figure(2, figsize=(16,9), facecolor="w")
92         fig2 = pylab.figure(3, figsize=(16,9), facecolor="w")
93         fig3 = pylab.figure(4, figsize=(16,9), facecolor="w")
94         
95         Ns = 650
96         Ne = 20000
97
98         fftlen = 8192
99         winfunc = scipy.blackman
100         fs = tb._fs
101
102         # Plot the input signal on its own figure
103         d = tb.snk_i.data()[Ns:Ne]
104         spin_f = fig_in.add_subplot(2, 1, 1)
105
106         X,freq = mlab.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs,
107                           window = lambda d: d*winfunc(fftlen),
108                           scale_by_freq=True)
109         X_in = 10.0*scipy.log10(abs(fftpack.fftshift(X)))
110         f_in = scipy.arange(-fs/2.0, fs/2.0, fs/float(X_in.size))
111         pin_f = spin_f.plot(f_in, X_in, "b")
112         spin_f.set_xlim([min(f_in), max(f_in)+1]) 
113         spin_f.set_ylim([-200.0, 50.0]) 
114
115         spin_f.set_title("Input Signal", weight="bold")
116         spin_f.set_xlabel("Frequency (Hz)")
117         spin_f.set_ylabel("Power (dBW)")
118
119
120         Ts = 1.0/fs
121         Tmax = len(d)*Ts
122         
123         t_in = scipy.arange(0, Tmax, Ts)
124         x_in = scipy.array(d)
125         spin_t = fig_in.add_subplot(2, 1, 2)
126         pin_t = spin_t.plot(t_in, x_in.real, "b")
127         pin_t = spin_t.plot(t_in, x_in.imag, "r")
128
129         spin_t.set_xlabel("Time (s)")
130         spin_t.set_ylabel("Amplitude")
131
132         Ncols = int(scipy.floor(scipy.sqrt(tb._M)))
133         Nrows = int(scipy.floor(tb._M / Ncols))
134         if(tb._M % Ncols != 0):
135             Nrows += 1
136
137         # Plot each of the channels outputs. Frequencies on Figure 2 and
138         # time signals on Figure 3
139         fs_o = tb._fs / tb._M
140         Ts_o = 1.0/fs_o
141         Tmax_o = len(d)*Ts_o
142         for i in xrange(len(tb.snks)):
143             # remove issues with the transients at the beginning
144             # also remove some corruption at the end of the stream
145             #    this is a bug, probably due to the corner cases
146             d = tb.snks[i].data()[Ns:Ne]
147
148             sp1_f = fig1.add_subplot(Nrows, Ncols, 1+i)
149             X,freq = mlab.psd(d, NFFT=fftlen, noverlap=fftlen/4, Fs=fs_o,
150                               window = lambda d: d*winfunc(fftlen),
151                               scale_by_freq=True)
152             X_o = 10.0*scipy.log10(abs(X))
153             f_o = freq
154             p2_f = sp1_f.plot(f_o, X_o, "b")
155             sp1_f.set_xlim([min(f_o), max(f_o)+1]) 
156             sp1_f.set_ylim([-200.0, 50.0]) 
157
158             sp1_f.set_title(("Channel %d" % i), weight="bold")
159             sp1_f.set_xlabel("Frequency (Hz)")
160             sp1_f.set_ylabel("Power (dBW)")
161
162             x_o = scipy.array(d)
163             t_o = scipy.arange(0, Tmax_o, Ts_o)
164             sp2_o = fig2.add_subplot(Nrows, Ncols, 1+i)
165             p2_o = sp2_o.plot(t_o, x_o.real, "b")
166             p2_o = sp2_o.plot(t_o, x_o.imag, "r")
167             sp2_o.set_xlim([min(t_o), max(t_o)+1]) 
168             sp2_o.set_ylim([-2, 2]) 
169
170             sp2_o.set_title(("Channel %d" % i), weight="bold")
171             sp2_o.set_xlabel("Time (s)")
172             sp2_o.set_ylabel("Amplitude")
173
174
175             sp3 = fig3.add_subplot(1,1,1)
176             p3 = sp3.plot(t_o, x_o.real)
177             sp3.set_xlim([min(t_o), max(t_o)+1]) 
178             sp3.set_ylim([-2, 2]) 
179
180         sp3.set_title("All Channels")
181         sp3.set_xlabel("Time (s)")
182         sp3.set_ylabel("Amplitude") 
183
184         pylab.show()
185
186
187 if __name__ == "__main__":
188     try:
189         main()
190     except KeyboardInterrupt:
191         pass
192