Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / mp-sched / wfm_rcv_pll_to_wav.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007 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, gru, eng_notation, optfir
24 from gnuradio import audio
25 from gnuradio import blks2
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28 import sys
29 import math
30
31 class wfm_rx_block (gr.top_block):
32     def __init__(self):
33         gr.top_block.__init__(self)
34
35         usage = "usage: %prog [options] input-samples-320kS.dat output.wav"
36         parser=OptionParser(option_class=eng_option, usage=usage)
37         parser.add_option("-V", "--volume", type="eng_float", default=None,
38                           help="set volume (default is midpoint)")
39
40         (options, args) = parser.parse_args()
41         if len(args) != 2:
42             parser.print_help()
43             sys.exit(1)
44         
45         input_filename = args[0]
46         output_filename = args[1]
47
48         self.vol = 0
49         
50         # build graph
51         
52         self.src = gr.file_source(gr.sizeof_gr_complex, input_filename, False)
53
54         adc_rate = 64e6                             # 64 MS/s
55         usrp_decim = 200
56         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
57         chanfilt_decim = 1
58         demod_rate = usrp_rate / chanfilt_decim
59         audio_decimation = 10
60         audio_rate = demod_rate / audio_decimation  # 32 kHz
61
62
63         chan_filt_coeffs = optfir.low_pass (1,           # gain
64                                             usrp_rate,   # sampling rate
65                                             80e3,        # passband cutoff
66                                             115e3,       # stopband cutoff
67                                             0.1,         # passband ripple
68                                             60)          # stopband attenuation
69         #print len(chan_filt_coeffs)
70         chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
71
72
73         #self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
74         self.guts = blks2.wfm_rcv_pll (demod_rate, audio_decimation)
75
76         # FIXME rework {add,multiply}_const_* to handle multiple streams
77         self.volume_control_l = gr.multiply_const_ff(self.vol)
78         self.volume_control_r = gr.multiply_const_ff(self.vol)
79
80         # wave file as final sink
81         if 1:
82             sink = gr.wavfile_sink(output_filename, 2, int(audio_rate), 16)
83         else:
84             sink = audio.sink (int (audio_rate),
85                                options.audio_output,
86                                False)   # ok_to_block
87         
88         # now wire it all together
89         self.connect (self.src, chan_filt, self.guts)
90         self.connect ((self.guts, 0), self.volume_control_l, (sink, 0))
91         self.connect ((self.guts, 1), self.volume_control_r, (sink, 1))
92         try:
93           self.guts.stereo_carrier_pll_recovery.squelch_enable(True)
94         except:
95           pass
96           #print "FYI: This implementation of the stereo_carrier_pll_recovery has no squelch implementation yet"
97
98         if options.volume is None:
99             g = self.volume_range()
100             options.volume = float(g[0]+g[1])/2
101
102         # set initial values
103
104         self.set_vol(options.volume)
105         try:
106           self.guts.stereo_carrier_pll_recovery.set_lock_threshold(options.squelch)
107         except:
108           pass
109           #print "FYI: This implementation of the stereo_carrier_pll_recovery has no squelch implementation yet"
110
111
112     def set_vol (self, vol):
113         g = self.volume_range()
114         self.vol = max(g[0], min(g[1], vol))
115         self.volume_control_l.set_k(10**(self.vol/10))
116         self.volume_control_r.set_k(10**(self.vol/10))
117
118     def volume_range(self):
119         return (-20.0, 0.0, 0.5)
120         
121
122 if __name__ == '__main__':
123     tb = wfm_rx_block()
124     try:
125         tb.run()
126     except KeyboardInterrupt:
127         pass