Fade in/out math
Jan 11, 2024 at 8:32 AM Thread Starter Post #1 of 10

71 dB

Headphoneus Supremus
Joined
Sep 17, 2017
Posts
2,818
Likes
1,031
Location
Helsinki, Finland
Generating a linear fade in of length T for a signal happens by multiplying the signal with function

f(t) = k*t, t ∈ [0, T]
f(t) = 0, t < 0
f(t) = 1, t > T

where k is the inverse of the length of the fade in (k = 1/T) and at the starting point of the fade in t = 0.

fadein1.png


Logarithmic fade in/out is more natural than linear fade in/out, because they are linear on logarithmic scale and especially logarithmic fade out follows the way reverberant sound decay. Here is -17.4 dB => 0 dB logarithmic fade in:

f(t) = exp(2*k*t)/exp(2), t ∈ [0, T]
f(t) = 0, t < 0
f(t) = 1, t > T

fadein2.png


The (theoretical) problem with these fade ins/outs is that the derivative of the fade in curve doesn't exist in the beginning and the end of the fade in/out. The sharp corners are theoretically illegal in digital audio, because they require infinite bandwidth. To fix this, the curves needs to be modified so that the derivate exists everywhere eliminating the need for infinite bandwidth. Since the derivative before and after the fade in/out section is clearly zero, the derivative of the fade in/out curve needs to also be zero at these points. Lets use scaled time variable x = k*t so that the scaled length of the fade in/out is 1. We can create the derivative for fixed "linear" fade in as:

f'(x) = 𝛂(x)(x-1) = 𝛂(x² - x)

where 𝛂 in a scaling constant. f'(x) clearly is zero when x = 0 or x = 1 satisfying our goal. Now the fixed linear fade in curve is the integral of f'(x):

f(x) = ∫f'(x)dx = 𝛂∫(x² - x)dx = 𝛂(x³/3 - x²/2) + C

The integration constant C can be determined by setting

f(0) = 0

giving us C = 0. For calculating scaling constant 𝛂, we set

f(1) = 1

giving us 𝛂 = -6.

Hence our fixed linear fade in function is

f(x) = 3x² - 2x³

It looks like this:

fadein3.png


Only the blue 0 to 1 part of it is used. Using this fixed linear fade in we can create fixed logarithmic fade in

f(x) = exp( 𝛽 * ( 3x² - 2x³ ) ) / exp( 𝛽 )

where 𝛽 determines the amount of fade in (about 8.7*𝛽 dB). In the graph below 𝛽=2 (17.4 dB fade in) and the gray dotted line shows the normal logarithmic fade in for comparison:

fadein4.png
 
Last edited:
Jan 20, 2024 at 12:03 PM Post #2 of 10
I came up with a function softramp(t,n) to soften the default ramp function ramp(t):

rampmath.png


Function softramp(t,n) is defined using function soften(t,n) doing the work that is basically bending the ramp curve horizontal (derivative = 0) at the beginning (x=0) and the end (x=1). The variable t is time scaled to make the length of the ramp 1 and the variable n ∈ ℕ is used to adjust the softness of the curve; n=1 makes the curse very soft and as n ⟶ ∞, the curve gets closer and closer to the default ramp. Examples of n=2 and n=7 below:

ONE.png
 
Feb 9, 2024 at 3:40 PM Post #3 of 10
Of course the shape of the fade is just one consideration. The modulation of the music during that time matters too. A mixer might alter the shape of the fade to compensate for very low or very high volume sound in one particular part of it. And the length of fades is a subjective judgement call based on the structure of the song- for instance you might want the resolving of a verse to be audible under the fade.
 
Feb 11, 2024 at 10:33 AM Post #4 of 10
Of course the shape of the fade is just one consideration. The modulation of the music during that time matters too. A mixer might alter the shape of the fade to compensate for very low or very high volume sound in one particular part of it. And the length of fades is a subjective judgement call based on the structure of the song- for instance you might want the resolving of a verse to be audible under the fade.
You are talking about the artistical side of fading (which of course is important) while I have been talking about a mathematical aspect related to the "legality" of digital signals. Fading is about multiplying the signal with a fading curve. The product of a "legal" digital signal and an "illegal" digital signal is "illegal." My point is we have to be careful with the starting and ending points of fading curves and make sure the derivative at these points exists (and is in pracise zero).
 
Feb 11, 2024 at 11:01 AM Post #5 of 10
Yes, it’s an aesthetic thing, as is the shape, with “slow ins and slow outs”.
 
Feb 12, 2024 at 3:25 PM Post #6 of 10
Oh yeah, the aesthetic sound of aliasing every time a volume fader is automated! I'm sure it's been knowingly employed by countless brilliant producers, I just can't remember any of them right now.
 
Feb 12, 2024 at 3:41 PM Post #7 of 10
I’m afraid I don’t know what you’re talking about. I’m talking about the shape of fades. It’s the same as with dissolves in video. A straight diagonal line doesn’t sound good, so you speed up in the middle and slow down on the ends. That’s called slow ins- slow outs.
 
Feb 12, 2024 at 9:43 PM Post #8 of 10
I’m afraid I don’t know what you’re talking about. I’m talking about the shape of fades. It’s the same as with dissolves in video. A straight diagonal line doesn’t sound good, so you speed up in the middle and slow down on the ends. That’s called slow ins- slow outs.
Ease in and outs are used in lots of different effects for video/animation. They're all similar in that there's a time graph with control points. Seems the discussion is what mathematical "presets" you could employ for generating the spline between one point to another (in this case fade in gain). I mainly use Maya for 3D animation and AfterEffects for comping/video effects. They both let you manage whatever effect over time with presets on the control points (shaped like 71 dB's examples), manually shaping them with spline tools, or get into your own formulas. Scripting for repeating an effect, doing certain randomizations, or generating procedural simulations are more advanced uses of the software. They also let you add time control splines to audio effects as well (IE, fade in gain, stereo separation, pitch, etc). As for what is "legal" zero in a control point: in practice I'm not familiar with it in my software. There are examples like transforms that can be near infinite + or - values (or at least to floating point). Or the software will round numbers for a given effect (such as animating 0-255 in standard RGB shader, or max gain with audio).
 
Last edited:
Feb 12, 2024 at 11:06 PM Post #9 of 10
The problem with programmed fades is that you might have drum beats or gaps in sound that make it not transition well.
 
Feb 12, 2024 at 11:10 PM Post #10 of 10
The problem with programmed fades is that you might have drum beats or gaps in sound that make it not transition well.
Well 71 dB was talking about the mathematical approaches between smooth in/outs with two control points. Seems another part you're indicating here is the artistic side of the application: where in the timing do you set the in and out control point (that is still something you can set with programmed presets in the software I'm familiar with).
 
Last edited:

Users who are viewing this thread

Back
Top