package utils;

public class ExponentialBackoffFilter implements DTEstimator, Cloneable {
  private double bar_v; private double alpha;
  public ExponentialBackoffFilter(double v, double alpha) {
    bar_v = v; this.alpha=alpha;
  }
  public void update(double v) {
    bar_v += (v-bar_v)*alpha;;
  }
  public Object clone() { try { return super.clone(); }catch(Exception e) { return null; } }
  public String toString() { return ""+bar_v; }
  public double value() { return bar_v; }

  // testing routine
  public static void main (String argv[]) {
    double v;
    ExponentialBackoffFilter ebf = new ExponentialBackoffFilter(230,0.1);
    System.out.println("Initial value: "+ebf.value());
    v=230; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=228; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=228; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=226; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=223; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=227; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=228; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=223; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=222; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=222; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=221; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=220; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=220; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=220; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=218; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=220; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=224; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=226; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=228; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=230; ebf.update(v); System.out.println(v+" "+ebf.value());
    v=240; ebf.update(v); System.out.println(v+" "+ebf.value());
    ebf.update(ebf.value()); System.out.println("FB: "+ebf.value()); //feedback
    ebf.update(ebf.value()); System.out.println("FB: "+ebf.value());
    ebf.update(ebf.value()); System.out.println("FB: "+ebf.value());
    ebf.update(ebf.value()); System.out.println("FB: "+ebf.value());
    ebf.update(ebf.value()); System.out.println("FB: "+ebf.value());
  }
}
