#define LENGTH 512
#define LEVEL 4
#define L_core 6
static void Covlution(double data[], double core[], double cov[], int LEN)
{
double temp[LENGTH + L_core - 1] = {0};
int i = 0;
int j = 0;
for(i = 0; i < LEN; i++)
{
for(j = 0; j < L_core; j++)
{
temp[i + j] += data[i] * core[j];
}
}
for(i = 0; i < LEN; i++)
{
if(i < L_core - 1)
cov[i] = temp[i] + temp[LEN + i];
else
cov[i] = temp[i];
}
}
static void Covlution2(double data[], double core[], double cov[], int LEN)
{
double temp[LENGTH + L_core - 1] = {0};
int i = 0;
int j = 0;
for(i = 0; i < LEN; i++)
{
for(j = 0; j < L_core; j++)
{
temp[i + j] += data[i] * core[j];
}
}
for(i = 0; i < LEN; i++)
{
if(i < L_core - 1)
cov[i + LEN - L_core + 1] = temp[i] + temp[LEN + i];
else
cov[i - L_core + 1] = temp[i];
}
}
static void DWT1D(double input[], double output[], double LF[], double HF[], int l)
{
int i = 0;
double temp[LENGTH] = {0};
int LEN = LENGTH / pow(2, l - 1);
Covlution(input, LF, temp, LEN);
for(i = 1; i < LEN; i += 2)
{
output[i/2] = temp[i];
}
Covlution(input, HF, temp, LEN);
for(i = 1; i < LEN; i += 2)
{
output[LEN/2 + i/2] = temp[i];
}
}
static void DWT(double input[], double output[], double LF[], double HF[], int len[])
{
int i;
int j;
len[0] = len[1] = LENGTH / pow(2, LEVEL);
for(i = 2; i <= LEVEL; i++) len[i] = len[i - 1] * 2;
DWT1D(input, output, LF, HF, 1);
for(i = 2; i <= LEVEL; i++)
{
for(j = 0; j < len[LEVEL + 2 - i]; j++) input[j] = output[j];
DWT1D(input, output, LF, HF, i);
}
}
static void IDWT1D(double input[], double output[], double LF[], double HF[], int l, int flag)
{
int i = 0;
double temp[LENGTH] = {0};
int LEN = l * 2;
if(flag) Covlution2(input, HF, temp, LEN);
else Covlution2(input, LF, temp, LEN);
for(i = 0; i < LEN; i++)
{
output[i] = temp[i];
}
}
static void IDWT(double input[], double output[], double LF[], double HF[], int len[], int level)
{
int i;
int j;
for(j = 0; j < len[LEVEL + 1 - level]; j++)
{
output[2 * j] = 0;
output[2 * j + 1] = input[j];
}
for(j = 0; j < 2 * len[LEVEL + 1 - level]; j++)
{
input[j] = output[j];
}
IDWT1D(input, output, LF, HF, len[LEVEL + 1 - level], 1);
for(i = level - 1; i > 0; i--)
{
for(j = 0; j < len[LEVEL + 1 - i]; j++)
{
input[2 * j] = 0;
input[2 * j + 1] = output[j];
}
IDWT1D(input, output, LF, HF, len[LEVEL + 1 - i], 0);
}
}