Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / fm_emph.py
1 #
2 # Copyright 2005 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
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 3, or (at your option)
9 # any later version.
10
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.
15
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.
20
21
22 from gnuradio import gr
23 import math
24
25
26 #
27 #           1
28 # H(s) = -------
29 #         1 + s
30 #
31 # tau is the RC time constant.
32 # critical frequency: w_p = 1/tau
33 #
34 # We prewarp and use the bilinear z-transform to get our IIR coefficients.
35 # See "Digital Signal Processing: A Practical Approach" by Ifeachor and Jervis
36 #
37
38 class fm_deemph(gr.hier_block):
39     """
40     FM Deemphasis IIR filter.
41     """
42     def __init__(self, fg, fs, tau=75e-6):
43         """
44         @param fg: flow graph
45         @type fg: gr.flow_graph
46         @param fs: sampling frequency in Hz
47         @type fs: float
48         @param tau: Time constant in seconds (75us in US, 50us in EUR)
49         @type tau: float
50         """
51         w_p = 1/tau
52         w_pp = math.tan (w_p / (fs * 2)) # prewarped analog freq
53
54         a1 = (w_pp - 1)/(w_pp + 1)
55         b0 = w_pp/(1 + w_pp)
56         b1 = b0
57
58         btaps = [b0, b1]
59         ataps = [1, a1]
60
61         if 0:
62             print "btaps =", btaps
63             print "ataps =", ataps
64             global plot1
65             plot1 = gru.gnuplot_freqz (gru.freqz (btaps, ataps), fs, True)
66
67         deemph = gr.iir_filter_ffd(btaps, ataps)
68         gr.hier_block.__init__(self, fg, deemph, deemph)
69
70 #
71 #         1 + s*t1
72 # H(s) = ----------
73 #         1 + s*t2
74 #
75 # I think this is the right transfer function.
76 #
77 #
78 # This fine ASCII rendition is based on Figure 5-15
79 # in "Digital and Analog Communication Systems", Leon W. Couch II
80 #
81 #
82 #               R1
83 #         +-----||------+
84 #         |             |
85 #  o------+             +-----+--------o
86 #         |      C1     |     |
87 #         +----/\/\/\/--+     \
88 #                             /
89 #                             \ R2
90 #                             /
91 #                             \
92 #                             |
93 #  o--------------------------+--------o
94 #
95 # f1 = 1/(2*pi*t1) = 1/(2*pi*R1*C)
96 #
97 #         1          R1 + R2
98 # f2 = ------- = ------------
99 #      2*pi*t2    2*pi*R1*R2*C
100 #
101 # t1 is 75us in US, 50us in EUR
102 # f2 should be higher than our audio bandwidth.
103 #
104 #
105 # The Bode plot looks like this:
106 #
107 #
108 #                    /----------------
109 #                   /
110 #                  /  <-- slope = 20dB/decade
111 #                 /
112 #   -------------/
113 #               f1    f2
114 #
115 # We prewarp and use the bilinear z-transform to get our IIR coefficients.
116 # See "Digital Signal Processing: A Practical Approach" by Ifeachor and Jervis
117 #
118
119 class fm_preemph(gr.hier_block):
120     """
121     FM Preemphasis IIR filter.
122     """
123     def __init__(self, fg, fs, tau=75e-6):
124         """
125         @param fg: flow graph
126         @type fg: gr.flow_graph
127         @param fs: sampling frequency in Hz
128         @type fs: float
129         @param tau: Time constant in seconds (75us in US, 50us in EUR)
130         @type tau: float
131         """
132
133         # FIXME make this compute the right answer
134         
135         btaps = [1]
136         ataps = [1]
137
138         if 0:
139             print "btaps =", btaps
140             print "ataps =", ataps
141             global plot2
142             plot2 = gru.gnuplot_freqz (gru.freqz (btaps, ataps), fs, True)
143
144         preemph = gr.iir_filter_ffd(btaps, ataps)
145         gr.hier_block.__init__(self, fg, preemph, preemph)