Operations on Signals
Most of signal processing is built from a handful of operations on the time axis and on values: shifting, reversing, scaling time, and adding or multiplying signals. Their effect on the spectrum is the content of the later "shift" and "scaling" theorems, so it pays to be precise about them now.
Time shift, reversal and scaling
For a continuous signal :
- is delayed by (shifted right when ),
- is reversed in time,
- with is compressed (plays faster), with stretched.
Combined operations are easiest to read by asking "where does the original go?". For , the original origin moves to and the signal is compressed by 2.
import numpy as np
def x(t): # a triangle pulse on [0, 2], peak at t = 1
return np.clip(1 - np.abs(t - 1), 0, None)
t = np.arange(-1, 6, 0.5)
print("t: ", t.tolist())
print("x(t): ", np.round(x(t), 2).tolist())
print("x(t - 2): ", np.round(x(t - 2), 2).tolist())
print("x(2t - 6): ", np.round(x(2 * t - 6), 2).tolist())
print("x(-t + 2): ", np.round(x(-t + 2), 2).tolist())t: [-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5]
x(t): [0.0, 0.0, 0.0, 0.5, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
x(t - 2): [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0]
x(2t - 6): [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]
x(-t + 2): [0.0, 0.0, 0.0, 0.5, 1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
The delayed pulse peaks at ; peaks at , where , and is half as wide.
Discrete time is different
For only integer shifts make sense, and time scaling becomes downsampling (keep every -th sample, which can lose information) or upsampling (insert zeros). Downsampling is the operation that causes aliasing.
import numpy as np
x = np.arange(10) ** 2 # 0, 1, 4, 9, ...
print("x[n] :", x.tolist())
print("x[n - 2] :", np.concatenate([[0, 0], x[:-2]]).tolist(), "(zeros shifted in)")
print("x[2n] :", x[::2].tolist())
up = np.zeros(2 * len(x[:5]), dtype=int)
up[::2] = x[:5]
print("upsampled :", up.tolist())x[n] : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
x[n - 2] : [0, 0, 0, 1, 4, 9, 16, 25, 36, 49] (zeros shifted in)
x[2n] : [0, 4, 16, 36, 64]
upsampled : [0, 0, 1, 0, 4, 0, 9, 0, 16, 0]
Adding and multiplying
Adding signals mixes them (two instruments). Multiplying a signal by a sinusoid is modulation, the basis of AM radio; multiplying by a 0/1 pulse is windowing, which cuts out a segment. Both have simple effects on the spectrum (Shift, Scaling and Modulation).
import numpy as np
fs = 100
t = np.arange(0, 1, 1 / fs)
message = 1 + 0.5 * np.cos(2 * np.pi * 2 * t) # slow "audio"
carrier = np.cos(2 * np.pi * 20 * t) # fast carrier
am = message * carrier
amp = 2 * np.abs(np.fft.rfft(am)) / len(t)
freqs = np.fft.rfftfreq(len(t), 1 / fs)
peaks = freqs[amp > 0.1]
print("frequencies present in the AM signal:", peaks.tolist())
print("their amplitudes:", np.round(amp[amp > 0.1], 3).tolist())frequencies present in the AM signal: [18.0, 20.0, 22.0]
their amplitudes: [0.25, 1.0, 0.25]
Multiplying by the 20 Hz carrier moved the message's frequencies (0 and 2 Hz) to and Hz.
In software
np.rollshifts circularly (samples wrap around); for a true delay pad with zeros, as above, or usescipy.ndimage.shift.- Changing playback speed without resampling (time scaling) also scales every frequency, which is why sped-up voices sound higher.
Exercises
Exercise 1: Where is the pulse?
A pulse is nonzero on . On which interval is nonzero?
Answer
.
Solution
when , that is . The pulse was reversed and moved so that the original sits at .
Exercise 2: Order of operations
Is the same as "compress by 2, then delay by 6"?
Answer
No: compressing by 2 then delaying by 3 gives it; compressing then delaying by 6 gives .
Solution
Compressing gives ; delaying by gives . To get we need . Alternatively delay first by 6, , then compress: .
Exercise 3: Downsampling loses information (code)
Downsample by 2 and by 4. What do you get?
Answer
By 2: ; by 4: all ones.
Solution
import numpy as np
n = np.arange(16)
x = np.round(np.cos(np.pi * n / 2)) + 0.0
print(x[::2].tolist())
print(x[::4].tolist())[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]
[1.0, 1.0, 1.0, 1.0]
After downsampling by 4 the oscillation has vanished completely: the samples land on the peaks only, so the result looks like a constant. That is aliasing.
Test yourself
is …
- advanced by 3
- delayed by 3
- compressed by 3
- reversed
compared with …
- lasts twice as long
- lasts half as long
- is delayed by 2
- has double amplitude
Multiplying a message by moves its spectrum to…
- around
- only
- nowhere, it is unchanged