开发者

Scale values from a smaller array into a bigger array

开发者 https://www.devze.com 2023-04-03 02:22 出处:网络
I have an array, whose size is 256. This array has elements which are in 0 to 1 range. Now, i need an array whose size should be 65536, where each element should be interpolated or scaled from the ori

I have an array, whose size is 256. This array has elements which are in 0 to 1 range. Now, i need an array whose size should be 65536, where each element should be interpolated or scaled from the original array of 256 values. I asked in another thread, but i wasnt clear enough :(

Some kind of mapping where, lets say, 10 values from the original image should match to more number values in the bigger array. Is it doable?

EDIT : lets say i have an arr开发者_运维百科ay which is 256 floats, each float is in 0 to 1 range. Now, i need a new array, which should be 65536 floats again each float in 0 to 1 range. These new float values should be interpolated or scaled from original 256 floats.


Disclaimer: My C++ isn't so great these days. This will look something more like Java. I'm assuming you weren't planning on copying and pasting anyway: knowing the algorithm is much more important then the implementation language.

The following algorithm does NOT make 65536 entries for a 256 entry input. Instead, it makes 65281. The reason for this is you need to scale FROM something TO something else. Consider a fence with 256 posts. You'll only have 255 sections of fence. Each scaling corresponds to a section of fence. For simple math, I make the number of samples between each original sample equal to the number of original samples. So if you put a 4 entry input, you'll get a 13 entry output. A 100 entry input will make a 9901 entry output.

Enjoy!

std::vector<double> sample(std::vector<double> orig) {

    std::vector<double> result(orig.size() * (orig.size() - 1) + 1);

    /* for each value, make that many values scaled between this and the last piece */
    for(int i = 0; i < orig.size() - 1; i++) {
        double last = orig[i];
        double curr = orig[i+1];
        double diff = curr - last;
        double step = diff / orig.size();
        int offset = i * orig.size();
        for(int j = 0; j < orig.size(); j++) {
            result[offset + j] = last + (step * j);
        }
    }
    result[result.size() - 1] = orig[orig.size() - 1];
    return result;
}


It sounds like you're asking about sample rate conversion. How best to do that depends on the meaning of your data. For example if your array represented a sound wave, you must take care not to introduce harmonic distortion, which would happen if you used linear interpolation.

The Wikipedia article on interpolation should give you some starting points.

0

精彩评论

暂无评论...
验证码 换一张
取 消