Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / fm_emph.py
1 #
2 # Copyright 2005,2007 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_block2):
39     """
40     FM Deemphasis IIR filter.
41     """
42     
43                             
44     def __init__(self, fs, tau=75e-6):
45         """
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         gr.hier_block2.__init__(self, "fm_deemph",
52                                 gr.io_signature(1, 1, gr.sizeof_float), # Input signature
53                                 gr.io_signature(1, 1, gr.sizeof_float)) # Output signature
54                                 
55         w_p = 1/tau
56         w_pp = math.tan (w_p / (fs * 2)) # prewarped analog freq
57
58         a1 = (w_pp - 1)/(w_pp + 1)
59         b0 = w_pp/(1 + w_pp)
60         b1 = b0
61
62         btaps = [b0, b1]
63         ataps = [1, a1]
64
65         if 0:
66             print "btaps =", btaps
67             print "ataps =", ataps
68             global plot1
69             plot1 = gru.gnuplot_freqz (gru.freqz (btaps, ataps), fs, True)
70
71         deemph = gr.iir_filter_ffd(btaps, ataps)
72         self.connect(self, deemph, self)
73
74 #
75 #         1 + s*t1
76 # H(s) = ----------
77 #         1 + s*t2
78 #
79 # I think this is the right transfer function.
80 #
81 #
82 # This fine ASCII rendition is based on Figure 5-15
83 # in "Digital and Analog Communication Systems", Leon W. Couch II
84 #
85 #
86 #               R1
87 #         +-----||------+
88 #         |             |
89 #  o------+             +-----+--------o
90 #         |      C1     |     |
91 #         +----/\/\/\/--+     \
92 #                             /
93 #                             \ R2
94 #                             /
95 #                             \
96 #                             |
97 #  o--------------------------+--------o
98 #
99 # f1 = 1/(2*pi*t1) = 1/(2*pi*R1*C)
100 #
101 #         1          R1 + R2
102 # f2 = ------- = ------------
103 #      2*pi*t2    2*pi*R1*R2*C
104 #
105 # t1 is 75us in US, 50us in EUR
106 # f2 should be higher than our audio bandwidth.
107 #
108 #
109 # The Bode plot looks like this:
110 #
111 #
112 #                    /----------------
113 #                   /
114 #                  /  <-- slope = 20dB/decade
115 #                 /
116 #   -------------/
117 #               f1    f2
118 #
119 # We prewarp and use the bilinear z-transform to get our IIR coefficients.
120 # See "Digital Signal Processing: A Practical Approach" by Ifeachor and Jervis
121 #
122
123 class fm_preemph(gr.hier_block2):
124     """
125     FM Preemphasis IIR filter.
126     """
127     def __init__(self, fs, tau=75e-6):
128         """
129         @param fs: sampling frequency in Hz
130         @type fs: float
131         @param tau: Time constant in seconds (75us in US, 50us in EUR)
132         @type tau: float
133         """
134
135         gr.hier_block2.__init__(self, "fm_deemph",
136                                 gr.io_signature(1, 1, gr.sizeof_float), # Input signature
137                                 gr.io_signature(1, 1, gr.sizeof_float)) # Output signature
138                                 
139         # FIXME make this compute the right answer
140         
141         btaps = [1]
142         ataps = [1]
143
144         if 0:
145             print "btaps =", btaps
146             print "ataps =", ataps
147             global plot2
148             plot2 = gru.gnuplot_freqz (gru.freqz (btaps, ataps), fs, True)
149
150         preemph = gr.iir_filter_ffd(btaps, ataps)
151         self.connect(self, preemph, self)