A sound signal goes from a microphone through a chain of stages to a speaker: a preamplifier, a long cable, a volume control, a power amplifier. Each stage multiplies the signal by some gain ratio. Data sheets give that ratio in two ways:
- an amplitude ratio (output voltage / input voltage), whose gain in decibels is
20 * log10(ratio), - a power ratio (output power / input power), whose gain in decibels is
10 * log10(ratio).
The two formulas agree, because power grows with the square of amplitude: an amplitude ratio of 10 is a power ratio of 100, and both are +20 dB. Decibels are handy because the gains of stages in a row simply add up. A negative number of decibels means the stage makes the signal smaller.
Each stage is a pair (kind, ratio) with kind either "amplitude" or "power". Write chain_gain(stages) that returns a tuple of three numbers for the whole chain:
- the total gain in dB (the sum of the stage gains; an empty chain has
0dB), - the overall amplitude ratio,
10 ** (total_db / 20), - the overall power ratio,
10 ** (total_db / 10).
Use math.log10.
Examples
Input: stages = [("amplitude", 10), ("power", 0.5), ("amplitude", 2)]
Output: (23.010299956639813, 14.142135623730951, 200.00000000000003)
Explanation: +20 dB, then -3.0103 dB (halving the power), then +6.0206 dB.
The voltage grows 10 * 2 * sqrt(0.5) = 14.14 times and the power 200 times.
Input: stages = [("power", 100), ("amplitude", 0.1)]
Output: (0.0, 1.0, 1.0)
Explanation: +20 dB, then -20 dB: the second stage undoes the first.
Constraints
- every
ratiois positive (a logarithm needs a positive number) - answers are compared with a tolerance of
1e-6
Goals
- Convert an amplitude ratio to decibels with 20 log10 and a power ratio with 10 log10
- Add gains in decibels where the ratios would be multiplied
- Convert a total in decibels back to an amplitude ratio and a power ratio