Interpolation over an array (or two)

后端 未结 8 628
闹比i
闹比i 2020-12-10 15:27

I\'m looking for a java library or some help to write my own interpolation function. That is I have two arrays of doubles which are potentially different sizes, but are ord

8条回答
  •  Happy的楠姐
    2020-12-10 15:57

    Light-weight version of one-dimensional array linear interpolator:

    public static float[] interpolate(float[] data) {
        int startIdx = -1;
        float startValue = 0f;
        float element;
        for (int i = 0; i < data.length - 1; i++) {
            element = data[i];
            if (element != 0f) {
                if (startIdx != -1) {
                    doInterpolate(startValue, element, startIdx + 1, i - startIdx - 1, data);
                }
                startValue = element;
                startIdx = i;
            }
        }
        return data;
    }
    
    private static void doInterpolate(float start, float end, int startIdx, int count, float[] data) {
        float delta = (end - start) / (count + 1);
        for (int i = startIdx; i < startIdx + count; i++) {
            data[i] = start + delta * (i - startIdx + 1);
        }
    }
    

提交回复
热议问题