Skip to content

Instantly share code, notes, and snippets.

@sleibrock
Created December 2, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleibrock/3a8fdf102df673e6a33355f6f66192fd to your computer and use it in GitHub Desktop.
Save sleibrock/3a8fdf102df673e6a33355f6f66192fd to your computer and use it in GitHub Desktop.
Discrete Cosine Transform test
#!/usr/bin/env python
from math import cos, pi, sqrt
from itertools import product
test_matrix = [[90, 100], [100, 105]]
def dct(lst):
if len(lst) == 0:
return []
p = pi / len(lst)
new_data = [0 for x in lst]
for (k, x) in enumerate(lst):
for n in range(len(lst)):
new_data[k] += lst[n] * cos(p * (n + 0.5) * k)
return new_data
def mdct(lst):
if len(lst) == 0:
return []
width = len(lst)
height = len(lst[0])
for row in lst:
if len(row) != height:
return []
new_data = [[0 for x in y] for y in lst]
indices = list(product(range(width), range(height)))
p1 = pi / width
p2 = pi / height
ci = 0
cj = 0
for (i, j) in indices:
dct = 0
ci = 0
cj = 0
dct = 0
sumv = 0
if i == 0:
ci = 1 / sqrt(width)
else:
ci = sqrt(2) / sqrt(width)
if j == 0:
cj = 1 / sqrt(height)
else:
cj = sqrt(2) / sqrt(height)
for k in range(width):
for l in range(height):
dct = lst[k][l] * cos(((2*k+1) * i * pi) / (2 * width)) * cos(((2*l+1) * j * pi)/(2*height))
print(f"({k}, {l}) = {dct}")
sumv += dct
new_data[i][j] = sumv * ci * cj
return new_data
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment