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

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

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.

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

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,

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

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.

• Thanks Spencer, that's very helpful to explain the concept. I tried to work through it and I seem to have met 3 of the 4 conditions. The derivative and point match at x=0, but I can only get the point to match at x=f, not the derivative. I've gone through my work a few times and used online equation solvers to help check, but I'm still not sure what I've done wrong. Any further help? desmos.com/calculator/dg1eetwbzg
– mike
May 5, 2019 at 2:36
• Hope this helps. Its entirely possible that I may have made a minor error, so let me know if it doesn't work. May 5, 2019 at 3:26
• Thanks Spencer. Based on that, my error was in equation (2) I had equated the derivative of the cubic with the derivative of f(f)=c^(d-f), which makes no sense. But unfortunately, your solution does not seem to work fully either. I'm not sure if it is a problem with your equation (2) perhaps as well, as it also doesn't make sense to me. You're saying the rate of change of the cubic (derivative) should equal the y value of the exponential at x=f. But I don't think that seems correct either, is it? Why would those two things be equal? Here's yours: desmos.com/calculator/ctzqomlard
– mike
May 5, 2019 at 4:04
• We are matching the slopes at the point $x=f$. The derivative (slope) of an exponential is itself, there is an extra negative sign because of the chain rule. May 5, 2019 at 4:11
• Thanks Spencer. Yeah - right - I just looked up some more basics on derivatives and tangents - It's been 20 years since I learned this stuff in high school (and I haven't touched it since until now)! In principle it makes sense then. But so why isn't it working?
– mike
May 5, 2019 at 4:13