原理和具体推导参考以下两篇博客:
https://blog.csdn.net/theonegis/article/details/86517377?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-7&spm=1001.2101.3001.4242
https://blog.csdn.net/theonegis/article/details/86533571?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-5&spm=1001.2101.3001.4242
代码:
一维:
多级:
public static double[] DiscreteHaarWaveletTransform_1D_Mul(double[] input) {
double[] output = new double[input.length];
for (int length = input.length / 2;; length = length / 2) {
for (int i = 0; i < length; i++) {
double sum = input[i * 2] + input[i * 2 + 1];
double difference = input[i * 2] - input[i * 2 + 1];
output[i] = sum/Math.sqrt(2);
output[length + i] = difference/Math.sqrt(2);
}
if (length == 1) {
return output;
}
System.arraycopy(output, 0, input, 0, length);
}
}
单级(matlab上对应的dwt函数):
public static double[] DiscreteHaarWaveletTransform_1D(double[] input) {
double[] output = new double[input.length];
for (int i = 0; i < input.length/2; i++) {
double sum = input[i * 2] + input[i * 2 + 1];
double difference = input[i * 2] - input[i * 2 + 1];
output[i] = sum/Math.sqrt(2);
output[input.length/2 + i] = difference/Math.sqrt(2);
}
return output;
}
二维:
public static double[][] DiscreteHaarWaveletTransform_2D(double[][] input) {
double[][] output = new double[input.length][input[0].length];
//对每一行做小波变换
for (int i = 0; i < input.length; i++) {
output[i]= DiscreteHaarWaveletTransform_1D(input[i]);
}
double[] temp = new double[input.length];
double[] res=null;
//对每一列做小波变换
for (int i = 0; i < output[0].length; i++) {
for (int j = 0; j < output.length; j++) {
temp[j]=output[j][i];
}
res = DiscreteHaarWaveletTransform_1D(temp);
for (int j = 0; j < output.length; j++) {
output[j][i]=res[j];
}
}
return output;
}