1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 #define LENGTH 512 5 #define LEVEL 4 6 #define L_core 6 7 8 int main() 9 { 10 11 12 } 13 14 15 static void Covlution(double data[], double core[], double cov[], int LEN) 16 { 17 double temp[LENGTH + L_core - 1] = { 0 }; 18 int i = 0; 19 int j = 0; 20 21 for (i = 0; i < LEN; i++) 22 { 23 for (j = 0; j < L_core; j++) 24 { 25 temp[i + j] += data[i] * core[j]; 26 } 27 } 28 29 for (i = 0; i < LEN; i++) 30 { 31 if (i < L_core - 1) 32 cov[i] = temp[i] + temp[LEN + i]; 33 else 34 cov[i] = temp[i]; 35 } 36 37 } 38 //卷积 39 static void Covlution2(double data[], double core[], double cov[], int LEN) 40 { 41 double temp[LENGTH + L_core - 1] = { 0 }; 42 int i = 0; 43 int j = 0; 44 45 for (i = 0; i < LEN; i++) 46 { 47 for (j = 0; j < L_core; j++) 48 { 49 temp[i + j] += data[i] * core[j]; 50 } 51 } 52 53 for (i = 0; i < LEN; i++) 54 { 55 if (i < L_core - 1) 56 cov[i + LEN - L_core + 1] = temp[i] + temp[LEN + i]; 57 else 58 cov[i - L_core + 1] = temp[i]; 59 } 60 61 } 62 //核 63 static void DWT1D(double input[], double output[], double LF[], double HF[], int l) 64 { 65 int i = 0; 66 double temp[LENGTH] = { 0 }; 67 int LEN = LENGTH / pow(2, l - 1); 68 69 Covlution(input, LF, temp, LEN); 70 for (i = 1; i < LEN; i += 2) 71 { 72 output[i / 2] = temp[i]; 73 } 74 75 Covlution(input, HF, temp, LEN); 76 for (i = 1; i < LEN; i += 2) 77 { 78 output[LEN / 2 + i / 2] = temp[i]; 79 } 80 } 81 //变换吗? 82 static void DWT(double input[], double output[], double LF[], double HF[], int len[]) 83 { 84 int i; 85 int j; 86 87 len[0] = len[1] = LENGTH / pow(2, LEVEL); 88 for (i = 2; i <= LEVEL; i++) len[i] = len[i - 1] * 2; 89 90 DWT1D(input, output, LF, HF, 1); 91 for (i = 2; i <= LEVEL; i++) 92 { 93 for (j = 0; j < len[LEVEL + 2 - i]; j++) input[j] = output[j]; 94 DWT1D(input, output, LF, HF, i); 95 } 96 } 97 //一层吗 98 static void IDWT1D(double input[], double output[], double LF[], double HF[], int l, int flag) 99 { 100 int i = 0; 101 double temp[LENGTH] = { 0 }; 102 int LEN = l * 2; 103 104 if (flag) Covlution2(input, HF, temp, LEN); 105 else Covlution2(input, LF, temp, LEN); 106 107 for (i = 0; i < LEN; i++) 108 { 109 output[i] = temp[i]; 110 } 111 } 112 //二层吗 113 static void IDWT(double input[], double output[], double LF[], double HF[], int len[], int level) 114 { 115 int i; 116 int j; 117 for (j = 0; j < len[LEVEL + 1 - level]; j++) 118 { 119 output[2 * j] = 0; 120 output[2 * j + 1] = input[j]; 121 } 122 for (j = 0; j < 2 * len[LEVEL + 1 - level]; j++) 123 { 124 input[j] = output[j]; 125 } 126 IDWT1D(input, output, LF, HF, len[LEVEL + 1 - level], 1); 127 128 for (i = level - 1; i > 0; i--) 129 { 130 for (j = 0; j < len[LEVEL + 1 - i]; j++) 131 { 132 input[2 * j] = 0; 133 input[2 * j + 1] = output[j]; 134 } 135 IDWT1D(input, output, LF, HF, len[LEVEL + 1 - i], 0); 136 } 137 }