package net.heartshield.filter;

import org.apache.commons.math3.complex.Complex;

/**
 * Hilbert transform turns a series of real-valued samples into complex-valued (analytic) samples.
 *
 * Introduces a delay of 32 samples.
 * Also, the first 32 output samples are invalid (initial settling of the filter).
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-03-26
 */
public class Hilbert extends RCFilterBlock {
    RFilterBlock mReal;
    FirFilter mImag;
    int mDelay;

    public Hilbert() {
        //super(FirFilters.hilbert(65), Double.NaN);
        mImag = new HilbertImag();
        mReal = new Delay(mImag.getDelay());
        mDelay = mImag.getDelay();
    }

    /** filter delay in number of samples */
    public int getDelay() {
        return mDelay;
    }

    @Override
    protected Complex[] batch(double[] x) {
        double[] real = mReal.batch(x);
        double[] imag = mImag.batch(x);
        Complex[] result = new Complex[real.length];
        for(int i = 0; i < real.length; i++)
            result[i] = new Complex(real[i], imag[i]);
        return result;
    }
}
