class FastDctTest(unittest.TestCase):
- def tst_fast_dct_lee_vs_naive(self):
- for i in range(3, 10):
+ def test_fast_dct_lee_vs_naive(self):
+ for i in range(3, 4):
n = 2**i
vector = FastDctTest.nonrandom_vector(n)
expect = naivedct.transform(vector)
actual = fastdctlee.transform2(vector)
self.assertListAlmostEqual(actual, expect)
expect = naivedct.inverse_transform(vector)
- actual = fastdctlee.inverse_transform(vector)
+ actual = fastdctlee.inverse_transform2(vector)
self.assertListAlmostEqual(actual, expect)
- def test_yield_dct_lee_vs_naive(self):
+ def tst_yield_dct_lee_vs_naive(self):
for i in range(3, 4):
n = 2**i
vector = FastDctTest.nonrandom_vector(n)
inverse_transform(alpha, False, indent+1)
inverse_transform(beta , False, indent+1)
for i in range(half):
- x = alpha[i]
- y = beta[i] / (math.cos((i + 0.5) * math.pi / n) * 2)
+ x, y = alpha[i], beta[i]
+ coeff = (math.cos((i + 0.5) * math.pi / n) * 2)
+ y /= coeff
vector[i] = x + y
- vector[-(i + 1)] = x - y
+ vector[n-(i+1)] = x - y
print (idt, " v[%d] = alpha[%d]+beta[%d]" % (i, i, i))
print (idt, " v[%d] = alpha[%d]-beta[%d]" % (n-i-1, i, i))
return vector
-def inverse_transform2(vector, root=True):
+def inverse_transform2(vector, root=True, indent=0):
+ idt = " " * indent
n = len(vector)
if root:
vector = list(vector)
+ vector[0] /= 2
if n == 1:
return vector
elif n == 0 or n % 2 != 0:
raise ValueError()
else:
half = n // 2
- alpha = [0]
- beta = [1]
+ alpha = [vector[0]]
+ beta = [vector[1]]
for i in range(2, n, 2):
- alpha.append(i)
- beta.append(("add", i - 1, i + 1))
- inverse_transform2(alpha, False)
- inverse_transform2(beta , False)
+ alpha.append(vector[i])
+ beta.append(vector[i - 1] + vector[i + 1])
+ inverse_transform2(alpha, False, indent+1)
+ inverse_transform2(beta , False, indent+1)
for i in range(half):
+ x, y = alpha[i], beta[i]
+ coeff = (math.cos((i + 0.5) * math.pi / n) * 2)
+ y /= coeff
+ vector[i] = x + y
+ vector[n-(i+1)] = x - y
+ print (idt, " v[%d] = alpha[%d]+beta[%d]" % (i, i, i))
+ print (idt, " v[%d] = alpha[%d]-beta[%d]" % (n-i-1, i, i))
x = alpha[i]
- y = ("cos", beta[i], i)
- vector[i] = ("add", x, y)
- vector[-(i + 1)] = ("sub", x, y)
return vector