psambit9791 / jdsp

A Java Library for Digital Signal Processing
https://jdsp.dev
MIT License
247 stars 43 forks source link

Zero-phase digital filtering #35

Closed liuyijuna closed 2 years ago

liuyijuna commented 2 years ago

This is a great library, now I translate matlab program to java code, there is a filtfilt() method in matlab, it's Zero-phase digital filtering, is it supported in your library? thanks

psambit9791 commented 2 years ago

JDSP currently does not have any equivalent method for filtfilt().

wenhuancui commented 1 year ago

A possible solution:

    public double[] bandPassFiltfilt(double[] signal, int order, double lowCutoff, double highCutoff) throws IllegalArgumentException {
        if (lowCutoff >= highCutoff) {
            throw new IllegalArgumentException("Lower Cutoff Frequency cannot be more than the Higher Cutoff Frequency");
        }
        double centreFreq = (highCutoff + lowCutoff) / 2.0;
        double width = Math.abs(highCutoff - lowCutoff);
        double[] output = new double[signal.length];
        uk.me.berndporr.iirj.Butterworth bp = new uk.me.berndporr.iirj.Butterworth();
        bp.bandPass(order, this.samplingFreq, centreFreq, width);
        for (int i = 0; i < output.length; i++) {
            output[i] = bp.filter(signal[i]);
        }
        bp.reset();
        double[] result = new double[output.length];
        for (int i = output.length - 1; i >= 0; i--) { // reverse
            result[i] = bp.filter(output[i]);
        }
        return result;
    }