root / gr-fft / python / qa_fft.py @ d7905455
History | View | Annotate | Download (9.8 kB)
| 1 | #!/usr/bin/env python
|
|---|---|
| 2 | #
|
| 3 | # Copyright 2008,2010,2012 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 fft_swig as fft |
| 24 | import sys |
| 25 | import random |
| 26 | |
| 27 | primes = (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53, |
| 28 | 59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131, |
| 29 | 137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223, |
| 30 | 227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311) |
| 31 | |
| 32 | |
| 33 | class test_fft(gr_unittest.TestCase): |
| 34 | |
| 35 | def setUp(self): |
| 36 | pass
|
| 37 | |
| 38 | def tearDown(self): |
| 39 | pass
|
| 40 | |
| 41 | def assert_fft_ok2(self, expected_result, result_data): |
| 42 | expected_result = expected_result[:len(result_data)]
|
| 43 | self.assertComplexTuplesAlmostEqual2 (expected_result, result_data,
|
| 44 | abs_eps=1e-9, rel_eps=4e-4) |
| 45 | |
| 46 | def assert_fft_float_ok2(self, expected_result, result_data, abs_eps=1e-9, rel_eps=4e-4): |
| 47 | expected_result = expected_result[:len(result_data)]
|
| 48 | self.assertFloatTuplesAlmostEqual2 (expected_result, result_data,
|
| 49 | abs_eps, rel_eps) |
| 50 | |
| 51 | def test_001(self): |
| 52 | tb = gr.top_block() |
| 53 | fft_size = 32
|
| 54 | src_data = tuple([complex(primes[2*i], primes[2*i+1]) for i in range(fft_size)]) |
| 55 | |
| 56 | expected_result = ((4377+4516j), |
| 57 | (-1706.1268310546875+1638.4256591796875j), |
| 58 | (-915.2083740234375+660.69427490234375j), |
| 59 | (-660.370361328125+381.59600830078125j), |
| 60 | (-499.96044921875+238.41630554199219j), |
| 61 | (-462.26748657226562+152.88948059082031j), |
| 62 | (-377.98440551757812+77.5928955078125j), |
| 63 | (-346.85821533203125+47.152004241943359j), |
| 64 | (-295+20j), |
| 65 | (-286.33609008789062-22.257017135620117j), |
| 66 | (-271.52999877929688-33.081821441650391j), |
| 67 | (-224.6358642578125-67.019538879394531j), |
| 68 | (-244.24473571777344-91.524826049804688j), |
| 69 | (-203.09068298339844-108.54627227783203j), |
| 70 | (-198.45195007324219-115.90768432617188j), |
| 71 | (-182.97744750976562-128.12318420410156j), |
| 72 | (-167-180j), |
| 73 | (-130.33688354492188-173.83778381347656j), |
| 74 | (-141.19784545898438-190.28807067871094j), |
| 75 | (-111.09677124023438-214.48896789550781j), |
| 76 | (-70.039543151855469-242.41630554199219j), |
| 77 | (-68.960540771484375-228.30015563964844j), |
| 78 | (-53.049201965332031-291.47097778320312j), |
| 79 | (-28.695289611816406-317.64553833007812j), |
| 80 | (57-300j), |
| 81 | (45.301143646240234-335.69509887695312j), |
| 82 | (91.936195373535156-373.32437133789062j), |
| 83 | (172.09465026855469-439.275146484375j), |
| 84 | (242.24473571777344-504.47515869140625j), |
| 85 | (387.81732177734375-666.6788330078125j), |
| 86 | (689.48553466796875-918.2142333984375j), |
| 87 | (1646.539306640625-1694.1956787109375j)) |
| 88 | |
| 89 | src = gr.vector_source_c(src_data) |
| 90 | s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size) |
| 91 | op = fft.fft_vcc(fft_size, True, [], False) |
| 92 | v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_size) |
| 93 | dst = gr.vector_sink_c() |
| 94 | tb.connect(src, s2v, op, v2s, dst) |
| 95 | tb.run() |
| 96 | result_data = dst.data() |
| 97 | #print 'expected:', expected_result
|
| 98 | #print 'results: ', result_data
|
| 99 | #self.assertComplexTuplesAlmostEqual (expected_result, result_data, 5)
|
| 100 | self.assert_fft_ok2(expected_result, result_data)
|
| 101 | |
| 102 | def test_002(self): |
| 103 | tb = gr.top_block() |
| 104 | fft_size = 32
|
| 105 | |
| 106 | tmp_data = ((4377+4516j), |
| 107 | (-1706.1268310546875+1638.4256591796875j), |
| 108 | (-915.2083740234375+660.69427490234375j), |
| 109 | (-660.370361328125+381.59600830078125j), |
| 110 | (-499.96044921875+238.41630554199219j), |
| 111 | (-462.26748657226562+152.88948059082031j), |
| 112 | (-377.98440551757812+77.5928955078125j), |
| 113 | (-346.85821533203125+47.152004241943359j), |
| 114 | (-295+20j), |
| 115 | (-286.33609008789062-22.257017135620117j), |
| 116 | (-271.52999877929688-33.081821441650391j), |
| 117 | (-224.6358642578125-67.019538879394531j), |
| 118 | (-244.24473571777344-91.524826049804688j), |
| 119 | (-203.09068298339844-108.54627227783203j), |
| 120 | (-198.45195007324219-115.90768432617188j), |
| 121 | (-182.97744750976562-128.12318420410156j), |
| 122 | (-167-180j), |
| 123 | (-130.33688354492188-173.83778381347656j), |
| 124 | (-141.19784545898438-190.28807067871094j), |
| 125 | (-111.09677124023438-214.48896789550781j), |
| 126 | (-70.039543151855469-242.41630554199219j), |
| 127 | (-68.960540771484375-228.30015563964844j), |
| 128 | (-53.049201965332031-291.47097778320312j), |
| 129 | (-28.695289611816406-317.64553833007812j), |
| 130 | (57-300j), |
| 131 | (45.301143646240234-335.69509887695312j), |
| 132 | (91.936195373535156-373.32437133789062j), |
| 133 | (172.09465026855469-439.275146484375j), |
| 134 | (242.24473571777344-504.47515869140625j), |
| 135 | (387.81732177734375-666.6788330078125j), |
| 136 | (689.48553466796875-918.2142333984375j), |
| 137 | (1646.539306640625-1694.1956787109375j)) |
| 138 | |
| 139 | src_data = tuple([x/fft_size for x in tmp_data]) |
| 140 | |
| 141 | expected_result = tuple([complex(primes[2*i], primes[2*i+1]) for i in range(fft_size)]) |
| 142 | |
| 143 | src = gr.vector_source_c(src_data) |
| 144 | s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size) |
| 145 | op = fft.fft_vcc(fft_size, False, [], False) |
| 146 | v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_size) |
| 147 | dst = gr.vector_sink_c() |
| 148 | tb.connect(src, s2v, op, v2s, dst) |
| 149 | tb.run() |
| 150 | result_data = dst.data() |
| 151 | #print 'expected:', expected_result
|
| 152 | #print 'results: ', result_data
|
| 153 | #self.assertComplexTuplesAlmostEqual (expected_result, result_data, 5)
|
| 154 | self.assert_fft_ok2(expected_result, result_data)
|
| 155 | |
| 156 | def test_003(self): |
| 157 | # Same test as above, only use 2 threads
|
| 158 | |
| 159 | tb = gr.top_block() |
| 160 | fft_size = 32
|
| 161 | |
| 162 | tmp_data = ((4377+4516j), |
| 163 | (-1706.1268310546875+1638.4256591796875j), |
| 164 | (-915.2083740234375+660.69427490234375j), |
| 165 | (-660.370361328125+381.59600830078125j), |
| 166 | (-499.96044921875+238.41630554199219j), |
| 167 | (-462.26748657226562+152.88948059082031j), |
| 168 | (-377.98440551757812+77.5928955078125j), |
| 169 | (-346.85821533203125+47.152004241943359j), |
| 170 | (-295+20j), |
| 171 | (-286.33609008789062-22.257017135620117j), |
| 172 | (-271.52999877929688-33.081821441650391j), |
| 173 | (-224.6358642578125-67.019538879394531j), |
| 174 | (-244.24473571777344-91.524826049804688j), |
| 175 | (-203.09068298339844-108.54627227783203j), |
| 176 | (-198.45195007324219-115.90768432617188j), |
| 177 | (-182.97744750976562-128.12318420410156j), |
| 178 | (-167-180j), |
| 179 | (-130.33688354492188-173.83778381347656j), |
| 180 | (-141.19784545898438-190.28807067871094j), |
| 181 | (-111.09677124023438-214.48896789550781j), |
| 182 | (-70.039543151855469-242.41630554199219j), |
| 183 | (-68.960540771484375-228.30015563964844j), |
| 184 | (-53.049201965332031-291.47097778320312j), |
| 185 | (-28.695289611816406-317.64553833007812j), |
| 186 | (57-300j), |
| 187 | (45.301143646240234-335.69509887695312j), |
| 188 | (91.936195373535156-373.32437133789062j), |
| 189 | (172.09465026855469-439.275146484375j), |
| 190 | (242.24473571777344-504.47515869140625j), |
| 191 | (387.81732177734375-666.6788330078125j), |
| 192 | (689.48553466796875-918.2142333984375j), |
| 193 | (1646.539306640625-1694.1956787109375j)) |
| 194 | |
| 195 | src_data = tuple([x/fft_size for x in tmp_data]) |
| 196 | |
| 197 | expected_result = tuple([complex(primes[2*i], primes[2*i+1]) for i in range(fft_size)]) |
| 198 | |
| 199 | nthreads = 2
|
| 200 | |
| 201 | src = gr.vector_source_c(src_data) |
| 202 | s2v = gr.stream_to_vector(gr.sizeof_gr_complex, fft_size) |
| 203 | op = fft.fft_vcc(fft_size, False, [], False, nthreads) |
| 204 | v2s = gr.vector_to_stream(gr.sizeof_gr_complex, fft_size) |
| 205 | dst = gr.vector_sink_c() |
| 206 | tb.connect(src, s2v, op, v2s, dst) |
| 207 | tb.run() |
| 208 | result_data = dst.data() |
| 209 | self.assert_fft_ok2(expected_result, result_data)
|
| 210 | |
| 211 | if __name__ == '__main__': |
| 212 | gr_unittest.run(test_fft, "test_fft.xml")
|
| 213 |