Under extreme circumstances, optfir might never produce an answer (atten>300), so...
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / pfb_arb_resampler.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, optfir
24
25 class pfb_arb_resampler_ccf(gr.hier_block2):
26     '''
27     Convenience wrapper for the polyphase filterbank arbitrary resampler.
28
29     The block takes a single complex stream in and outputs a single complex
30     stream out. As such, it requires no extra glue to handle the input/output
31     streams. This block is provided to be consistent with the interface to the
32     other PFB block.
33     '''
34     def __init__(self, rate, taps=None, flt_size=32, atten=100):
35         gr.hier_block2.__init__(self, "pfb_arb_resampler_ccf",
36                                 gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
37                                 gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature
38         
39         self._rate = rate
40         self._size = flt_size
41
42         if taps is not None:
43             self._taps = taps
44         else:
45             # Create a filter that covers the full bandwidth of the input signal
46             bw = 0.4
47             tb = 0.2
48             ripple = 0.1
49             #self._taps = gr.firdes.low_pass_2(self._size, self._size, bw, tb, atten)
50             made = False
51             while not made:
52                 try:
53                     self._taps = optfir.low_pass(self._size, self._size, bw, bw+tb, ripple, atten)
54                     made = True
55                 except RuntimeError:
56                     ripple += 0.01
57                     made = False
58                     print("Warning: set ripple to %.4f dB. If this is a problem, adjust the attenuation or create your own filter taps." % (ripple))
59
60                     # Build in an exit strategy; if we've come this far, it ain't working.
61                     if(ripple >= 1.0):
62                         raise RuntimeError("optfir could not generate an appropriate filter.")
63
64         self.pfb = gr.pfb_arb_resampler_ccf(self._rate, self._taps, self._size)
65
66         self.connect(self, self.pfb)
67         self.connect(self.pfb, self)
68         
69     def set_taps(self, taps):
70         self.pfb.set_taps(taps)