iTutor Math Updates
- Details
- Written by Super User
- Hits: 1087
Here is a simple C code for DCT/IDCT to calculate. main function relevant readily accessible.
* First, the original signal is generated values, and print them:
#define N 16
double X[N];
for (int i=0; i<N; i++)
{
double x=(double)i/(double)N;
X[i] = 0.3*x*x*x*x - 0.5*x*x*x - 0.9*x*x - 0.1*x + 0.75;
}
for (int i=0; i<N; i++)
printf("%.20f\n", X[i]);
printf("\n\n");
* Then the signal calculated for the coefficients in the function mkDCT. The soundness of coefficients is that they can be quantized and packed efficiently. For example, in GSM, the basic idea here.
double Y[N];
mkDCT(Y, X, N);
for (int i=0; i<N; i++)
printf("%.20f\n", Y[i]);
printf("\n\n");
* Finally, calculated on the original values of the coefficients of the function mkIDCT:
double Z[N];
mkIDCT(Z, Y, N);
for (int i=0; i<N; i++)
printf("%.20f\n", Z[i]);
printf("\n\n");
Code:
#include <math.h>
#include <stdio.h>
#define pi 3.14159265358979323846
void mkDCT(double *dst, double *src, int N)
{
for (int k=0; k<N; k++)
{
dst[k]=0;
for (int n=0; n<N; n++)
{
dst[k]+=src[n]*cos(pi*k/N*(n+0.5));
}
}
}
void mkIDCT(double *dst, double *src, int N)
{
for (int k=0; k<N; k++)
{
dst[k]=src[0]/2;
for (int n=1; n<N; n++)
{
dst[k]+=src[n]*cos(pi*n/N*(k+0.5));
}
dst[k]=dst[k]*2/N;
}
}
void main(void)
{
#define N 16
double X[N];
for (int i=0; i<N; i++)
{
double x=(double)i/(double)N;
X[i] = 0.3*x*x*x*x - 0.5*x*x*x - 0.9*x*x - 0.1*x + 0.75;
}
for (int i=0; i<N; i++)
printf("%.20f\n", X[i]);
printf("\n\n");
double Y[N];
mkDCT(Y, X, N);
for (int i=0; i<N; i++)
printf("%.20f\n", Y[i]);
printf("\n\n");
double Z[N];
mkIDCT(Z, Y, N);
for (int i=0; i<N; i++)
printf("%.20f\n", Z[i]);
printf("\n\n");
}