9917847bb81d7b03f8844d8c4a7552a8610a851f
[debian/gnuradio] / gr-noaa / apps / usrp_rx_hrpt_nogui.py
1 #!/usr/bin/env python
2 ##################################################
3 # Gnuradio Python Flow Graph
4 # Title: USRP HRPT Receiver
5 # Generated: Sun Sep 27 10:13:25 2009
6 ##################################################
7
8 from gnuradio import eng_notation
9 from gnuradio import gr
10 from gnuradio import noaa
11 from gnuradio.eng_option import eng_option
12 from gnuradio.gr import firdes
13 from grc_gnuradio import usrp as grc_usrp
14 from optparse import OptionParser
15 import ConfigParser
16 import math
17
18 class usrp_rx_hrpt_nogui(gr.top_block):
19
20         def __init__(self):
21                 gr.top_block.__init__(self, "USRP HRPT Receiver")
22
23                 ##################################################
24                 # Variables
25                 ##################################################
26                 self.config_filename = config_filename = 'usrp_rx_hrpt.cfg'
27                 self._decim_config = ConfigParser.ConfigParser()
28                 self._decim_config.read(config_filename)
29                 try: decim = self._decim_config.getfloat('usrp', 'decim')
30                 except: decim = 16
31                 self.decim = decim
32                 self.sym_rate = sym_rate = 600*1109
33                 self.sample_rate = sample_rate = 64e6/decim
34                 self.sps = sps = sample_rate/sym_rate
35                 self.hs = hs = int(sps/2.0)
36                 self._sync_alpha_config = ConfigParser.ConfigParser()
37                 self._sync_alpha_config.read(config_filename)
38                 try: sync_alpha = self._sync_alpha_config.getfloat('demod', 'sync_alpha')
39                 except: sync_alpha = 0.05
40                 self.sync_alpha = sync_alpha
41                 self._side_config = ConfigParser.ConfigParser()
42                 self._side_config.read(config_filename)
43                 try: side = self._side_config.get('usrp', 'side')
44                 except: side = 'A'
45                 self.side = side
46                 self._pll_alpha_config = ConfigParser.ConfigParser()
47                 self._pll_alpha_config.read(config_filename)
48                 try: pll_alpha = self._pll_alpha_config.getfloat('demod', 'pll_alpha')
49                 except: pll_alpha = 0.05
50                 self.pll_alpha = pll_alpha
51                 self._output_filename_config = ConfigParser.ConfigParser()
52                 self._output_filename_config.read(config_filename)
53                 try: output_filename = self._output_filename_config.get('output', 'filename')
54                 except: output_filename = 'frames.dat'
55                 self.output_filename = output_filename
56                 self.mf_taps = mf_taps = [-0.5/hs,]*hs+[0.5/hs,]*hs
57                 self.max_sync_offset = max_sync_offset = 0.01
58                 self.max_carrier_offset = max_carrier_offset = 2*math.pi*100e3/sample_rate
59                 self._gain_config = ConfigParser.ConfigParser()
60                 self._gain_config.read(config_filename)
61                 try: gain = self._gain_config.getfloat('usrp', 'gain')
62                 except: gain = 35
63                 self.gain = gain
64                 self._freq_config = ConfigParser.ConfigParser()
65                 self._freq_config.read(config_filename)
66                 try: freq = self._freq_config.getfloat('usrp', 'freq')
67                 except: freq = 1698e6
68                 self.freq = freq
69
70                 ##################################################
71                 # Blocks
72                 ##################################################
73                 self.agc = gr.agc_cc(1e-6, 1.0, 1.0, 1.0)
74                 self.decoder = noaa.hrpt_decoder()
75                 self.deframer = noaa.hrpt_deframer()
76                 self.frame_sink = gr.file_sink(gr.sizeof_short*1, output_filename)
77                 self.gr_fir_filter_xxx_0 = gr.fir_filter_ccc(1, (mf_taps))
78                 self.pll = noaa.hrpt_pll_cf(pll_alpha, pll_alpha**2/4.0, max_carrier_offset)
79                 self.sync = noaa.hrpt_sync_fb(sync_alpha, sync_alpha**2/4.0, sps, max_sync_offset)
80                 self.usrp_source = grc_usrp.simple_source_c(which=0, side=side, rx_ant="RXA")
81                 self.usrp_source.set_decim_rate(decim)
82                 self.usrp_source.set_frequency(freq, verbose=True)
83                 self.usrp_source.set_gain(gain)
84
85                 ##################################################
86                 # Connections
87                 ##################################################
88                 self.connect((self.usrp_source, 0), (self.agc, 0))
89                 self.connect((self.deframer, 0), (self.decoder, 0))
90                 self.connect((self.pll, 0), (self.sync, 0))
91                 self.connect((self.sync, 0), (self.deframer, 0))
92                 self.connect((self.deframer, 0), (self.frame_sink, 0))
93                 self.connect((self.agc, 0), (self.gr_fir_filter_xxx_0, 0))
94                 self.connect((self.gr_fir_filter_xxx_0, 0), (self.pll, 0))
95
96         def set_config_filename(self, config_filename):
97                 self.config_filename = config_filename
98                 self._output_filename_config = ConfigParser.ConfigParser()
99                 self._output_filename_config.read(self.config_filename)
100                 if not self._output_filename_config.has_section('output'):
101                         self._output_filename_config.add_section('output')
102                 self._output_filename_config.set('output', 'filename', str(self.output_filename))
103                 self._output_filename_config.write(open(self.config_filename, 'w'))
104                 self._sync_alpha_config = ConfigParser.ConfigParser()
105                 self._sync_alpha_config.read(self.config_filename)
106                 if not self._sync_alpha_config.has_section('demod'):
107                         self._sync_alpha_config.add_section('demod')
108                 self._sync_alpha_config.set('demod', 'sync_alpha', str(self.sync_alpha))
109                 self._sync_alpha_config.write(open(self.config_filename, 'w'))
110                 self._pll_alpha_config = ConfigParser.ConfigParser()
111                 self._pll_alpha_config.read(self.config_filename)
112                 if not self._pll_alpha_config.has_section('demod'):
113                         self._pll_alpha_config.add_section('demod')
114                 self._pll_alpha_config.set('demod', 'pll_alpha', str(self.pll_alpha))
115                 self._pll_alpha_config.write(open(self.config_filename, 'w'))
116                 self._gain_config = ConfigParser.ConfigParser()
117                 self._gain_config.read(self.config_filename)
118                 if not self._gain_config.has_section('usrp'):
119                         self._gain_config.add_section('usrp')
120                 self._gain_config.set('usrp', 'gain', str(self.gain))
121                 self._gain_config.write(open(self.config_filename, 'w'))
122                 self._freq_config = ConfigParser.ConfigParser()
123                 self._freq_config.read(self.config_filename)
124                 if not self._freq_config.has_section('usrp'):
125                         self._freq_config.add_section('usrp')
126                 self._freq_config.set('usrp', 'freq', str(self.freq))
127                 self._freq_config.write(open(self.config_filename, 'w'))
128                 self._decim_config = ConfigParser.ConfigParser()
129                 self._decim_config.read(self.config_filename)
130                 if not self._decim_config.has_section('usrp'):
131                         self._decim_config.add_section('usrp')
132                 self._decim_config.set('usrp', 'decim', str(self.decim))
133                 self._decim_config.write(open(self.config_filename, 'w'))
134                 self._side_config = ConfigParser.ConfigParser()
135                 self._side_config.read(self.config_filename)
136                 if not self._side_config.has_section('usrp'):
137                         self._side_config.add_section('usrp')
138                 self._side_config.set('usrp', 'side', str(self.side))
139                 self._side_config.write(open(self.config_filename, 'w'))
140
141         def set_decim(self, decim):
142                 self.decim = decim
143                 self.set_sample_rate(64e6/self.decim)
144                 self._decim_config = ConfigParser.ConfigParser()
145                 self._decim_config.read(self.config_filename)
146                 if not self._decim_config.has_section('usrp'):
147                         self._decim_config.add_section('usrp')
148                 self._decim_config.set('usrp', 'decim', str(self.decim))
149                 self._decim_config.write(open(self.config_filename, 'w'))
150                 self.usrp_source.set_decim_rate(self.decim)
151
152         def set_sym_rate(self, sym_rate):
153                 self.sym_rate = sym_rate
154                 self.set_sps(self.sample_rate/self.sym_rate)
155
156         def set_sample_rate(self, sample_rate):
157                 self.sample_rate = sample_rate
158                 self.set_max_carrier_offset(2*math.pi*100e3/self.sample_rate)
159                 self.set_sps(self.sample_rate/self.sym_rate)
160
161         def set_sps(self, sps):
162                 self.sps = sps
163                 self.set_hs(int(self.sps/2.0))
164
165         def set_hs(self, hs):
166                 self.hs = hs
167                 self.set_mf_taps([-0.5/self.hs,]*self.hs+[0.5/self.hs,]*self.hs)
168
169         def set_sync_alpha(self, sync_alpha):
170                 self.sync_alpha = sync_alpha
171                 self._sync_alpha_config = ConfigParser.ConfigParser()
172                 self._sync_alpha_config.read(self.config_filename)
173                 if not self._sync_alpha_config.has_section('demod'):
174                         self._sync_alpha_config.add_section('demod')
175                 self._sync_alpha_config.set('demod', 'sync_alpha', str(self.sync_alpha))
176                 self._sync_alpha_config.write(open(self.config_filename, 'w'))
177                 self.sync.set_alpha(self.sync_alpha)
178                 self.sync.set_beta(self.sync_alpha**2/4.0)
179
180         def set_side(self, side):
181                 self.side = side
182                 self._side_config = ConfigParser.ConfigParser()
183                 self._side_config.read(self.config_filename)
184                 if not self._side_config.has_section('usrp'):
185                         self._side_config.add_section('usrp')
186                 self._side_config.set('usrp', 'side', str(self.side))
187                 self._side_config.write(open(self.config_filename, 'w'))
188
189         def set_pll_alpha(self, pll_alpha):
190                 self.pll_alpha = pll_alpha
191                 self._pll_alpha_config = ConfigParser.ConfigParser()
192                 self._pll_alpha_config.read(self.config_filename)
193                 if not self._pll_alpha_config.has_section('demod'):
194                         self._pll_alpha_config.add_section('demod')
195                 self._pll_alpha_config.set('demod', 'pll_alpha', str(self.pll_alpha))
196                 self._pll_alpha_config.write(open(self.config_filename, 'w'))
197                 self.pll.set_alpha(self.pll_alpha)
198                 self.pll.set_beta(self.pll_alpha**2/4.0)
199
200         def set_output_filename(self, output_filename):
201                 self.output_filename = output_filename
202                 self._output_filename_config = ConfigParser.ConfigParser()
203                 self._output_filename_config.read(self.config_filename)
204                 if not self._output_filename_config.has_section('output'):
205                         self._output_filename_config.add_section('output')
206                 self._output_filename_config.set('output', 'filename', str(self.output_filename))
207                 self._output_filename_config.write(open(self.config_filename, 'w'))
208
209         def set_mf_taps(self, mf_taps):
210                 self.mf_taps = mf_taps
211                 self.gr_fir_filter_xxx_0.set_taps((self.mf_taps))
212
213         def set_max_sync_offset(self, max_sync_offset):
214                 self.max_sync_offset = max_sync_offset
215                 self.sync.set_max_offset(self.max_sync_offset)
216
217         def set_max_carrier_offset(self, max_carrier_offset):
218                 self.max_carrier_offset = max_carrier_offset
219                 self.pll.set_max_offset(self.max_carrier_offset)
220
221         def set_gain(self, gain):
222                 self.gain = gain
223                 self._gain_config = ConfigParser.ConfigParser()
224                 self._gain_config.read(self.config_filename)
225                 if not self._gain_config.has_section('usrp'):
226                         self._gain_config.add_section('usrp')
227                 self._gain_config.set('usrp', 'gain', str(self.gain))
228                 self._gain_config.write(open(self.config_filename, 'w'))
229                 self.usrp_source.set_gain(self.gain)
230
231         def set_freq(self, freq):
232                 self.freq = freq
233                 self._freq_config = ConfigParser.ConfigParser()
234                 self._freq_config.read(self.config_filename)
235                 if not self._freq_config.has_section('usrp'):
236                         self._freq_config.add_section('usrp')
237                 self._freq_config.set('usrp', 'freq', str(self.freq))
238                 self._freq_config.write(open(self.config_filename, 'w'))
239                 self.usrp_source.set_frequency(self.freq)
240
241 if __name__ == '__main__':
242         parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
243         (options, args) = parser.parse_args()
244         if gr.enable_realtime_scheduling() != gr.RT_OK:
245                 print "Error: failed to enable realtime scheduling."
246         tb = usrp_rx_hrpt_nogui()
247         tb.start()
248         raw_input('Press Enter to quit: ')
249         tb.stop()
250