Skip to content

Instantly share code, notes, and snippets.

@RdlP
Created November 1, 2017 19:46
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 RdlP/eb546bf69b7ceff5859ec45244599c64 to your computer and use it in GitHub Desktop.
Save RdlP/eb546bf69b7ceff5859ec45244599c64 to your computer and use it in GitHub Desktop.
Implementation of dct forward and inverse
#include <math.h>
void dct(float *dct_matrix, float *matrix, int N){
int i,j,u,v;
float Cu, Cv;
memset(dct_matrix, 0, N*N*sizeof(float));
float accum = 0;
for (u = 0; u < N; ++u){
for (v = 0; v < N; ++v){
if (u == 0){
Cu = 1.0/sqrt(2.0);
}else{
Cu = 1.0;
}
if (v == 0){
Cv = 1.0 /sqrt(2.0);
}else{
Cv = 1.0;
}
accum = 0.0f;
for (i = 0; i < N; i ++){
for (j = 0; j < N; j++){
accum += matrix[i*8+j] * cos((2*j+1)*u*M_PI/16.0) * cos((2*i+1)*v*M_PI/16.0);
}
}
dct_matrix[u*8+v] = 0.25*Cu*Cv*accum;
}
}
}
void idct(float *matrix, float *dct_matrix, int N){
int i,j,u,v;
float Cu, Cv;
float accum = 0;
for (i = 0; i < N; ++i){
for (j = 0; j < N; ++j){
accum = 0.0f;
for (u = 0; u < N; u ++){
for (v = 0; v < N; v++){
if (u == 0){
Cu = 1.0/sqrt(2.0);
}else{
Cu = 1.0;
}
if (v == 0){
Cv = 1.0 /sqrt(2.0);
}else{
Cv = 1.0;
}
accum += dct_matrix[u*8+v] * Cu * Cv * cos((2*j+1)*u*M_PI/16.0) * cos((2*i+1)*v*M_PI/16.0);
}
}
matrix[i*8+j] = 0.25*accum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment