package net.heartshield.filter;


import junit.framework.TestCase;

import net.heartshield.signal.DVec;
import net.heartshield.signal.SVec;

public class AlivecorTests extends TestCase {
    public void testAlivecor() {
        // 1490155457_series.b_7F07F378
        //short[] audio = AudioHelper.readAudio("signal/src/test/res/raw/test100hz.wav");

        long t;

        t = System.nanoTime();
        AudioHelper ah = new AudioHelper("signal/src/test/res/raw/1490155457_series.b_7F07F378");
        short[] audio = ah.readAudio();
        System.out.println("readAudio() took " + (System.nanoTime()-t)/1000000 + " ms");

        t = System.nanoTime();
        double[] sig = SVec.toDouble(audio);
        double[] norm = DVec.mul(sig, 1.0 / Math.pow(2, 15));
        System.out.println("toDouble() and norm took " + (System.nanoTime()-t)/1000000 + " ms");

        t = System.nanoTime();
        AlivecorFilter af = new AlivecorFilter(ah.getFps());
        DataSink out = new DataSink();
        af.connect(out);
        af.put(norm);
        System.out.println("af.put() took " + (System.nanoTime()-t)/1000000 + " ms");
        // af.put() took 5279 ms

        t = System.nanoTime();
        double[] res = out.getData();

        double[] slow = DVec.downsample(res, 100);
        double[] show = DVec.get(slow, 2000, slow.length);
        show = DVec.sub(show, 2.0);

        System.out.println("rest took " + (System.nanoTime()-t)/1000000 + " ms");

        t = System.nanoTime();
        //LineChart lc = new LineChart("hilbert", new XYSeries[]{ inSeries, outSeries });
        LineChart lc = LineChart.plot(show);
        lc.showChart();
        System.out.println("plotting took " + (System.nanoTime()-t)/1000000 + " ms");
        lc.waitChart();
    }
}
