Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_fft.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 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 along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21
22 from gnuradio import gr, gr_unittest
23 import sys
24 import random
25
26 primes = (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,
27           59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,
28           137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,
29           227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311)
30
31
32 class test_fft_filter(gr_unittest.TestCase):
33
34     def setUp(self):
35         pass
36
37     def tearDown(self):
38         pass
39
40     def assert_fft_ok2(self, expected_result, result_data):
41         expected_result = expected_result[:len(result_data)]
42         self.assertComplexTuplesAlmostEqual2 (expected_result, result_data,
43                                               abs_eps=1e-9, rel_eps=4e-4)
44
45     def assert_fft_float_ok2(self, expected_result, result_data, abs_eps=1e-9, rel_eps=4e-4):
46         expected_result = expected_result[:len(result_data)]
47         self.assertFloatTuplesAlmostEqual2 (expected_result, result_data,
48                                             abs_eps, rel_eps)
49
50     def test_001(self):
51         tb = gr.top_block()
52         fft_size = 32
53         src_data = tuple([complex(primes[2*i], primes[2*i+1]) for i in range(fft_size)])
54
55         expected_result = ((4377+4516j),
56                            (-1706.1268310546875+1638.4256591796875j),
57                            (-915.2083740234375+660.69427490234375j),
58                            (-660.370361328125+381.59600830078125j),
59                            (-499.96044921875+238.41630554199219j),
60                            (-462.26748657226562+152.88948059082031j),
61                            (-377.98440551757812+77.5928955078125j),
62                            (-346.85821533203125+47.152004241943359j),
63                            (-295+20j),
64                            (-286.33609008789062-22.257017135620117j),
65                            (-271.52999877929688-33.081821441650391j),
66                            (-224.6358642578125-67.019538879394531j),
67                            (-244.24473571777344-91.524826049804688j),
68                            (-203.09068298339844-108.54627227783203j),
69                            (-198.45195007324219-115.90768432617188j),
70                            (-182.97744750976562-128.12318420410156j),
71                            (-167-180j),
72                            (-130.33688354492188-173.83778381347656j),
73                            (-141.19784545898438-190.28807067871094j),
74                            (-111.09677124023438-214.48896789550781j),
75                            (-70.039543151855469-242.41630554199219j),
76                            (-68.960540771484375-228.30015563964844j),
77                            (-53.049201965332031-291.47097778320312j),
78                            (-28.695289611816406-317.64553833007812j),
79                            (57-300j),
80                            (45.301143646240234-335.69509887695312j),
81                            (91.936195373535156-373.32437133789062j),
82                            (172.09465026855469-439.275146484375j),
83                            (242.24473571777344-504.47515869140625j),
84                            (387.81732177734375-666.6788330078125j),
85                            (689.48553466796875-918.2142333984375j),
86                            (1646.539306640625-1694.1956787109375j))
87
88         src = gr.vector_source_c(src_data)
89         s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
90         fft = gr.fft_vcc(fft_size, True, [], False)
91         v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_size)
92         dst = gr.vector_sink_c()
93         tb.connect(src, s2v, fft, v2s, dst)
94         tb.run()
95         result_data = dst.data()
96         #print 'expected:', expected_result
97         #print 'results: ', result_data
98         #self.assertComplexTuplesAlmostEqual (expected_result, result_data, 5)
99         self.assert_fft_ok2(expected_result, result_data)
100
101     def test_002(self):
102         tb = gr.top_block()
103         fft_size = 32
104
105         tmp_data = ((4377+4516j),
106                     (-1706.1268310546875+1638.4256591796875j),
107                     (-915.2083740234375+660.69427490234375j),
108                     (-660.370361328125+381.59600830078125j),
109                     (-499.96044921875+238.41630554199219j),
110                     (-462.26748657226562+152.88948059082031j),
111                     (-377.98440551757812+77.5928955078125j),
112                     (-346.85821533203125+47.152004241943359j),
113                     (-295+20j),
114                     (-286.33609008789062-22.257017135620117j),
115                     (-271.52999877929688-33.081821441650391j),
116                     (-224.6358642578125-67.019538879394531j),
117                     (-244.24473571777344-91.524826049804688j),
118                     (-203.09068298339844-108.54627227783203j),
119                     (-198.45195007324219-115.90768432617188j),
120                     (-182.97744750976562-128.12318420410156j),
121                     (-167-180j),
122                     (-130.33688354492188-173.83778381347656j),
123                     (-141.19784545898438-190.28807067871094j),
124                     (-111.09677124023438-214.48896789550781j),
125                     (-70.039543151855469-242.41630554199219j),
126                     (-68.960540771484375-228.30015563964844j),
127                     (-53.049201965332031-291.47097778320312j),
128                     (-28.695289611816406-317.64553833007812j),
129                     (57-300j),
130                     (45.301143646240234-335.69509887695312j),
131                     (91.936195373535156-373.32437133789062j),
132                     (172.09465026855469-439.275146484375j),
133                     (242.24473571777344-504.47515869140625j),
134                     (387.81732177734375-666.6788330078125j),
135                     (689.48553466796875-918.2142333984375j),
136                     (1646.539306640625-1694.1956787109375j))
137
138         src_data = tuple([x/fft_size for x in tmp_data])
139
140         expected_result = tuple([complex(primes[2*i], primes[2*i+1]) for i in range(fft_size)])
141
142         src = gr.vector_source_c(src_data)
143         s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size)
144         fft = gr.fft_vcc(fft_size, False, [], False)
145         v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_size)
146         dst = gr.vector_sink_c()
147         tb.connect(src, s2v, fft, v2s, dst)
148         tb.run()
149         result_data = dst.data()
150         #print 'expected:', expected_result
151         #print 'results: ', result_data
152         #self.assertComplexTuplesAlmostEqual (expected_result, result_data, 5)
153         self.assert_fft_ok2(expected_result, result_data)
154
155
156 if __name__ == '__main__':
157     gr_unittest.main ()
158