Skip to content

Instantly share code, notes, and snippets.

@ste5e99
Created December 2, 2019 21:21
Show Gist options
  • Select an option

  • Save ste5e99/3a8fdf102df673e6a33355f6f66192fd to your computer and use it in GitHub Desktop.

Select an option

Save ste5e99/3a8fdf102df673e6a33355f6f66192fd to your computer and use it in GitHub Desktop.

Revisions

  1. ste5e99 created this gist Dec 2, 2019.
    65 changes: 65 additions & 0 deletions dct.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/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