package talkhouse;

public class Segmenter {
  private double[] buf=null;
  int winsz;
  int winshift;
  static final int MIN_SEGMENTS = 3;

  public Segmenter(int winsz, int winshift) {
    this.winsz = winsz;
    this.winshift = winshift;
  }

  public double[][] apply(double[] data) {
    double[] combo;
    if (buf != null) {
        combo = new double[buf.length + data.length];
        System.arraycopy(buf, 0, combo, 0, buf.length);
        System.arraycopy(data, 0, combo, buf.length, data.length); 
    } else {
        combo = data;
    }
    if (combo.length < winsz + winshift * MIN_SEGMENTS){
        buf = combo;
        return null;
    } else {
        buf = null;
    }
    double[][] result = new double[(combo.length-winsz)/winshift+1][];
    int i = 0;
    int j=0;
    while (i+winsz <= combo.length) {
      double[] seg = new double[winsz];
      System.arraycopy(combo, i, seg, 0, winsz);
      result[j++] = seg;
      i+=winshift;
    }
    
    int bufsize = combo.length - i;
    if (bufsize > 0) {
        if (buf == null || buf.length != bufsize) {
          buf = new double[bufsize];
        }
        System.arraycopy(combo, combo.length - bufsize, buf, 0, bufsize);
    } else {
        buf = null;
    }
    return result;
  }
};

@@ -690,40 +690,35 @@
     
-double%5B%5D%5B%5D result = new double%5B(
+int rows = (combo.length - 
 comb
@@ -729,93 +729,133 @@
 ngth
--
+ %25 
 wins
-z)/winshift+1%5D%5B%5D;%0A    int i = 0;%0A    int j=0;%0A    while (i+winsz %3C= combo.length
+hift - winsz +%0A          winshift)/ winshift;%0A    double%5B%5D%5B%5D result = new double%5Brows%5D%5B%5D;%0A%0A    for (int i=0;i%3Crows;++i
 ) %7B%0A
@@ -925,16 +925,25 @@
 combo, i
+*winshift
 , seg, 0
@@ -969,11 +969,9 @@
 ult%5B
-j++
+i
 %5D = 
@@ -979,27 +979,8 @@
 eg;%0A
-      i+=winshift;%0A
     
@@ -1019,17 +1019,31 @@
 ength - 
-i
+rows * winshift
 ;%0A    if

Update Java segmenter to use the same formula as the C version.