2 # Copyright 2005 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 from gnuradio import gr, gru
26 def design_filter(interpolation, decimation, fractional_bw):
28 Given the interpolation rate, decimation rate and a fractional bandwidth,
31 @param interpolation: interpolation factor
32 @type interpolation: integer > 0
33 @param decimation: decimation factor
34 @type decimation: integer > 0
35 @param fractional_bw: fractional bandwidth in (0, 0.5) 0.4 works well.
36 @type fractional_bw: float
37 @returns: sequence of numbers
42 if fractional_bw >= 0.5 or fractional_bw <= 0:
43 raise ValueError, "Invalid fractional_bandwidth, must be in (0, 0.5)"
46 trans_width = 0.5 - fractional_bw
47 mid_transition_band = 0.5 - trans_width/2
49 taps = gr.firdes.low_pass(interpolation, # gain
51 mid_transition_band/interpolation, # trans mid point
52 trans_width/interpolation, # transition width
56 # print "len(resampler_taps) =", len(taps)
57 # _plot = gru.gnuplot_freqz(gru.freqz(taps, 1), 1)
63 class _rational_resampler_base(gr.hier_block):
65 base class for all rational resampler variants.
67 def __init__(self, resampler_base, fg,
68 interpolation, decimation, taps=None, fractional_bw=None):
70 Rational resampling polyphase FIR filter.
72 Either taps or fractional_bw may be specified, but not both.
73 If neither is specified, a reasonable default, 0.4, is used as
77 @param interpolation: interpolation factor
78 @type interpolation: integer > 0
79 @param decimation: decimation factor
80 @type decimation: integer > 0
81 @param taps: optional filter coefficients
83 @param fractional_bw: fractional bandwidth in (0, 0.5), measured at final freq (use 0.4)
84 @type fractional_bw: float
87 if not isinstance(interpolation, int) or interpolation < 1:
88 raise ValueError, "interpolation must be an integer >= 1"
90 if not isinstance(decimation, int) or decimation < 1:
91 raise ValueError, "decimation must be an integer >= 1"
93 if taps is None and fractional_bw is None:
96 d = gru.gcd(interpolation, decimation)
97 interpolation = interpolation // d
98 decimation = decimation // d
101 taps = design_filter(interpolation, decimation, fractional_bw)
103 resampler = resampler_base(interpolation, decimation, taps)
104 gr.hier_block.__init__(self, fg, resampler, resampler)
108 class rational_resampler_fff(_rational_resampler_base):
109 def __init__(self, fg, interpolation, decimation, taps=None, fractional_bw=None):
111 Rational resampling polyphase FIR filter with
112 float input, float output and float taps.
114 _rational_resampler_base.__init__(self, gr.rational_resampler_base_fff, fg,
115 interpolation, decimation,
118 class rational_resampler_ccf(_rational_resampler_base):
119 def __init__(self, fg, interpolation, decimation, taps=None, fractional_bw=None):
121 Rational resampling polyphase FIR filter with
122 complex input, complex output and float taps.
124 _rational_resampler_base.__init__(self, gr.rational_resampler_base_ccf, fg,
125 interpolation, decimation,
128 class rational_resampler_ccc(_rational_resampler_base):
129 def __init__(self, fg, interpolation, decimation, taps=None, fractional_bw=None):
131 Rational resampling polyphase FIR filter with
132 complex input, complex output and complex taps.
134 _rational_resampler_base.__init__(self, gr.rational_resampler_base_ccc, fg,
135 interpolation, decimation,