package net.heartshield.signal;

import junit.framework.TestCase;

import net.heartshield.filter.LineChart;

import java.util.ArrayList;
import java.util.List;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-05-11
 */
public class RunningQualityTest extends TestCase {
    private class TestResults {
        int nbeats = 0;
        List<Double> posCorrCoeffs = new ArrayList<>();
    }

    public void testRQBeatDet() {
        final double FPS = 30.0;
        final TestResults results = new TestResults();

        RunningQuality rq = new RunningQuality(FPS);
        rq.setListener(new RunningQuality.BeatListener() {
            @Override
            public void onLocked(int startIdx) {}

            @Override
            public void onBeat(int startIdx, boolean goodBeat, double posCorrCoeff) {
                System.out.println("onBeat(posCorrCoeff=" + posCorrCoeff);
                results.nbeats++;
                results.posCorrCoeffs.add(posCorrCoeff);
            }
        });

        Series series = new Series("data/1480348240_B319CE46_meta.ppg_raw_even.json");

        // TODO: detrend

        /*
        LineChart lc = LineChart.plot(series.t, series.x);
        lc.showChart();
        lc.waitChart();
        */
        // note: it works even on raw (undetrended) data.
        // but in real scenario, we need to detrend, otherwise a signal with around 230 DC and only <1 AC will always have very good correlation

        for(int i = 0; i < series.x.length - 30; i += 30)
            rq.append(i / FPS, DVec.get(series.x, i, i + 30));

        assertTrue("not enough beats detected", results.nbeats > 40);
        assertTrue("too many beats detected", results.nbeats < 180);
    }
}
