一维小波变换的C++实现

#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);
     }
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值