I use a variation of a 5-cross median filter on image data on a small embedded system, i.e.
x
x x x
x
The algorithm is really simple, read 5 values, get the highest 2 and do some calculations on those and write back the result.
The 5 input values are all in the range of 0-20. The calculated value are also in the 0-20 range!
I'm trying to figure out if I can use a lookup-table to speed things up but I have failed to generate a key that I can use.
As an example, considering if the input was in the range of 0-5, one way of generating a unique key would be to take the binary representation and just concatenate the numbers, i.e.
101 101 101 101 101
key = x[0] | x[1] << 3 | x[2] << 6 | x[3] << 9 | x[4] << 12
But that LUT is huge, ~23k items.
Since [5,0,0,0,5]
is the same as [5,0,5,0,0]
one simplification
could be to use 2 LUTs,
LUT1 = [0, 1, 6, 31, 156, 781]
Where each items is 1 more than the maximum sum of 5 of the previous elements
Then the key could be calculated as (using python syntax)
key = sum([LUT1[x[0]], LUT1[x[1]], LUT1[x[2]],
LUT1[x[3]], LUT1[x[4]a]])
But again, this approach doesn't scale up to the range 0-20 for each element.
Using a sorting network as described in Fastest sort of fixed length 6 int array doesn't improve the performance; I'm only interested in the 2 highest values.
So, is it possible to create a unique key from five positive integers in the range of 0-20 that can be used as index into a LUT?