Skip to content

Lesson 2 of 315 min Essential path

Operations on amplitude

Scale, offset, add, multiply, difference and clip a signal's values, and see why differencing and summing undo each other.

Before thisKinds of signals (1.2)

Before this1.2
Chapter 2 · Lesson 2 of 3

First, the picture

Drag Loudness below zero, then bring it back and drag Baseline instead, and watch what each one does to the shape.

Louder, quieter, and shifted

The faint stems are the original sequence, unchanged.

original x[n]result y[n]
The result
y[n] = 1.4 · x[n] + 0.0
1.4×
0.0
Describe this picture

One plot of a 32-sample sequence, amplitude against sample number: the original x[n]x[n] as faint stems and the result y[n]y[n] over it. The “Loudness” slider, the gain aa, runs from −2-2 to 2; the “Baseline” slider, the offset bb, runs from −1-1 to 1. The readout “The result” shows the formula for the current settings, starting at y[n]=1.4⋅x[n]+0.0y[n] = 1.4 \cdot x[n] + 0.0.

The values, not the timing

In Shifting, reversing and scaling time (2.1), I moved a signal around in time: I slid it, stretched it, reversed it, but I never touched what it measured at each moment. Here I do the opposite. I leave every sample’s timing alone and change its value instead: how loud it is, what happens when two signals add, what happens when a signal runs into a limit. That makes seven small operations, and you already use most of them without a name for them.

Louder, quieter, and shifted

Turn a volume knob and a song gets louder or quieter, but it still sounds like the same song, bigger or smaller. Now picture a bathroom scale that is a little off, always reading one kilogram heavy no matter what is on it. Fixing that scale means sliding every reading down by one kilogram, not stretching or shrinking anything.

Those are the two simplest things you can do to a signal’s values. Multiplying every sample by a number aa (the gain) makes the whole trace bigger or smaller, and if aa is negative it also flips the trace upside down around zero. Adding a constant bb (the offset, the same “DC” idea from How big is a signal (1.3)) slides the whole trace up or down without changing its shape. Put together:

y[n]=a⋅x[n]+by[n] = a \cdot x[n] + b

Here x[n]x[n] is the original signal’s value at sample nn, aa is the gain you multiply by, bb is the constant you add, and y[n]y[n] is the result.

In the picture at the top of the page, Loudness is the gain aa and Baseline is the offset bb. Loudness stretches, shrinks, or flips the trace around zero, while Baseline slides the whole trace up or down, leaving its up-and-down shape as it was.

Adding two signals together

Two people talk at once near a microphone. The microphone can’t tell the voices apart: at every instant it records however much the air pressure has changed, and that is the two voices’ pushes added together.

Adding two signals works the same way: at each sample, you add the two values.

y[n]=x1[n]+x2[n]y[n] = x_1[n] + x_2[n]

x1[n]x_1[n] and x2[n]x_2[n] are the two signals’ values at sample nn, and y[n]y[n] is their sum at that same sample.

Switch between Show them apart and Show the sum, and compare each sum stem with the two above it.

Adding two signals

A tone, and a slower, quieter tone.

toneslow tone
sum
Display
Describe this picture

Two panels against sample number. The upper one draws a fast tone, “tone”, and a slower, quieter one, “slow tone”, 40 samples each, their stems slightly to either side of each sample number. The lower panel holds their sum, y[n]y[n]. The “Display” buttons, “Show them apart” and “Show the sum”, empty or fill the sum panel; it starts on the sum.

At every sample, the sum stem’s height is the two stems’ heights added: where both are high together the sum peaks higher than either one alone, and where one dips below zero it pulls the sum down.

Multiplying by an envelope

A guitarist’s tremolo pedal doesn’t change the pitch of a note. It makes the loudness swell and fade in a slow, steady rhythm while the note keeps ringing underneath. A recording engineer does something similar by hand at the start and end of a track, fading it in from silence and fading it out again at the end.

Both are the same operation: multiplying a signal by a second, slower signal called an envelope. Multiplying scales one signal’s value by the other’s value at that same instant.

y[n]=x[n]⋅w[n]y[n] = x[n] \cdot w[n]

x[n]x[n] is the fast signal, like a musical tone, w[n]w[n] is the slow envelope, and y[n]y[n] is the product. Wherever w[n]w[n] is zero, y[n]y[n] is zero no matter what x[n]x[n] was doing; wherever w[n]w[n] is one, y[n]y[n] equals x[n]x[n].

Switch between Swell and Fade in and out, and watch the outline of the product.

Multiplying by an envelope

Each sample of the tone is multiplied by the envelope's value at the same sample.

tone x[n]
envelope w[n] (swell)
product y[n] = x[n] · w[n]
Envelope shape
Describe this picture

Three panels against sample number: the tone x[n]x[n], 64 samples at about eight samples per cycle; the envelope w[n]w[n]; and the product y[n]=x[n]⋅w[n]y[n] = x[n] \cdot w[n]. The “Envelope shape” buttons choose “Swell”, a repeating swell with two humps, or “Fade in and out”, a single fade-in, fade-out window.

The product’s outline follows the envelope: it is silent wherever the envelope is zero, full-strength wherever the envelope is one, and in between it is partway.

How much it changed

If you read a car’s odometer once a second, you can work out how fast it was going without a speedometer: subtract each reading from the one after it. A big jump means fast driving; almost no jump means the car barely moved.

That subtraction is called the first difference. It measures how much a signal changed from one sample to the next:

d[n]=x[n]−x[n−1]d[n] = x[n] - x[n-1]

x[n]x[n] is the current sample, x[n−1]x[n-1] is the previous one, and d[n]d[n] is how much the signal changed between them. If you’ve met derivatives in calculus, this is their discrete-time version. Either way, it is near zero on flat stretches and large wherever the signal is changing fast.

Drag Position along the sequence and watch Change at this step.

How much it changed

Drag the marker, or use the slider.

x[n]the two samples being compared
change d[n] = x[n] - x[n-1]
Sample x[n]
+1.7
Sample before, x[n-1]
+1.0
Change at this step
+0.7
2
Describe this picture

Two panels against sample number: a smooth 16-sample sequence x[n]x[n] on top, with a marker on the current sample and the sample before it highlighted, and the change d[n]=x[n]−x[n−1]d[n] = x[n] - x[n-1] below, with the same marker. The marker starts at sample 2 and moves by dragging or with the “Position” slider. Three readouts show “Sample x[n]”, “Sample before, x[n-1]” and “Change at this step”.

Right where the original trace is flattest, at a peak or a trough, the difference is close to zero and changes sign; wherever the original is steepest, the difference is at its largest.

Adding it up as you go

A bank statement lists each day’s deposits and withdrawals separately, but what you actually care about is your running balance: add up every entry so far, and that running total is your balance at any given day.

That running total is called the running sum: at each sample, add up every value the signal has taken so far, including the current one.

s[n]=x[0]+x[1]+⋯+x[n]s[n] = x[0] + x[1] + \cdots + x[n]

or, written with the summation symbol,

s[n]=∑k=0nx[k]s[n] = \sum_{k=0}^{n} x[k]

Here the signal starts at sample 00. x[k]x[k] is its value at sample kk, and s[n]s[n] is the running total once you’ve added up every sample from k=0k = 0 up to and including nn.

Drag Position along the sequence and watch Running total.

Adding it up as you go

Drag the marker, or use the slider.

deposits (+) and withdrawals (-) x[n]still to come
running total s[n] = x[0] + x[1] + ... + x[n]
Deposit x[n]
+2
Running total
2
8
Describe this picture

Two panels against sample number: deposits (positive) and withdrawals (negative) x[n]x[n] on top, with the entries still to come drawn faint, and the running total s[n]=x[0]+x[1]+⋯+x[n]s[n] = x[0] + x[1] + \cdots + x[n] below, built up to the marker. The marker moves by dragging or with the “Position” slider, and two readouts show “Deposit x[n]” and “Running total”.

A run of positive deposits makes the running sum climb steadily, while a run of withdrawals makes it fall.

Key idea

Differencing and running-summing undo each other. Take the difference of a signal, then run a sum back over that difference, and you land back on the original signal exactly (as long as the signal starts from zero, like every recording that begins in silence). Differencing asks “how much did it change since last time?”; summing asks “how much has it built up to?”, and running one right after the other cancels out.

Capping and folding

Push an amplifier past what it can put out and the tops of the wave don’t get louder. They get chopped flat, which is why an overdriven speaker sounds harsh instead of loud. That flattening is called clipping: any value past a fixed limit xclipx_\text{clip} gets capped at that limit instead of being allowed through.

y[n]={x[n],∣x[n]∣≤xclipxclip sgn(x[n]),elsey[n] = \begin{cases} x[n], & \lvert x[n]\rvert \le x_\text{clip} \\ x_\text{clip}\,\mathrm{sgn}(x[n]), & \text{else} \end{cases}

Here sgn(x[n])\mathrm{sgn}(x[n]) is +1+1 for a positive value and −1-1 for a negative one, so a value past the limit is capped at +xclip+x_\text{clip} or −xclip-x_\text{clip}, matching its own sign.

A different operation, rectification, keeps a signal’s size but throws away its sign: every negative value gets flipped positive instead.

y[n]=∣x[n]∣y[n] = \lvert x[n]\rvert

Here is a quick test that separates these two from a plain gain. With a gain, double the input and the output doubles too, every time. Clipping fails that test: double an input that is already past the limit xclipx_\text{clip} and the output does not change; it stays at xclipx_\text{clip}. Rectification fails a sign-flip test: flipping the input’s sign should flip the output’s sign, but ∣−x∣=∣x∣\lvert -x\rvert = \lvert x\rvert, not −∣x∣-\lvert x\rvert. An operation that fails tests like these is called nonlinear.

Switch between As is, Capped, and Folded up, and watch what happens past the limit and below zero.

Capping and folding

The limit is drawn as dashed lines when capping is on.

original x[n]result y[n]
Samples changed
28 of 48
Operation
Describe this picture

One plot of a 48-sample sine-like sequence, amplitude against sample number, with three “Operation” buttons: “As is”, “Capped” and “Folded up”; it starts on Capped. When capping is on, the limit is drawn as dashed lines and labelled "xclip=1x_\text{clip} = 1". When an operation is on, the original is drawn faint behind the result. The readout “Samples changed” counts how many of the 48 samples the operation altered.

Capping leaves flat plateaus wherever the original went past the limit, while folding (rectification) lifts every dip below zero to above it.

Worked example

Take the sequence x[n]=1,3,2,−1,4x[n] = 1, 3, 2, -1, 4 for n=0,1,2,3,4n = 0, 1, 2, 3, 4 (zero everywhere else).

Its first difference, d[n]=x[n]−x[n−1]d[n] = x[n] - x[n-1]: d=1,2,−1,−3,5,−4d = 1, 2, -1, -3, 5, -4 for n=0,…,5n = 0, \dots, 5. The last value comes from the signal dropping back to zero: d[5]=0−4=−4d[5] = 0 - 4 = -4.

Its running sum, s[n]=∑k=0nx[k]s[n] = \sum_{k=0}^{n} x[k]: s=1,4,6,5,9s = 1, 4, 6, 5, 9.

Run a sum back over d[n]d[n] and you get 1,3,2,−1,4,01, 3, 2, -1, 4, 0: exactly x[n]x[n] again (with x[5]=0x[5] = 0), which is the difference and the running sum undoing each other.

Now cap that same sequence at a limit xclip=2x_\text{clip} = 2. Clipped: 1,2,2,−1,21, 2, 2, -1, 2 (only the 33 and the 44 get capped down to 22). Rectified instead (no capping, y=∣x∣y = \lvert x\rvert): 1,3,2,1,41, 3, 2, 1, 4.

Where you’ll meet this

A mixing console’s fader is a gain; a sensor’s “zero” adjustment is an offset. Two microphones feeding one recording channel are added together exactly as here. A synthesizer’s tremolo and a video editor’s audio fade are both multiplication by an envelope. A speedometer estimating speed from odometer readings is a first difference; a bank balance is a running sum. And an overdriven amplifier, or a circuit that turns every negative swing of a wave positive, is clipping or rectification.

Reference card

OperationFormulaNotes
Scale & offsety[n]=a⋅x[n]+by[n] = a \cdot x[n] + baa: gain, negative flips it; bb: constant offset
Additiony[n]=x1[n]+x2[n]y[n] = x_1[n] + x_2[n]sample-by-sample sum
Multiplicationy[n]=x[n]⋅w[n]y[n] = x[n] \cdot w[n]envelope × tone: tremolo, fades
First differenced[n]=x[n]−x[n−1]d[n] = x[n] - x[n-1]how much it changed since the last sample
Running sums[n]=∑k=0nx[k]s[n] = \sum_{k=0}^{n} x[k]total built up so far; undoes the first difference
Clippingy[n]=x[n]y[n] = x[n] if ∣x[n]∣≤xclip\lvert x[n] \rvert \le x_\text{clip}, else xclip sgn(x[n])x_\text{clip}\,\mathrm{sgn}(x[n])caps peaks at a limit xclipx_\text{clip}; nonlinear
Rectificationy[n]=∣x[n]∣y[n] = \lvert x[n] \rvertkeeps size, drops sign; nonlinear

End of lesson 2.2

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look