2 # Fast discrete cosine transform algorithms (Python)
4 # Copyright (c) 2020 Project Nayuki. (MIT License)
5 # https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
7 # Permission is hereby granted, free of charge, to any person obtaining a copy of
8 # this software and associated documentation files (the "Software"), to deal in
9 # the Software without restriction, including without limitation the rights to
10 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 # - The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 # - The Software is provided "as is", without warranty of any kind, express or
16 # implied, including but not limited to the warranties of merchantability,
17 # fitness for a particular purpose and noninfringement. In no event shall the
18 # authors or copyright holders be liable for any claim, damages or other
19 # liability, whether in an action of contract, tort or otherwise,
20 # arising from, out of or in connection with the Software or the use
21 # or other dealings in the Software.
24 import math
, random
, unittest
25 import fastdctlee
, naivedct
, remap_dct_yield
28 class FastDctTest(unittest
.TestCase
):
30 def tst_fast_dct_lee_vs_naive(self
):
33 vector
= FastDctTest
.nonrandom_vector(n
)
34 expect
= naivedct
.transform(vector
)
35 original
= fastdctlee
.transform(vector
)
36 actual
= fastdctlee
.transform2(vector
)
37 self
.assertListAlmostEqual(actual
, expect
)
38 expect
= naivedct
.inverse_transform(vector
)
39 original
= fastdctlee
.inverse_transform2(vector
)
40 actual
= fastdctlee
.inverse_transform_iter(vector
)
41 self
.assertListAlmostEqual(actual
, expect
)
43 def test_yield_dct_lee_vs_naive(self
):
44 for i
in range(2, 10):
46 vector
= FastDctTest
.nonrandom_vector(n
)
47 expect
= fastdctlee
.transform2(vector
)
48 actual
= remap_dct_yield
.transform2(vector
)
49 self
.assertListAlmostEqual(actual
, expect
)
50 expect
= naivedct
.inverse_transform(vector
)
51 actual
= fastdctlee
.inverse_transform_iter(vector
)
52 self
.assertListAlmostEqual(actual
, expect
)
53 actual
= remap_dct_yield
.inverse_transform2(vector
)
54 self
.assertListAlmostEqual(actual
, expect
)
56 def tst_fast_dct_lee_invertibility(self
):
57 for i
in range(1, 10):
59 vector
= FastDctTest
.random_vector(n
)
60 temp
= fastdctlee
.transform2(vector
)
61 temp
= fastdctlee
.inverse_transform(temp
)
62 temp
= [(val
* 2.0 / n
) for val
in temp
]
63 self
.assertListAlmostEqual(vector
, temp
)
65 def tst_yield_fast_dct_lee_invertibility(self
):
66 for i
in range(1, 10):
68 vector
= FastDctTest
.random_vector(n
)
69 temp
= remap_dct_yield
.transform2(vector
)
70 temp
= fastdctlee
.inverse_transform(temp
)
71 temp
= [(val
* 2.0 / n
) for val
in temp
]
72 self
.assertListAlmostEqual(vector
, temp
)
74 def assertListAlmostEqual(self
, actual
, expect
):
75 self
.assertEqual(len(actual
), len(expect
))
76 for (x
, y
) in zip(actual
, expect
):
77 self
.assertAlmostEqual(x
, y
, delta
=FastDctTest
._EPSILON
)
81 return [random
.uniform(-1.0, 1.0) for _
in range(n
)]
84 def nonrandom_vector(n
):
85 return [(i
-n
/2.0+pow(1.001, n
)) for i
in range(n
)]
91 if __name__
== "__main__":