Signal processing · sampling
Time Axis for Sampled Signals
A sampled signal is a list of numbers; the time axis says when each one was taken. For a uniform sampling rate fs the instant of sample n, counting from zero, is t(n) = n / fs. A time axis function builds that vector for N samples so that plots, integrals and derivatives use real seconds rather than sample numbers. It is the first vector built in most signal processing scripts.
Spacing, length and duration
The step between samples is the sampling period Ts = 1/fs. A vector of N samples covers N − 1 steps from first to last, but it represents a record of duration N·Ts, because each sample stands for one period. Both numbers are used and they differ: the last time stamp is (N − 1)/fs, the duration is N/fs.
Take fs = 8000 Hz and N = 8. The period is 0.125 ms, and the instants are 0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875 ms. The last stamp is 0.875 ms, the record lasts 1 ms. The frequency spacing for the same record, 1 / 1 ms = 1000 Hz, equals fs/N = 8000/8, which ties this axis to the frequency axis built from the same two numbers.
Start offsets and unusual grids
When the record begins at t₀ rather than zero, add it: t(n) = t₀ + n/fs. A pulse at the center of a window of N points may be easier to read with a symmetric axis, t(n) = (n − (N − 1)/2) / fs, which for N = 5 and fs = 4 gives −0.5, −0.25, 0, 0.25, 0.5 seconds.
Going the other way, a record described by a start, an end and a count needs the rate worked out. Does the end belong in the record? With N points between t₀ and t₁ inclusive the step is (t₁ − t₀)/(N − 1). Excluding the end, as periodic records do, gives (t₁ − t₀)/N. The difference is small for long records and decisive for short ones. For 5 points on 0 to 1 s inclusive the step is 0.25 s; excluding the end, 0.2 s.
Avoiding drift
Building the axis by repeatedly adding the step accumulates rounding error. For a million samples at a rate that is not a power of two the accumulated error can reach many units in the last place. The safer route is multiplying an integer index by the step, or dividing by the rate, since each entry then carries only a single rounding. When a computed end time must match a measured one exactly, work with integer sample counts and convert at the end.
Matching the axis to the data
An axis of the wrong length is the commonest error, caught as a size mismatch in plotting. Convolving two signals produces a longer vector, as in convolution of vectors, so the output needs its own axis with m + n − 1 entries and the start offset equal to the sum of the starting offsets of the two inputs. If x starts at t = 0 and h starts at t = −2 samples, the output starts at −2 samples.
For resampled data the axis changes spacing: an interpolation by a factor L shortens the step to Ts/L, while a decimation by M lengthens it to M·Ts. The principle of keeping the duration fixed while the count changes is shown on the resampling page.
Plotting checks
A quick test of any axis is to generate a sine of known frequency, say 1 kHz at 8 kHz sampling, and plot it against the axis: one period should span exactly 8 samples and 1 ms. If it spans something else, the rate passed to the function is wrong. A second test is that a segment of the axis, differenced, gives a constant equal to Ts; a deviation means the grid is not uniform and the methods above no longer apply without interpolation.
Pseudocode
function time_axis(N, fs, t0):
n = 0 .. N-1
return t0 + n / fs # one rounding per entry