I'm stuck on a developing issue and desperately looking for some help or ideas.

**The setup:**- I have an array of x degree-values (each of them has a value between 0 and 360).
- My goal is to 'normalize' each of the values using its two neighbors. To get more 'smoothe' degree values.

Don't know if this is clear to everyone. I can't explain it better. :/ So here's an

**Example:**
Let's say an array is

Lua Code:

degrees = {

[1] = 39,

[2] = 25,

[3] = 18,

[4] = 6,

[5] = 353,

[6] = 320,

[7] = 333,

[8] = 342,

[9] = 338,

[10] = 318,

[11] = 350,

[12] = 359,

[13] = 340,

[14] = 329,

[15] = 320,

[16] = 313

}

I'm starting my 'normalization' with the second value ([2]). (The first one is my reference and is not modified.)

Lua Code:

[1] = 38,

[2] = 25,

[3] = 18,

I'm calculating a new 'normalized' value for [2] using the value of [1] and [3]:

*38 - 18 / 2 = 10*

38 - 10 = 28

So the normalized value of [2] is 28

Now I'm doing the same with the remaining values.

That means, the next step would be to normalize value [3]

Lua Code:

[2] = 25,

[3] = 18,

[4] = 6,

which is

*25 - 6 / 2 = 9.5*

25 – 9.5 = 15.5

Normalized value of [3] = 15.5

(To be accurate: the value of [2] would be 28 and not 25 as it was normalized it in the previous step. But to keep the example as simple as possible I'm using the original value.)

Works nice so far. But now it's starting to get dirty. The next value is

Lua Code:

[3] = 18,

[4] = 6,

[5] = 353,

So I'll have to consider the 0 threshold with this one.

*18 + (360 - 353) / 2 = 12.5*

18 - 12.5 = 5.5

And the next value makes it really worse:

Lua Code:

[4] = 6,

[5] = 353,

[6] = 320,

Which translates into

*6 + (360 - 320) / 2 = 23*

360 + (6 – 23) = 343

**The Problem**
The degree values could be in any order/sequence. The array could be reversed for example. The values could be decreasing, then increasing and decreasing again. They could pass the 0 threshold never, once or multiple times. :/

I just can't imagine a way or an algorithm to do what I want.

I really would appreciate any idea, hint, or approach that helps me solve this development issue.

How would you do it?

btw: Thanks a lot for reading all the way down to this.