java实现Haar小波变换

原理和具体推导参考以下两篇博客:
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值