package net.heartshield.filter;

/**
 * Realtime FM demodulator for AliveCor Kardia hand-held ECG.
 * Introduces a delay and outputs some initial invalid samples (initial settling of the filter).
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-03-29
 */
public class AlivecorFilter extends RFilterBlock {
    private BandPassFilter mPrefilter;
    private Hilbert mHilbert;
    private PllFilter mPll;
    private LowPassFilter mLowpass;
    private BandRejectFilter mBandreject;
    private int mDelay;

    public AlivecorFilter(double fps) {
        mPrefilter = new BandPassFilter(fps, 17000, 21000, 500);
        mHilbert = new Hilbert();
        mPll = new PllFilter(fps, 1500, 17000, 21000);
        mLowpass = new LowPassFilter(fps, 200, 50);
        //mBandreject = new BandRejectFilter(fps, 40, 60, 3);

        mPrefilter.connect(mHilbert);
        mHilbert.connect(mPll);
        mPll.connect(mLowpass);
        //mLowpass.connect(mBandreject);

        mDelay = mPrefilter.getDelay() + mHilbert.getDelay() + mLowpass.getDelay(); // + mBandreject.getDelay();
    }

    public double getFps() {
        return mPrefilter.getFps();
    }

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

    @Override
    protected double[] batch(double[] x) {
        throw new IllegalStateException();
    }

    @Override
    public void connect(IRFilterBlock consumer) {
        //mBandreject.connect(consumer);
        mLowpass.connect(consumer);
    }

    @Override
    public void put(double[] x) {
        mPrefilter.put(x);
    }
}
