0

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 array 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.

8
  • 4
    Yes, it's doable. But until you make your problem more specific, you won't get any answers; as-is, this question will most likely be closed for the same reason your last one was. Commented Sep 7, 2011 at 18:15
  • As an idea: For your new array, index 384 lies halfway between the old array's indexes 1 and 2, so it should contain a correspondingly weighted average of those two. Now just generalize that. Commented Sep 7, 2011 at 18:18
  • Yes, something like bins, i have 256 bins, each with some float value in 0 to 1 range, but now, i need 65536 bins, where each bin should have an interpolated or scaled value from the old 256 bins Commented Sep 7, 2011 at 18:21
  • @ildjarn, this is exactly my question!, how should interpolate or scale these values!. Thanks! Commented Sep 7, 2011 at 18:23
  • 1
    @ildj i dont know the answer :) Commented Sep 7, 2011 at 19:45

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's a linear interpolation?
@Duck that should see a doctor about the mooing, yes that is a linear interpolation.
3

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.

1 Comment

i believe what is he is asking is for a direct/non-skewed translation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.