package net.heartshield.filter;

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

/**
 * Phase-locked loop filter.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-03-29
 */
public class PllFilter extends CRFilterBlock {
    private double mFps;
    private Pll mPll;

    /**
     * @param bandwidth  determines the lock range (Hz)
     * @param minFreq    minimum frequency (Hz)
     * @param maxFreq    maximum frequency (Hz)
     */
    public PllFilter(double fps, double bandwidth, double minFreq, double maxFreq) {
        mFps = fps;
        mPll = new Pll(normFreq(bandwidth), normFreq(minFreq), normFreq(maxFreq));
    }

    /** @return normalized frequency */
    private double normFreq(double f) { return f * 2.0 * Math.PI / mFps; }

    @Override
    protected double[] batch(Complex[] x) {
        double[] ret = new double[x.length];
        mPll.batch(x, ret);
        return ret;
    }
}
