package yi.stat.hist;

public class LogarithmicQuantizer implements Quantizer{

  /**
   * The base of the logarithmic calculation
   */
  double m_base;

  /**
   * Bin number
   */
  int m_binNum;

  public LogarithmicQuantizer(double base, int bin_num) {
    m_base = base;
    m_binNum = bin_num;
  }

  /**
   * Quantize the data useing logarithmic scale.  For example, if base = 2, then
   * the data value ranges are: [0, 1), [1, 2), [2, 4), [4, 8), etc.  The upper
   * limit of range-i is 2^i.  If the data is out the range (decided by
   * m_binNum).  It will be quantized to the maximum possible value
   * @param data
   * @param base
   * @return
   */
  public int quantize(double data) {
    if (data <= 0) return 0;
    int level = (int)Math.floor(Math.log(data) / Math.log(m_base)) + 1;
    if (level < 0) level = 0;
    if (level >= m_binNum) level = m_binNum - 1;
    return level;
  }

  /**
   *
   */
  public static void main(String[] args) {
    LogarithmicQuantizer quantizer = new LogarithmicQuantizer(2, 5);

    for (int i = -1; i <= 20; i++) {
      System.out.println(Double.toString(i*0.5) + "->" + quantizer.quantize(i*0.5));
    }
  }

}