Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blksimpl / wfm_rcv_pll.py
1 #
2 # Copyright 2005,2006 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 from gnuradio.blksimpl.fm_emph import fm_deemph
24 import math
25
26 class wfm_rcv_pll(gr.hier_block):
27     def __init__ (self, fg, demod_rate, audio_decimation):
28         """
29         Hierarchical block for demodulating a broadcast FM signal.
30         
31         The input is the downconverted complex baseband signal (gr_complex).
32         The output is two streams of the demodulated audio (float) 0=Left, 1=Right.
33         
34         @param fg: flow graph.
35         @type fg: flow graph
36         @param demod_rate: input sample rate of complex baseband input.
37         @type demod_rate: float
38         @param audio_decimation: how much to decimate demod_rate to get to audio.
39         @type audio_decimation: integer
40         """
41
42         bandwidth = 200e3
43         audio_rate = demod_rate / audio_decimation
44
45
46         # We assign to self so that outsiders can grab the demodulator 
47         # if they need to.  E.g., to plot its output.
48         #
49         # input: complex; output: float
50         alpha = 0.25*bandwidth * math.pi / demod_rate
51         beta = alpha * alpha / 4.0
52         max_freq = 2.0*math.pi*100e3/demod_rate
53             
54         self.fm_demod = gr.pll_freqdet_cf (alpha,beta,max_freq,-max_freq)
55
56         # input: float; output: float
57         self.deemph_Left  = fm_deemph (fg, audio_rate)
58         self.deemph_Right = fm_deemph (fg, audio_rate)
59         
60         # compute FIR filter taps for audio filter
61         width_of_transition_band = audio_rate / 32
62         audio_coeffs = gr.firdes.low_pass (1.0 ,         # gain
63                                            demod_rate,      # sampling rate
64                                            15000 ,
65                                            width_of_transition_band,
66                                            gr.firdes.WIN_HAMMING)
67         # input: float; output: float
68         self.audio_filter = gr.fir_filter_fff (audio_decimation, audio_coeffs)
69         if 1:
70             # Pick off the stereo carrier/2 with this filter. It attenuated 10 dB so apply 10 dB gain
71             # We pick off the negative frequency half because we want to base band by it!
72             ##  NOTE  THIS WAS HACKED TO OFFSET INSERTION LOSS DUE TO DEEMPHASIS
73
74             stereo_carrier_filter_coeffs = gr.firdes.complex_band_pass(10.0,
75                                                                    demod_rate,
76                                                                    -19020,
77                                                                    -18980,
78                                                                    width_of_transition_band,
79                                                                    gr.firdes.WIN_HAMMING)
80             
81             #print "len stereo carrier filter = ",len(stereo_carrier_filter_coeffs)
82             #print "stereo carrier filter ", stereo_carrier_filter_coeffs
83             #print "width of transition band = ",width_of_transition_band, " audio rate = ", audio_rate
84
85             # Pick off the double side band suppressed carrier Left-Right audio. It is attenuated 10 dB so apply 10 dB gain
86
87             stereo_dsbsc_filter_coeffs = gr.firdes.complex_band_pass(20.0,
88                                                                      demod_rate,
89                                                                      38000-15000/2,
90                                                                      38000+15000/2,
91                                                                      width_of_transition_band,
92                                                                      gr.firdes.WIN_HAMMING)
93             #print "len stereo dsbsc filter = ",len(stereo_dsbsc_filter_coeffs)
94             #print "stereo dsbsc filter ", stereo_dsbsc_filter_coeffs
95             # construct overlap add filter system from coefficients for stereo carrier
96
97             self.stereo_carrier_filter = gr.fir_filter_fcc(audio_decimation, stereo_carrier_filter_coeffs)
98
99             # carrier is twice the picked off carrier so arrange to do a commplex multiply
100
101             self.stereo_carrier_generator = gr.multiply_cc();
102
103             # Pick off the rds signal
104
105             stereo_rds_filter_coeffs = gr.firdes.complex_band_pass(30.0,
106                                                                      demod_rate,
107                                                                      57000 - 1500,
108                                                                      57000 + 1500,
109                                                                      width_of_transition_band,
110                                                                      gr.firdes.WIN_HAMMING)
111             #print "len stereo dsbsc filter = ",len(stereo_dsbsc_filter_coeffs)
112             #print "stereo dsbsc filter ", stereo_dsbsc_filter_coeffs
113             # construct overlap add filter system from coefficients for stereo carrier
114
115             self.stereo_carrier_filter = gr.fir_filter_fcc(audio_decimation, stereo_carrier_filter_coeffs)
116             self.rds_signal_filter = gr.fir_filter_fcc(audio_decimation, stereo_rds_filter_coeffs)
117
118
119
120
121
122
123             self.rds_carrier_generator = gr.multiply_cc();
124             self.rds_signal_generator = gr.multiply_cc();
125             self_rds_signal_processor = gr.null_sink(gr.sizeof_gr_complex);
126
127
128
129             alpha = 5 * 0.25 * math.pi / (audio_rate)
130             beta = alpha * alpha / 4.0
131             max_freq = -2.0*math.pi*18990/audio_rate;
132             min_freq = -2.0*math.pi*19010/audio_rate;
133             
134             self.stereo_carrier_pll_recovery = gr.pll_refout_cc(alpha,beta,max_freq,min_freq);
135             #self.stereo_carrier_pll_recovery.squelch_enable(False) #pll_refout does not have squelch yet, so disabled for now 
136             
137
138             # set up mixer (multiplier) to get the L-R signal at baseband
139
140             self.stereo_basebander = gr.multiply_cc();
141
142             # pick off the real component of the basebanded L-R signal.  The imaginary SHOULD be zero
143
144             self.LmR_real = gr.complex_to_real();
145             self.Make_Left = gr.add_ff();
146             self.Make_Right = gr.sub_ff();
147             
148             self.stereo_dsbsc_filter = gr.fir_filter_fcc(audio_decimation, stereo_dsbsc_filter_coeffs)
149
150
151         if 1:
152
153             # send the real signal to complex filter to pick off the carrier and then to one side of a multiplier
154             fg.connect (self.fm_demod,self.stereo_carrier_filter,self.stereo_carrier_pll_recovery, (self.stereo_carrier_generator,0))
155             # send the already filtered carrier to the otherside of the carrier
156             fg.connect (self.stereo_carrier_pll_recovery, (self.stereo_carrier_generator,1))
157             # the resulting signal from this multiplier is the carrier with correct phase but at -38000 Hz.
158
159             # send the new carrier to one side of the mixer (multiplier)
160             fg.connect (self.stereo_carrier_generator, (self.stereo_basebander,0))
161             # send the demphasized audio to the DSBSC pick off filter,  the complex
162             # DSBSC signal at +38000 Hz is sent to the other side of the mixer/multiplier
163             fg.connect (self.fm_demod,self.stereo_dsbsc_filter, (self.stereo_basebander,1))
164             # the result is BASEBANDED DSBSC with phase zero!
165
166             # Pick off the real part since the imaginary is theoretically zero and then to one side of a summer
167             fg.connect (self.stereo_basebander, self.LmR_real, (self.Make_Left,0))
168             #take the same real part of the DSBSC baseband signal and send it to negative side of a subtracter
169             fg.connect (self.LmR_real,(self.Make_Right,1))
170
171             # Make rds carrier by taking the squared pilot tone and multiplying by pilot tone
172             fg.connect (self.stereo_basebander,(self.rds_carrier_generator,0))
173             fg.connect (self.stereo_carrier_pll_recovery,(self.rds_carrier_generator,1)) 
174             # take signal, filter off rds,  send into mixer 0 channel
175             fg.connect (self.fm_demod,self.rds_signal_filter,(self.rds_signal_generator,0))
176             # take rds_carrier_generator output and send into mixer 1 channel
177             fg.connect (self.rds_carrier_generator,(self.rds_signal_generator,1))
178             # send basebanded rds signal and send into "processor" which for now is a null sink
179             fg.connect (self.rds_signal_generator,self_rds_signal_processor)
180             
181
182         if 1:
183             # pick off the audio, L+R that is what we used to have and send it to the summer
184             fg.connect(self.fm_demod, self.audio_filter, (self.Make_Left, 1))
185             # take the picked off L+R audio and send it to the PLUS side of the subtractor
186             fg.connect(self.audio_filter,(self.Make_Right, 0))
187             # The result of  Make_Left  gets    (L+R) +  (L-R) and results in 2*L
188             # The result of Make_Right gets  (L+R) - (L-R) and results in 2*R
189
190
191             # kludge the signals into a stereo channel
192             kludge = gr.kludge_copy(gr.sizeof_float)
193             fg.connect(self.Make_Left , self.deemph_Left, (kludge, 0))
194             fg.connect(self.Make_Right, self.deemph_Right, (kludge, 1))
195
196            #send it to the audio system
197             gr.hier_block.__init__(self,
198                                    fg,
199                                    self.fm_demod,       # head of the pipeline
200                                    kludge)              # tail of the pipeline
201         else:
202             fg.connect (self.fm_demod, self.audio_filter)
203             gr.hier_block.__init__(self,
204                                    fg,
205                                    self.fm_demod,       # head of the pipeline
206                                    self.audio_filter)   # tail of the pipeline