How can I create a cubic spline to connect these two line segments?

938 Views Asked by At

I am trying to create an audio envelope smoothing curve to smooth a steady signal (y=1) as it goes into exponential decay (y=c^-x).

I have expressed the graphs of the two line segments I am trying to connect in terms of the variables I need here:

https://www.desmos.com/calculator/85lw0cmyw7

cubic spline needed

In this case, how would I connect the two segments with a cubic spline between x=0 and x=f?

I am trying to smooth between y = 1 and y = c ^ (d - x) over this range.

1

There are 1 best solutions below

14
On BEST ANSWER

The cubic equation will be,

$$y = Ax^3+Bx^2+Cx+D,$$

we need to determine the coefficients $A,B,C,$ and $D$. These will be determined by the two functions we are trying to join together. Our cubic will have to match their values, and the values of their derivatives at the boundary points.

Here is our overall function,

$$ f(x) = \begin{cases} 1 \qquad x \leq 0 \\ Ax^3+Bx^2+Cx+D \qquad 0 < x<f\\ c^{-(x-d)} \qquad x \geq f \end{cases} $$

First we enforce continuity.

$$ \lim_{x\rightarrow 0^-} f(x) = \lim_{x\rightarrow 0^+} f(x)$$

$$ \lim_{x\rightarrow f^-} f(x) = \lim_{x\rightarrow f^+} f(x)$$

$$ \lim_{x\rightarrow 0^-} f'(x) = \lim_{x\rightarrow 0^+} f'(x)$$

$$ \lim_{x\rightarrow f^-} f'(x) = \lim_{x\rightarrow f^+} f'(x)$$

these equations need to be solved for $A,B,C,$ and $D$.


It's relatively easy to show that $D=1$ and $C=0$.

We will work through the calculation to solve for $A$ and $B$.

$$ \lim_{x\rightarrow f^-} f(x) = \lim_{x\rightarrow f^+} f(x)$$

$$ \lim_{x\rightarrow f^-} \Big( Ax^3+Bx^2+Cx+D \Big) = \lim_{x\rightarrow f^+} \Big( c^{-(x-d)} \Big)$$

$$ Af^3+Bf^2+Cf+D = c^{-(f-d)} $$

$$ Af^3+Bf^2+0f+1 = c^{d-f} $$

$$ \boxed{Af^3+Bf^2 = c^{d-f}-1} \quad \textbf{(1)}$$

$$ \lim_{x\rightarrow f^-} f'(x) = \lim_{x\rightarrow f^+} f'(x)$$

$$ \lim_{x\rightarrow f^-} \Big(3Ax^2+2Bx+C\Big) = \lim_{x\rightarrow f^+} \Big( - \ln(c) c^{-(x-d)}\Big)$$

$$ 3Af^2+2Bf+C = - \ln(c) c^{-(f-d)}$$

$$ 3Af^2+2Bf+0 = - \ln(c)c^{d-f}$$

$$ \boxed{3Af^2+2Bf = -\ln(c) c^{d-f}}\quad \textbf{(2)}$$


To avoid potential errors I used wxMaxima to solve the equations,

enter image description here


I then checked the results using a plotting software called gnuplot. The following is for $c=5$ and $f=0.76$.

enter image description here

The code to make this plot is here :

d=0.4
f=0.76
c=5

F(x) = c**(-(x-d))
G(x) = 1

A = - (c**d * log(c) * f - 2 * c**f + 2 * c**d)/(c**f * f**3)
B = (c**d * log(c) * f - 3 * c**f + 3 * c**d ) / (c**f * f **2 ) 

C = 0 
D = 1

P(x) = A*x**3+B*x**2+C*x+D

piecewise(x) = x < 0? G(x) : x <= f ? P(x) :  F(x) 
set xrange[0:f+1]
set yrange[0:c**(d)]

plot piecewise(x), c**(-(x-d))

Here is a link to the updated Desmos graph : https://www.desmos.com/calculator/kt2xzjwbui

You should be aware that wherever I have "log(c)" in the wxMaxima or gnuplot code that this refers to the natural logarithm.