Problem 118705 · easy · Level 01 Prerequisites & Setup

Turning Up a Quiet Recording

normalisation · scaling · absolute value · lists

Digital audio is stored as numbers between -1 and 1: anything outside that range is cut off ("clipped") and sounds harsh. A voice memo recorded too quietly only uses a small part of the range. Peak normalisation makes it louder without changing its shape: multiply every sample by the same gain so that the sample furthest from zero (positive or negative) ends up exactly at the level target. Sound engineers usually pick a target a little below 1, such as 0.9, to leave some headroom.

Write normalise(x, target) that returns the new list of samples. The peak of the signal is the largest absolute value |x[k]|; the gain is target / peak. A recording of pure silence (every sample 0) cannot be made louder: return a list of zeros of the same length.

Do not change x. Press Run with plot(x) and plot(normalise(x, 0.9)) to compare.

Examples

Input:  x = [0.1, -0.2, 0.05, 0.15], target = 0.8
Output: [0.4, -0.8, 0.2, 0.6]
Explanation: the peak is |-0.2| = 0.2, so the gain is 0.8 / 0.2 = 4.

Input:  x = [0, 0, 0], target = 0.9
Output: [0, 0, 0]

Constraints

  • answers are compared with a tolerance of 1e-6, so ints and floats are both fine

Goals

  • Find the peak of a signal as its largest absolute sample
  • Scale every sample by one gain so the peak lands on a target level
  • Handle a silent recording without dividing by zero
Starting Python…