Qu

impulse

Builtin index · Signal processing

result = impulse(n, [index=0], [amplitude=1])

Input: n, index, amplitude · Output: result

What it is

n: number (integer length); index (optional number, integer sample position); amplitude (optional number).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = impulse(n, [index=0], [amplitude=1])
n numbern: number (integer length).
index number, optional (default 0)index (optional number, integer sample position).
amplitude number, optional (default 1)amplitude (optional number).
result vector or Signal
Returns a length-n plain vector (not a Signal, since no fs is given — wrap in signal(impulse(n), fs) if a sample rate is needed): a Kronecker delta, all zeros except amplitude at index. Also reachable as signals.impulse.

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
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

impulse(n, [index=], [amplitude=]) — a Kronecker delta: all zeros except amplitude at index (default the first sample). A plain Vec, not a Signal — the sample rate doesn't change what this sequence *is*; wrap it in signal(impulse(n), fs) if you need one.

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