/** Get median of values within kernel-sized neighborhood. Kernel size kNPoints should be odd. */ private static float getMedian(float[] cache, int xCache0, int[] kernel, float[] aboveBuf, float[]belowBuf, int kNPoints, float guess) { int nAbove = 0, nBelow = 0; for (int kk=0; kk<kernel.length; kk++) { for (int p=kernel[kk++]+xCache0; p<=kernel[kk]+xCache0; p++) { float v = cache[p]; if (v > guess) { aboveBuf[nAbove] = v; nAbove++; } else if (v < guess) { belowBuf[nBelow] = v; nBelow++; } } } int half = kNPoints/2; if (nAbove>half) return findNthLowestNumber(aboveBuf, nAbove, nAbove-half-1); else if (nBelow>half) return findNthLowestNumber(belowBuf, nBelow, half); else return guess; }
/** Get median of values within kernel-sized neighborhood. Kernel size kNPoints should be odd. */ private static float getMedian(float[] cache, int xCache0, int[] kernel, float[] aboveBuf, float[]belowBuf, int kNPoints, float guess) { int nAbove = 0, nBelow = 0; for (int kk=0; kk<kernel.length; kk++) { for (int p=kernel[kk++]+xCache0; p<=kernel[kk]+xCache0; p++) { float v = cache[p]; if (v > guess) { aboveBuf[nAbove] = v; nAbove++; } else if (v < guess) { belowBuf[nBelow] = v; nBelow++; } } } int half = kNPoints/2; if (nAbove>half) return findNthLowestNumber(aboveBuf, nAbove, nAbove-half-1); else if (nBelow>half) return findNthLowestNumber(belowBuf, nBelow, half); else return guess; }
/** Get median of values within kernel-sized neighborhood. * NaN data values are ignored; the output is NaN only if there are only NaN values in the * kernel-sized neighborhood */ private static float getNaNAwareMedian(float[] cache, int xCache0, int[] kernel, float[] aboveBuf, float[]belowBuf, int kNPoints, float guess) { int nAbove = 0, nBelow = 0; for (int kk=0; kk<kernel.length; kk++) { for (int p=kernel[kk++]+xCache0; p<=kernel[kk]+xCache0; p++) { float v = cache[p]; if (Float.isNaN(v)) { kNPoints--; } else if (v > guess) { aboveBuf[nAbove] = v; nAbove++; } else if (v < guess) { belowBuf[nBelow] = v; nBelow++; } } } if (kNPoints == 0) return Float.NaN; //only NaN data in the neighborhood? int half = kNPoints/2; if (nAbove>half) return findNthLowestNumber(aboveBuf, nAbove, nAbove-half-1); else if (nBelow>half) return findNthLowestNumber(belowBuf, nBelow, half); else return guess; }
/** Get median of values within kernel-sized neighborhood. * NaN data values are ignored; the output is NaN only if there are only NaN values in the * kernel-sized neighborhood */ private static float getNaNAwareMedian(float[] cache, int xCache0, int[] kernel, float[] aboveBuf, float[]belowBuf, int kNPoints, float guess) { int nAbove = 0, nBelow = 0; for (int kk=0; kk<kernel.length; kk++) { for (int p=kernel[kk++]+xCache0; p<=kernel[kk]+xCache0; p++) { float v = cache[p]; if (Float.isNaN(v)) { kNPoints--; } else if (v > guess) { aboveBuf[nAbove] = v; nAbove++; } else if (v < guess) { belowBuf[nBelow] = v; nBelow++; } } } if (kNPoints == 0) return Float.NaN; //only NaN data in the neighborhood? int half = kNPoints/2; if (nAbove>half) return findNthLowestNumber(aboveBuf, nAbove, nAbove-half-1); else if (nBelow>half) return findNthLowestNumber(belowBuf, nBelow, half); else return guess; }