package net.heartshield.filter;

import junit.framework.TestCase;

import net.heartshield.filter.FFirFilterRunning;
import net.heartshield.filter.FirFilterRunning;
import net.heartshield.signal.Signal;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-05-15
 */
public class FirPerfTest extends TestCase {
    public void testPerformanceDouble() {
        double[] taps = Signal.triangle(200);
        double[] sig = new double[20000];

        double FPS = 200.0;
        double F = 0.1;
        for(int i = 0; i < sig.length; i++)
            sig[i] = Math.sin(2*Math.PI*F/FPS*i);

        FirFilterRunning fir = new FirFilterRunning(taps);

        long t1 = System.nanoTime();
        double[] y = fir.batch(sig);
        long t2 = System.nanoTime();

        throw new RuntimeException("time elapsed: " + (t2-t1) + " ns");

        /*
         * Nexus 5x: ~ 40 ms
         * 28033752 ns
         * 40981567 ns
         * 46468442 ns
         * 48293911 ns
         */
    }

    private static float[] d2f(double[] x) {
        float[] f = new float[x.length];
        for(int i = 0; i < x.length; i++)
            f[i] = (float) x[i];
        return f;
    }

    public void testPerformanceFloat() {
        double[] taps = Signal.triangle(200);
        float[] sig = new float[20000];

        float FPS = 200.0f;
        float F = 0.1f;
        for(int i = 0; i < sig.length; i++)
            sig[i] = (float) Math.sin(2*Math.PI*F/FPS*i);

        FFirFilterRunning fir = new FFirFilterRunning(d2f(taps));

        long t1 = System.nanoTime();
        float[] y = fir.batch(sig);
        long t2 = System.nanoTime();

        throw new RuntimeException("time elapsed: " + (t2-t1) + " ns");

        /*
         * Nexus 5x:
         * 31485888 ns
         * 58385840 ns
         * 54760839 ns
         * 61453548 ns
         *
         * not faster - why?
         *
         * - CPU ALU is the same speed
         * - debugging overhead prevails
         */
    }
}
