Skip to content

Instantly share code, notes, and snippets.

@justjkk
Created February 13, 2011 07:01
Show Gist options
  • Select an option

  • Save justjkk/824509 to your computer and use it in GitHub Desktop.

Select an option

Save justjkk/824509 to your computer and use it in GitHub Desktop.
Discrete Cosine Transformation in python using FP
from math import cos
PI = 3.141592
N = 8
def f(u,v):
if u + v % 2: return 0
return 1
def sum(x,y):
return x + y
def sigma(r,f):
return reduce(sum, map(f,r))
def C(u):
if u == 0: return 2**-0.5
return 1.0
def F(u,v):
return round((C(u) * C(v) / 4) * sigma(range(N), lambda i: sigma(range(N), lambda j: cos((2 * i + 1) * u * PI / (2 * N)) * cos((2 * j + 1) * v * PI / (2 * N)) * f(i,j))),2)
def dct():
return map(lambda u: map(lambda v: F(u,v), range(N)), range(N))
print dct()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment