# Does it really take 6 minutes and 20 seconds to properly fade-out a single precision audio signal?

Update

• This question is purely theoretical and should not account for any psychoacoustic effects, or practical implementations.
• Perhaps the question should be rephrased to "How long will it take to apply the smoothest possible fade, in a single precision system"?

A sine wave is generated within a 32-bit float system, with a sample rate of 44.1 kHz. The task in hand is to fade out this sine with as little digital distortion as possible (this is the meaning of 'properly'), without using dither. The maths below resolves to approximately 6 minutes and 20 seconds to do so. Is this correct?

# Axioms

• Digital audio within a 32-bit float system is represented with the values -1.0 to 1.0
• All the values within this range are normalized
• The sine wave peaks at 1.0 and -1.0
• The audio resolution is $$2^{25}$$, made of 23 bit mantissa + 1 implied (normalized) bit + 1 sign bit.
• The possible gain coefficients are $$2^{24}$$, with values ranging between 0.0 to 1.0

Any fade will create distortion due to quantisation of the gain values.

For instance, a fade from unity gain (1.0) to silence (0.0), if happens between two consecutive samples, is similar to 1-bit reduction (this will generate high and low frequency distortion correlated to the phase of the sine). If the fade is performed over 4 samples, there will be 4 gain steps; 8 samples mean 8 gain steps; and so forth.

It follows that the smoothest fade possible is that where between each sample the gain changes by the minimum possible amount, which is $$1/2^{24}$$. This gives $$2^{24}$$ gain steps.

$$2^{24} = 16,777,216$$

Meaning 16,777,216 samples are needed for the fade. If the system sample rate is 44,100 Hz:

$$16,777,216 / 44,100 = ~380.4$$ seconds.

Which equals approximately 6 minutes and 20 seconds.

This seems unreasonable. So where is the calculation wrong?

Just to further demonstrate what distortion I am talking about.

The original sine and its frequency analysis:

With a medium fade (50 samples):

• I don't think your calculations seem incorrect, but you're right that such a scheme is probably unreasonable. What application do you have that is so concerned with fading out so smoothly? Your sample rate seems to suggest that maybe you're doing audio processing. I can definitely say that you can fade out in a much shorter duration than 380 seconds and not yield any audible artifacts. If you're that sensitive to gain quantization, then you could consider implementing the gain in analog hardware. – Jason R Jul 9 '13 at 2:08
• Your calculation is correct, your definition of "properly" is is unreasonable, and your assumption that reducing the envelope of the amplitude by $1/2^{24}$ per time step actually minimizes any measure of "distortion" is incorrect. (For example: you would minimize "distortion" even less if you reduced the amplitude envelope by $1/2^{24}$ per 10 time steps.) Choose a fade-out time, then choose any reasonable smooth function that goes from 1 to 0 over the chosen fade-out time and multiply the smooth function by the signal. – Wandering Logic Jul 9 '13 at 2:16
• For a "smooth function" I would choose a function like $\cos ((\pi / 2T) t)$ where the fadeout starts at time $t = 0$ and ends at time $T$. It's smoother (and thus less "distorty") at time 0 and time $T$ than a linear ramp. – Wandering Logic Jul 9 '13 at 2:18
• Another option that might work well is the smoothstep and/or smootherstep function. It is designed to ease in smoothly at each end. – Jason R Jul 9 '13 at 2:38
• "as little digital distortion as possible (this is the meaning of 'properly'), without using dither" All signals have a noise floor (you should be adding dither to create one if you're synthesizing the signal digitally), and your distortion components will likely be hidden below the noise floor anyway. – endolith Jul 9 '13 at 16:51

You start off with wrong assumptions, so your result is nonsensical.

1. Your definition of good and bad should be based on some psychoacoustic understanding of what type if distortion and artifact is actually audible and/or objectionable
2. The 24-bit mantissa argument (it if were valid at all) would only hold a fixed point number. In floating point you can apply pretty much any gain you want. The smallest non-zero gain is actually 1.1755e-38.

There are various different ways to design a fade: linear, quasi-log, "half-window" functions, all kinds of continous derivative functions etc. They all sound slightly different and the best choice really depends on your application: musical fade, cross-fade between two signals, emergency fade out that needs to be fast, etc.

• +1 especially for the psychoacoustic note. This very important point is missing in almost all of those very technical audio dsp diskussions. In the end the auditorial cortex decides, not the math. – André Bergner Jul 9 '13 at 7:21
• I understand the call for psychoacoustic account, but the question is purely theoretical. To begin with, accounting for psychoacoustic effect would make this question very difficult to answer. Second, while a single gain stage may not yield anything audible, the accumulation of distortion from different stages may (and there is no way to determine how many stages there are); Should you apply dither when converting from 32-bit float (25 bit precision) to 24 bit integer (24 bit precision)? The distortion may be well bellow -120dB, but could accumulate to a level above this threshold. – Izhaki Jul 9 '13 at 9:30
• As for the second point - I stand corrected. The use of $2^{24}$ was indeed completely arbitrary. I have edited my question. – Izhaki Jul 9 '13 at 10:59
• Your are still making incorrect assumptions about the relationship between distortion and how many least-significant bits you subtract from the mantissa of any sample compared to how many bits you subtracted from the mantissa of the previous sample. You apply distortion-free gain/attenuation to a signal by multiplying each sample by exactly the same amount. For example: multiplying every sample by 1/2 would result in distortion free gain of 1/2. But that's not fading out, that's attenuation. You need to start with a mathematical definition of "distortion" that is relevant. – Wandering Logic Jul 9 '13 at 13:53
• @Izhaki: your statement "audio signal itself is quantized to the 225 mantissa precision" is not entirely correct. It's true that every sample as 25 bits of mantissa but the exponent changes throughout the sine wave so you have higher resolution at lower amplitudes then you have at +1. That's the whole point of floating point representation. You can reduce the gain be 2^-24 and STILL have 2^-24 relative precision (or 2^-48 with respect to +1). – Hilmar Jul 9 '13 at 16:08

Okay, let's try this again based on the edits: In order to determine the

"least distorted signal"

you have to first precisely define a distortion metric. A fading sine wave is by definition not a sine wave any more and has certain spectral properties which are related to the fading function (shape and length). This is completely independent of the quantization and would happen regardless of precision. How is an "undistorted faded sine wave" supposed to look like?

• Consider that the fade occurs over 4 samples, 2 to each side of the peak. The difference in level between the input samples is minute. But at the output, you get 1.0, 0.75, 0.5, 0.25, 0; Now you have clearly generated a high frequency distortion, that will also alias within the system. Is this incorrect? – Izhaki Jul 9 '13 at 16:25
• Every fade will create a deviation from an ideal sine wave. If it's short in time it's wide and frequency and vice versa. In your particular example, the fade is so short that the noise generate is almost white. It's too short to create any measurable aliasing, it's simply a very short white noise burst. – Hilmar Jul 9 '13 at 17:11
• Now we're getting somewhere! You haven't exactly created a high frequency distortion. You've multiplied the original signal by the envelope function. Multiplication in the time domain is convolution ("smearing") in the frequency domain. The only way to avoid the convolution is to multiply by a constant (i.e., don't do a fade.) You can, however, ask questions like, "given that I want the fade to happen over the course of 1 second, what function should I use for my envelope so that I minimize the variance of the convolution kernel in the frequency domain?" – Wandering Logic Jul 9 '13 at 17:20
• Now your question is well defined: the answer is "you should choose a length of infinity". To do the frequency analysis you actually need to take the fade-in into account as well. To minimize the width of the frequency domain convolution kernel the uncertainty principle says that you should multiply your time-domain signal by something that looks as much as possible as like a Gaussian. (Which of course, means that you can't ever totally fade out.) – Wandering Logic Jul 9 '13 at 22:53
• This whole question sounds like a case of premature optimization. Without some quantitative criteria to drive performance requirements of your overall system, you can't make very detailed decisions such as these. Engineering is about finding a reasonable solution that is "good enough" for your application. Except for very specialized applications, very extreme approaches like a 6-minute fade are usually a red flag that you're on the wrong track. – Jason R Jul 10 '13 at 0:02