Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_agc.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 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 2, 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, gr_unittest
24 import math
25
26 test_output = False
27
28 class test_sig_source (gr_unittest.TestCase):
29
30     def setUp (self):
31         self.fg = gr.flow_graph ()
32
33     def tearDown (self):
34         self.fg = None
35
36     
37     def test_001(self):
38         ''' Test the complex AGC loop (single rate input) '''
39         fg = self.fg
40
41         expected_result = (
42             (100.000244140625+7.2191943445432116e-07j),
43             (72.892257690429688+52.959323883056641j),
44             (25.089065551757812+77.216217041015625j),
45             (-22.611061096191406+69.589706420898438j),
46             (-53.357715606689453+38.766635894775391j),
47             (-59.458671569824219+3.4792964243024471e-07j),
48             (-43.373462677001953-31.512666702270508j),
49             (-14.94139289855957-45.984889984130859j),
50             (13.478158950805664-41.48150634765625j),
51             (31.838506698608398-23.132022857666016j),
52             (35.519271850585938-3.1176801940091536e-07j),
53             (25.942903518676758+18.848621368408203j),
54             (8.9492912292480469+27.5430908203125j),
55             (-8.0852642059326172+24.883890151977539j),
56             (-19.131628036499023+13.899936676025391j),
57             (-21.383295059204102+3.1281737733479531e-07j),
58             (-15.650330543518066-11.370632171630859j),
59             (-5.4110145568847656-16.65339469909668j),
60             (4.9008159637451172-15.083160400390625j),
61             (11.628337860107422-8.4484796524047852j),
62             (13.036135673522949-2.288476110834381e-07j),
63             (9.5726661682128906+6.954948902130127j),
64             (3.3216962814331055+10.223132133483887j),
65             (-3.0204284191131592+9.2959251403808594j),
66             (-7.1977195739746094+5.2294478416442871j),
67             (-8.1072216033935547+1.8976157889483147e-07j),
68             (-5.9838657379150391-4.3475332260131836j),
69             (-2.0879747867584229-6.4261269569396973j),
70             (1.9100792407989502-5.8786196708679199j),
71             (4.5814824104309082-3.3286411762237549j),
72             (5.1967458724975586-1.3684227440080576e-07j),
73             (3.8647139072418213+2.8078789710998535j),
74             (1.3594740629196167+4.1840314865112305j),
75             (-1.2544282674789429+3.8607344627380371j),
76             (-3.0366206169128418+2.2062335014343262j),
77             (-3.4781389236450195+1.1194014604143376e-07j),
78             (-2.6133756637573242-1.8987287282943726j),
79             (-0.9293016791343689-2.8600969314575195j),
80             (0.86727333068847656-2.6691930294036865j),
81             (2.1243946552276611-1.5434627532958984j),
82             (2.4633183479309082-8.6486437567145913e-08j),
83             (1.8744727373123169+1.3618841171264648j),
84             (0.67528903484344482+2.0783262252807617j),
85             (-0.63866174221038818+1.965599536895752j),
86             (-1.5857341289520264+1.152103066444397j),
87             (-1.8640764951705933+7.6355092915036948e-08j),
88             (-1.4381576776504517-1.0448826551437378j),
89             (-0.52529704570770264-1.6166983842849731j),
90             (0.50366902351379395-1.5501341819763184j),
91             (1.26766037940979-0.92100900411605835j))
92
93         sampling_freq = 100
94         src1 = gr.sig_source_c (sampling_freq, gr.GR_SIN_WAVE,
95                                 sampling_freq * 0.10, 100.0)
96         dst1 = gr.vector_sink_c ()
97         head = gr.head (gr.sizeof_gr_complex, int (5*sampling_freq * 0.10))
98
99         agc = gr.agc_cc(1e-3, 1, 1, 1000)
100         
101         fg.connect (src1, head)
102         fg.connect (head, agc)
103         fg.connect (agc, dst1)
104
105         if test_output == True:
106             fg.connect (agc, gr.file_sink(gr.sizeof_gr_complex, "test_agc_cc.dat"))
107
108         fg.run ()
109         dst_data = dst1.data ()
110         self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 4)
111
112     def test_002(self):
113         ''' Test the floating point AGC loop (single rate input) '''
114         fg = self.fg
115
116         expected_result = (
117             7.2191943445432116e-07,
118             58.837181091308594,
119             89.700050354003906,
120             81.264183044433594,
121             45.506141662597656,
122             4.269894304798072e-07,
123             -42.948936462402344,
124             -65.50335693359375,
125             -59.368724822998047,
126             -33.261005401611328,
127             -4.683740257860336e-07,
128             31.423542022705078,
129             47.950984954833984,
130             43.485683441162109,
131             24.378345489501953,
132             5.7254135299444897e-07,
133             -23.062990188598633,
134             -35.218441009521484,
135             -31.964075088500977,
136             -17.934831619262695,
137             -5.0591745548445033e-07,
138             16.998210906982422,
139             25.982204437255859,
140             23.606258392333984,
141             13.260685920715332,
142             4.9936483037527069e-07,
143             -12.59880542755127,
144             -19.28221321105957,
145             -17.54347038269043,
146             -9.8700437545776367,
147             -4.188150626305287e-07,
148             9.4074573516845703,
149             14.422011375427246,
150             13.145503044128418,
151             7.41046142578125,
152             3.8512698097292741e-07,
153             -7.0924453735351562,
154             -10.896408081054688,
155             -9.9552040100097656,
156             -5.6262712478637695,
157             -3.1982864356905338e-07,
158             5.4131259918212891,
159             8.3389215469360352,
160             7.6409502029418945,
161             4.3320145606994629,
162             2.882407841298118e-07,
163             -4.194943904876709,
164             -6.4837145805358887,
165             -5.9621825218200684,
166             -3.3931560516357422)
167
168         sampling_freq = 100
169         src1 = gr.sig_source_f (sampling_freq, gr.GR_SIN_WAVE,
170                                 sampling_freq * 0.10, 100.0)
171         dst1 = gr.vector_sink_f ()
172         head = gr.head (gr.sizeof_float, int (5*sampling_freq * 0.10))
173
174         agc = gr.agc_ff(1e-3, 1, 1, 1000)
175         
176         fg.connect (src1, head)
177         fg.connect (head, agc)
178         fg.connect (agc, dst1)
179
180         if test_output == True:
181             fg.connect (agc, gr.file_sink(gr.sizeof_float, "test_agc_ff.dat"))
182
183         fg.run ()
184         dst_data = dst1.data ()
185         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 4)
186
187     def test_003(self):
188         ''' Test the complex AGC loop (attack and decay rate inputs) '''
189         fg = self.fg
190
191         expected_result = \
192                         ((100.000244140625+7.2191943445432116e-07j),
193                          (0.80881959199905396+0.58764183521270752j),
194                          (0.30894950032234192+0.95084899663925171j),
195                          (-0.30895623564720154+0.95086973905563354j),
196                          (-0.80887287855148315+0.58768033981323242j),
197                          (-0.99984413385391235+5.850709250410091e-09j),
198                          (-0.80889981985092163-0.58770018815994263j),
199                          (-0.30897706747055054-0.95093393325805664j),
200                          (0.30898112058639526-0.95094609260559082j),
201                          (0.80893135070800781-0.58772283792495728j),
202                          (0.99990922212600708-8.7766354184282136e-09j),
203                          (0.80894720554351807+0.58773452043533325j),
204                          (0.30899339914321899+0.95098406076431274j),
205                          (-0.30899572372436523+0.95099133253097534j),
206                          (-0.80896598100662231+0.58774799108505249j),
207                          (-0.99994778633117676+1.4628290578855285e-08j),
208                          (-0.80897533893585205-0.58775502443313599j),
209                          (-0.30900305509567261-0.95101380348205566j),
210                          (0.30900448560714722-0.95101797580718994j),
211                          (0.80898630619049072-0.58776277303695679j),
212                          (0.99997037649154663-1.7554345532744264e-08j),
213                          (0.80899184942245483+0.58776694536209106j),
214                          (0.30900871753692627+0.95103120803833008j),
215                          (-0.30900952219963074+0.95103377103805542j),
216                          (-0.8089984655380249+0.58777159452438354j),
217                          (-0.99998390674591064+2.3406109050938539e-08j),
218                          (-0.809001624584198-0.58777409791946411j),
219                          (-0.30901208519935608-0.95104163885116577j),
220                          (0.30901262164115906-0.95104306936264038j),
221                          (0.80900543928146362-0.587776780128479j),
222                          (0.99999171495437622-2.6332081404234486e-08j),
223                          (0.80900734663009644+0.58777821063995361j),
224                          (0.30901408195495605+0.95104765892028809j),
225                          (-0.30901429057121277+0.95104855298995972j),
226                          (-0.80900967121124268+0.58777981996536255j),
227                          (-0.99999648332595825+3.2183805842578295e-08j),
228                          (-0.80901080369949341-0.58778077363967896j),
229                          (-0.30901527404785156-0.95105135440826416j),
230                          (0.30901545286178589-0.95105189085006714j),
231                          (0.80901217460632324-0.58778166770935059j),
232                          (0.99999916553497314-3.5109700036173308e-08j),
233                          (0.809012770652771+0.58778214454650879j),
234                          (0.30901595950126648+0.9510534405708313j),
235                          (-0.30901598930358887+0.95105385780334473j),
236                          (-0.80901366472244263+0.58778274059295654j),
237                          (-1.0000008344650269+4.0961388947380328e-08j),
238                          (-0.8090139627456665-0.58778303861618042j),
239                          (-0.30901634693145752-0.95105475187301636j),
240                          (0.30901640653610229-0.95105493068695068j),
241                          (0.80901449918746948-0.5877833366394043j))
242
243         sampling_freq = 100
244         src1 = gr.sig_source_c (sampling_freq, gr.GR_SIN_WAVE,
245                                 sampling_freq * 0.10, 100)
246         dst1 = gr.vector_sink_c ()
247         head = gr.head (gr.sizeof_gr_complex, int (5*sampling_freq * 0.10))
248
249         agc = gr.agc2_cc(1e-2, 1e-3, 1, 1, 1000)
250         
251         fg.connect (src1, head)
252         fg.connect (head, agc)
253         fg.connect (agc, dst1)
254
255         if test_output == True:
256             fg.connect (agc, gr.file_sink(gr.sizeof_gr_complex, "test_agc2_cc.dat"))
257
258         fg.run ()
259         dst_data = dst1.data ()
260         self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 4)
261
262     def test_004(self):
263         ''' Test the floating point AGC loop (attack and decay rate inputs) '''
264         fg = self.fg
265
266         expected_result = \
267             (7.2191943445432116e-07,
268              58.837181091308594,
269              40.194305419921875,
270              2.9183335304260254,
271              0.67606079578399658,
272              8.6260438791896377e-09,
273              -1.4542514085769653,
274              -1.9210131168365479,
275              -1.0450780391693115,
276              -0.61939650774002075,
277              -1.2590258613442984e-08,
278              1.4308931827545166,
279              1.9054338932037354,
280              1.0443156957626343,
281              0.61937344074249268,
282              2.0983527804219193e-08,
283              -1.4308838844299316,
284              -1.9054274559020996,
285              -1.0443152189254761,
286              -0.61937344074249268,
287              -2.5180233009791664e-08,
288              1.4308837652206421,
289              1.9054274559020996,
290              1.0443154573440552,
291              0.61937344074249268,
292              3.3573645197293445e-08,
293              -1.4308838844299316,
294              -1.9054274559020996,
295              -1.0443152189254761,
296              -0.61937350034713745,
297              -3.7770352179222755e-08,
298              1.4308837652206421,
299              1.9054274559020996,
300              1.0443154573440552,
301              0.61937350034713745,
302              4.6163762590367696e-08,
303              -1.4308838844299316,
304              -1.9054274559020996,
305              -1.0443153381347656,
306              -0.61937344074249268,
307              -5.0360466019583328e-08,
308              1.4308837652206421,
309              1.9054274559020996,
310              1.0443155765533447,
311              0.61937344074249268,
312              5.8753879983441948e-08,
313              -1.4308837652206421,
314              -1.9054274559020996,
315              -1.0443153381347656,
316              -0.61937344074249268)
317
318         sampling_freq = 100
319         src1 = gr.sig_source_f (sampling_freq, gr.GR_SIN_WAVE,
320                                 sampling_freq * 0.10, 100)
321         dst1 = gr.vector_sink_f ()
322         head = gr.head (gr.sizeof_float, int (5*sampling_freq * 0.10))
323
324         agc = gr.agc2_ff(1e-2, 1e-3, 1, 1, 1000)
325         
326         fg.connect (src1, head)
327         fg.connect (head, agc)
328         fg.connect (agc, dst1)
329
330         if test_output == True:
331             fg.connect (agc, gr.file_sink(gr.sizeof_float, "test_agc2_ff.dat"))
332
333         fg.run ()
334         dst_data = dst1.data ()
335         self.assertFloatTuplesAlmostEqual (expected_result, dst_data, 4)
336
337
338     def test_005(self):
339         ''' Test the complex AGC loop (attack and decay rate inputs) '''
340         fg = self.fg
341
342         expected_result = \
343                         ((100.000244140625+7.2191943445432116e-07j),
344                          (0.80881959199905396+0.58764183521270752j),
345                          (0.30894950032234192+0.95084899663925171j),
346                          (-0.30895623564720154+0.95086973905563354j),
347                          (-0.80887287855148315+0.58768033981323242j),
348                          (-0.99984413385391235+5.850709250410091e-09j),
349                          (-0.80889981985092163-0.58770018815994263j),
350                          (-0.30897706747055054-0.95093393325805664j),
351                          (0.30898112058639526-0.95094609260559082j),
352                          (0.80893135070800781-0.58772283792495728j),
353                          (0.99990922212600708-8.7766354184282136e-09j),
354                          (0.80894720554351807+0.58773452043533325j),
355                          (0.30899339914321899+0.95098406076431274j),
356                          (-0.30899572372436523+0.95099133253097534j),
357                          (-0.80896598100662231+0.58774799108505249j),
358                          (-0.99994778633117676+1.4628290578855285e-08j),
359                          (-0.80897533893585205-0.58775502443313599j),
360                          (-0.30900305509567261-0.95101380348205566j),
361                          (0.30900448560714722-0.95101797580718994j),
362                          (0.80898630619049072-0.58776277303695679j),
363                          (0.99997037649154663-1.7554345532744264e-08j),
364                          (0.80899184942245483+0.58776694536209106j),
365                          (0.30900871753692627+0.95103120803833008j),
366                          (-0.30900952219963074+0.95103377103805542j),
367                          (-0.8089984655380249+0.58777159452438354j),
368                          (-0.99998390674591064+2.3406109050938539e-08j),
369                          (-0.809001624584198-0.58777409791946411j),
370                          (-0.30901208519935608-0.95104163885116577j),
371                          (0.30901262164115906-0.95104306936264038j),
372                          (0.80900543928146362-0.587776780128479j),
373                          (0.99999171495437622-2.6332081404234486e-08j),
374                          (0.80900734663009644+0.58777821063995361j),
375                          (0.30901408195495605+0.95104765892028809j),
376                          (-0.30901429057121277+0.95104855298995972j),
377                          (-0.80900967121124268+0.58777981996536255j),
378                          (-0.99999648332595825+3.2183805842578295e-08j),
379                          (-0.80901080369949341-0.58778077363967896j),
380                          (-0.30901527404785156-0.95105135440826416j),
381                          (0.30901545286178589-0.95105189085006714j),
382                          (0.80901217460632324-0.58778166770935059j),
383                          (0.99999916553497314-3.5109700036173308e-08j),
384                          (0.809012770652771+0.58778214454650879j),
385                          (0.30901595950126648+0.9510534405708313j),
386                          (-0.30901598930358887+0.95105385780334473j),
387                          (-0.80901366472244263+0.58778274059295654j),
388                          (-1.0000008344650269+4.0961388947380328e-08j),
389                          (-0.8090139627456665-0.58778303861618042j),
390                          (-0.30901634693145752-0.95105475187301636j),
391                          (0.30901640653610229-0.95105493068695068j),
392                          (0.80901449918746948-0.5877833366394043j))
393
394         sampling_freq = 100
395         src1 = gr.sig_source_c (sampling_freq, gr.GR_SIN_WAVE,
396                                 sampling_freq * 0.10, 100)
397         dst1 = gr.vector_sink_c ()
398         head = gr.head (gr.sizeof_gr_complex, int (5*sampling_freq * 0.10))
399
400         agc = gr.agc2_cc(1e-2, 1e-3, 1, 1, 1000)
401         
402         fg.connect (src1, head)
403         fg.connect (head, agc)
404         fg.connect (agc, dst1)
405
406         if test_output == True:
407             fg.connect (agc, gr.file_sink(gr.sizeof_gr_complex, "test_agc2_cc.dat"))
408
409         fg.run ()
410         dst_data = dst1.data ()
411         self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 4)
412
413
414     def test_100(self):        # FIXME needs work
415         ''' Test complex feedforward agc with constant input '''
416         input_data = 16*(0.0,) + 64*(1.0,) + 64*(0.0,)
417         expected_result = ()
418
419         src = gr.vector_source_c(input_data)
420         agc = gr.feedforward_agc_cc(16, 2.0)
421         dst = gr.vector_sink_c ()
422         self.fg.connect (src, agc, dst)
423
424         if test_output == True:
425             self.fg.connect (agc, gr.file_sink(gr.sizeof_gr_complex, "test_feedforward_cc.dat"))
426
427         self.fg.run ()
428         dst_data = dst.data ()
429         #self.assertComplexTuplesAlmostEqual (expected_result, dst_data, 4)
430
431
432 if __name__ == '__main__':
433     gr_unittest.main ()