add_noise
result = add_noise(x, kind, [snr=], [amplitude=], [seed=])
Input: x, kind, snr, amplitude, seed · Output: result
What it is
Adds noise of the named kind (a string — see the kinds table near the top of this chapter) to x, a length-N number vector or an Image (noise is added per-pixel, per-channel). snr (named number, dB) or amplitude (named number, raw units) sets the level — give one or the other, never both; with neither, defaults to 20 dB. seed is a named integer for a reproducible draw, default 1.
Overloads
x can be string, number, vector, or Image; kind can be string, number, vector, or Image.
2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.
| Code | Input | Output | Description |
result = add_noise(x, kind, [snr=], [amplitude=], [seed=]) | x string, number, vector, or ImageAdds noise of the named kind (a string — see the kinds table near the top of this chapter) to x, a length-N number vector or an Image (noise is added per-pixel, per-channel).kind string, number, vector, or ImageAdds noise of the named kind (a string — see the kinds table near the top of this chapter) to x, a length-N number vector or an Image (noise is added per-pixel, per-channel).snr number, optionalsnr (named number, dB) or amplitude (named number, raw units) sets the level — give one or the other, never both.amplitude number, optionalsnr (named number, dB) or amplitude (named number, raw units) sets the level — give one or the other, never both.seed optionalseed is a named integer for a reproducible draw, default 1. | result — | Every sample is perturbed by a little: none of the output matches the clean input exactly. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
x = sin(2 * pi * 5 * (0 to 199) / 100)
g = add_noise(x, "gaussian", snr = 6, seed = 1)
untouched = 0
for i in 0 to 199
if g[i] == x[i]
untouched = untouched + 1
end if
end for
print(untouched) # 0 -- every sample was perturbed a little
0
| Code | Input | Output | Description |
result = add_noise(x, kind, [snr=], [amplitude=], [seed=]) | x string, number, vector, or ImageAdds noise of the named kind (a string — see the kinds table near the top of this chapter) to x, a length-N number vector or an Image (noise is added per-pixel, per-channel).kind string, number, vector, or ImageAdds noise of the named kind (a string — see the kinds table near the top of this chapter) to x, a length-N number vector or an Image (noise is added per-pixel, per-channel).snr number, optionalThe same snr targets a corruption *rate* instead: most samples are left bit-for-bit alone, and a fraction are slammed to an extreme.amplitude number, optionalsnr (named number, dB) or amplitude (named number, raw units) sets the level — give one or the other, never both.seed optionalseed is a named integer for a reproducible draw, default 1. | result — | — |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
x = sin(2 * pi * 5 * (0 to 199) / 100)
sp = add_noise(x, "salt_pepper", snr = 6, seed = 1)
untouched = 0
for i in 0 to 199
if sp[i] == x[i]
untouched = untouched + 1
end if
end for
print(untouched) # 184 -- ~8% got slammed to an extreme, the rest untouched
184
Pink noise is worth a second look: it dominates at low frequencies, which is why a slow measurement is harder than a fast one and a long integration is not free.
Works on a vector and on an image alike — every kind is per-sample, so one function covers 1-D and 2-D rather than two that drift apart.