From 2c10dfc7aa259ad0fd5f3eab8879e870e77fd667 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 6 Apr 2014 00:44:00 +0200 Subject: m + fix cubic interpolation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- TODO | 2 ++ src/interpolation.py | 11 +++++++---- test/cubic_interpolation | 15 ++++++++++----- test/linear_interpolation | 5 ++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index c5d0d16..3ed5a10 100644 --- a/TODO +++ b/TODO @@ -15,6 +15,8 @@ Low priority: https://en.wikipedia.org/wiki/Monotone_cubic_interpolation https://en.wikipedia.org/wiki/Spline_interpolation https://en.wikipedia.org/wiki/Lanczos_resampling + https://en.wikipedia.org/wiki/Kochanek–Bartels_spline + https://en.wikipedia.org/wiki/Stairstep_interpolation Use curve sizes returned from RandR/VidMode/... Use Robertson's method diff --git a/src/interpolation.py b/src/interpolation.py index 0d16838..e81c93c 100644 --- a/src/interpolation.py +++ b/src/interpolation.py @@ -47,13 +47,14 @@ def linearly_interpolate_ramp(r, g, b): return (R, G, B) -def cubicly_interpolate_ramp(r, g, b): +def cubicly_interpolate_ramp(r, g, b, tension = 0): # TODO demo and document tension parameter ''' Interpolate ramps to the size of the output axes using cubic Hermite spline @param r:list The red colour curves @param g:list The green colour curves @param b:list The blue colour curves + @param tension:float A [0, 1] value of the tension @return :(r:list, g:list, b:list) The input parameters extended to sizes of `o_size`, or their original size, whatever is larger. ''' @@ -61,8 +62,8 @@ def cubicly_interpolate_ramp(r, g, b): R, G, B = C(r), C(g), C(b) # Basis functions h00 = lambda t : (1 + 2 * t) * (1 - t) ** 2 - h01 = lambda t : t * (1 - t) ** 2 - h10 = lambda t : t ** 2 * (3 - 2 * t) + h10 = lambda t : t * (1 - t) ** 2 + h01 = lambda t : t ** 2 * (3 - 2 * t) h11 = lambda t : t ** 2 * (t - 1) def tangent(values, index, last): ''' @@ -77,6 +78,8 @@ def cubicly_interpolate_ramp(r, g, b): if index == 0: return values[1] - values[0] if index == last: return values[last] - values[last - 1] return (values[index + 1] - values[index - 1]) / 2 + # Tension coefficent + c_ = 1 - tension # Interpolate each curve for small, large in ((r, R), (g, G), (b, B)): small_, large_ = len(small) - 1, len(large) - 1 @@ -90,7 +93,7 @@ def cubicly_interpolate_ramp(r, g, b): # Points pj, pk = small[j], small[k] # Tangents - mj, mk = tangent(small, j, small_), tangent(small, k, small_) + mj, mk = c_ * tangent(small, j, small_), c_ * tangent(small, k, small_) # Interpolation large[i] = h00(w) * pj + h10(w) * mj + h01(w) * pk + h11(w) * mk ## Check local monotonicity diff --git a/test/cubic_interpolation b/test/cubic_interpolation index 249aa89..6d2b22c 100755 --- a/test/cubic_interpolation +++ b/test/cubic_interpolation @@ -12,13 +12,16 @@ print('Loading matplotlib.pyplot...') import matplotlib.pyplot as plot print('Done loading matplotlib.pyplot') +from math import * + def main(): # Create a page with graphs fig = plot.figure() # Add graphs - add_graph(fig, 111, [i / 15 for i in range(16)]) + add_graph(fig, 211, [i / 15 for i in range(16)]) + add_graph(fig, 212, [sin(6 * i / 15) for i in range(16)]) # Show graphs plot.show() @@ -45,7 +48,7 @@ def add_graph(fig, graph_pos, input_values): graph.plot([i / (n - 1) for i in range(n)], input_values, 'ro') -def interpolate(small): +def interpolate(small, tension = 0): ''' Interpolate data @@ -56,8 +59,8 @@ def interpolate(small): small_, large_ = len(small) - 1, len(large) - 1 # Basis functions h00 = lambda t : (1 + 2 * t) * (1 - t) ** 2 - h01 = lambda t : t * (1 - t) ** 2 - h10 = lambda t : t ** 2 * (3 - 2 * t) + h10 = lambda t : t * (1 - t) ** 2 + h01 = lambda t : t ** 2 * (3 - 2 * t) h11 = lambda t : t ** 2 * (t - 1) def tangent(values, index, last): ''' @@ -72,6 +75,8 @@ def interpolate(small): if index == 0: return values[1] - values[0] if index == last: return values[last] - values[last - 1] return (values[index + 1] - values[index - 1]) / 2 + # Tension coefficent + c_ = 1 - tension for i in range(len(large)): # Scaling j = i * small_ / large_ @@ -80,7 +85,7 @@ def interpolate(small): # Points pj, pk = small[j], small[k] # Tangents - mj, mk = tangent(small, j, small_), tangent(small, k, small_) + mj, mk = c_ * tangent(small, j, small_), c_ * tangent(small, k, small_) # Interpolation large[i] = h00(w) * pj + h10(w) * mj + h01(w) * pk + h11(w) * mk return large diff --git a/test/linear_interpolation b/test/linear_interpolation index ae622cc..140616c 100755 --- a/test/linear_interpolation +++ b/test/linear_interpolation @@ -14,13 +14,16 @@ print('Loading matplotlib.pyplot...') import matplotlib.pyplot as plot print('Done loading matplotlib.pyplot') +from math import * + def main(): # Create a page with graphs fig = plot.figure() # Add graphs - add_graph(fig, 111, [i / 15 for i in range(16)]) + add_graph(fig, 211, [i / 15 for i in range(16)]) + add_graph(fig, 212, [sin(6 * i / 15) for i in range(16)]) # Show graphs plot.show() -- cgit v1.2.3-70-g09d2