0

Suppose we have a 1D array named that consists of 9 elements: Source[0 to 8].

Using "Linear Interpolation" we want to shrink it into a smaller 4 point array: Destination [0 to 3].

This is how I understand the Algorithm:

  1. Calculate the ratio between both array lengths: 9/4 = 2.5
  2. Iterate over the destination coordinates and find the appropriate source coordinate:

Destination [0] = 0 * 2.5 = Source [0] -> Success! use this exact value.

Destination [1] = 1 * 2.5 = Source [2.5] -> No such element! Calculate the average of Source[2] and Source[3].

Destination [2] = 2 * 2.5 = Source [5] -> Success! use this exact value.

Destination [2] = 3 * 2.5 = Source [7.5] -> No such element! Calculate the average of Source[7] and Source[8].

Is this correct ?

duplode
  • 33,731
  • 7
  • 79
  • 150
shaiko
  • 159
  • 6

1 Answers1

1

Almost correct. 9/4 = 2.25. ;-)

Anyway, if you want to preserve the endpoint values, you should calculate the ratio as (9-1)/(4-1) = 2.666... (Between points 0, 1, 2, 3 there are only three segments, thus the length equals to 3. The same refers to 0...8).

If you don't hit the exact value, remember to compute a weigheted mean, e.g.

Destination[1] = 1 * 2.667 -> (3-2.667)*Source[2] + (2.667-2)*Source[3]

This is from the equation,

y = y0(x1-x) + y1(x-x0)

where, in this case,

x=2.66
x0=2
x1=3
y0=Source[2]
y1=Source[3]
Bartłomiej
  • 1,068
  • 1
  • 14
  • 23
  • Thanks for correcting me! Regarding what you said: "Anyway, if you want to preserve the endpoint values, you should calculate the ratio as (9-1)/(4-1)" So if there where 1024 points at the source and 512 points at the destination - the actual ratio should be: (1024-1) / (512-1) ~ 2.002 ? – shaiko Jan 22 '18 at 08:01
  • Yes, that is true. But in this case probably you should consider nearest-neighbor interpolation? If `i<255`, then `Dest[i]=Source[2*i]` and if `i>=255` then `Dest[i]=Source[2*i+1]` Pros: there is no new values and almost no computing. Cons: There is a gap in between. Another option: always `Dest[i]=Source[2*i]`. Then last elements don't fit (`Dest[511]==Source[1022]`). You must choose which interpolation is the best. – Bartłomiej Jan 22 '18 at 08:25
  • I'm designing an FPGA block that can do any m/n scaling... Therefore I'm looking for something generic and not tailored to specific problem conditions. – shaiko Jan 22 '18 at 13:29