Qu

measure_snr

Builtin index · Noise

result = measure_snr(clean, noisy)

Input: clean, noisy · Output: result

What it is

Computes the achieved signal-to-noise ratio by treating noisy - clean as the noise. clean and noisy are two length-N number vectors of equal length.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = measure_snr(clean, noisy)
clean number or vectorclean and noisy are two length-N number vectors of equal length.
noisy —Computes the achieved signal-to-noise ratio by treating noisy - clean as the noise.
result number
Returns a single number, in dB. Closing the loop.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

fs = 1000
t = (0 to 2047) / fs
clean = sin(2 * pi * 7 * t)

noisy = add_noise(clean, "gaussian", snr = 3, seed = 7)
print("asked for 3 dB, measured {measure_snr(clean, noisy):.2f} dB")
output
asked for 3 dB, measured 3.00 dB

from noise

fs = 1000
t = (0 to 999) / fs
clean = sin(2 * pi * 7 * t)

h = hum(clean, fs, freq = 50, harmonics = 3, snr = 6)
e = emf(clean, fs, carrier = 150, snr = 6)
print("hum {measure_snr(clean, h):.2f} dB, emf {measure_snr(clean, e):.2f} dB")
output
hum 6.00 dB, emf 6.00 dB

from noise

fs = 1000
n = 500
t = (0 to n - 1) / fs
clean = sin(2 * pi * 7 * t)

spike = 5 * impulse(n, index = 200)
corrupted = clean + spike + 0.01 * (0 to n - 1)   # a spike, plus baseline drift


fixed = medfilt(corrupted, 5)     # the spike

flat = detrend(fixed, 1)          # the ramp

sg = savgol(flat, 11, 2)          # a final polish, shape-preserving

sm = smooth(flat, 11)             # same thing, one word (default method is savgol)

print("after cleanup: {measure_snr(clean, sg):.2f} dB (savgol), {measure_snr(clean, sm):.2f} dB (smooth)")
output
after cleanup: 17.81 dB (savgol), 17.81 dB (smooth)

from noise

How it works

measure_snr(clean, noisy) — what the SNR actually is, taking the noise as the difference. The loop-closer: it lets "add noise at 3 dB" be checked rather than trusted. Not called snr, which already means the spectral single-argument estimate.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.