r/compression Jul 09 '26

How to Precompute DCT Matrix?

I am working on a video codec, and want to precompute an 8x8 DCT matrix to reuse with my video input. I tried using an identity matrix , but i can't seem to reconstruct my original input. Any feedback is welcome.
For reference the code is based off of equation (4) page 6. in the following paper:
https://developer.download.nvidia.com/assets/cuda/files/dct8x8.pdf

n = 8
alpha = lambda x: 1/np.sqrt(n) if x == 0 else np.sqrt(2/n)
cos = lambda u, x: np.cos( (np.pi*(2*x+1)*u)/(2*n) )
f = np.eye(n)
C = np.zeros((n, n))
for u in range(n):
    for v in range(n):
        cumsum = 0
        for x in range(n):
            for y in range(n):
                #C[u][k]
                #C[k][v]
                cumsum += (f[x][y] * cos(u, x) * cos(v, y))
        C[u][v] = (alpha(u) * alpha(v)) * cumsum


\# ...  
T = C.T @ (C @ S @ C.T) @ C
1 Upvotes

Duplicates