简体   繁体   English

浮点数组重采样

[英]Float array resampling

I want to "stretch" a one-dimensional float array into a bigger array. 我想将一维浮点数“拉伸”到一个更大的数组中。

//expected behaviour

float[] initialArray = {2.0, 6.5, 2.0}
float[] biggerArray = resample(initialArray, 7 /*new size*/)

//output: {2.0, 3.5, 5.0, 6.5, 5.0, 3.5, 2.0}

The new values should propobaly be calculated from linear interpolation of the previous array values but i can't figure out how to achieve that. 新值应该通过先前数组值的线性插值计算,但我无法弄清楚如何实现这一点。

Any hint ? 任何提示?

Lets the length of a source array is N , and the length of a destination array is M where N < M and N > 1 . 让源数组的长度为N ,目标数组的长度为M ,其中N < MN > 1

You can calculate the new index of the source i -th element by the formula: 您可以通过以下公式计算源i元素的新索引:

j = i * (M - 1)/(N - 1);

When i == 0 then j == 0 ; i == 0j == 0 ; and when i == N - 1 then j == M - 1 . i == N - 1然后j == M - 1 The external loop can looks like this: 外部循环可能如下所示:

float[] source = ...;
float[] destination = ...;

destination[0] = source[0];
for (int i = 1; i < source.Length; i++)
{
    int j = i * (destination.Length - 1)/(source.Length - 1);
    destination[j] = source[i];
    // interpolation
}

To interpolation you should calculate intermediate values for each pair (source[i - 1], source[i]) . 要进行插值,您应该计算每对的中间值(source[i - 1], source[i]) You'll need to store previous value of j : 您需要存储以前的j值:

destination[0] = source[0];
int jPrevious = 0;
for (int i = 1; i < source.Length; i++)
{
    int j = i * (destination.Length - 1)/(source.Length - 1);
    Interpolate(destination, jPrevious, j, source[i - 1], source[i]);

    jPrevious = j;
}

private static void Interpolate(float[] destination, int destFrom, int destTo, float valueFrom, float valueTo)
{
    int destLength = destTo - destFrom;
    float valueLength = valueTo - valueFrom;
    for (int i = 0; i <= destLength; i++)
        destination[destFrom + i] = valueFrom + (valueLength * i)/destLength;
}

You can use List<float> : 您可以使用List<float>

float[] initialArray = { 2.0f, 6.5f, 2.0f };
List<float> initialArrayTemp = ToListFloat(initialArray);

private List<float> ToListFloat(float[] array)
        {
            List<float> list = new List<float>();
            for (int i = 0; i < array.Length; i++)
            {
                list.Add(array[i]);
            }
            return list;
        }

Now your array is a dynamic array and you can add your new nodes anywhere of your Array by using Insert() method. 现在,您的数组是一个动态数组,您可以使用Insert()方法在Array的任何位置添加新节点。

As soon as you need a new static array, use below: 只要您需要新的静态数组,请使用以下命令:

float[] newInitialArray = initialArrayTemp.ToArray();

This one works for both if source size is larger than destination and vice versa. 如果源大小大于目标,则这个适用于两者,反之亦然。

    private double[] Resample(double[] source, int n)
    {
        //n destination length
        int m = source.Length; //source length
        double[] destination = new double[n];
        destination[0] = source[0];
        destination[n-1] = source[m-1];

        for (int i = 1; i < n-1; i++)
        {
            double jd = ((double)i * (double)(m - 1) / (double)(n - 1));
            int j = (int)jd;
            destination[i] = source[j] + (source[j + 1] - source[j]) * (jd - (double)j);
        }
        return destination;        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM