Qu

blocks

Builtin index · Signal processing

result = blocks(x, n, [hop=n])

Input: x, n, hop · Output: result

What it is

x: a vector or Signal; n: block length, either a whole number of samples or a duration (100 ms) resolved against x's own Fs; hop: distance between block starts, default n (no overlap).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = blocks(x, n, [hop=n])
x vector or Signalx: a vector or Signal.
n numbern: block length, either a whole number of samples or a duration (100 ms) resolved against x's own Fs.
hop optional (default n)hop: distance between block starts, default n (no overlap).
Fs vectora plain vector if x had none
Returns a list of blocks, so for b in s.blocks(4096) iterates it with the ordinary for loop. Each block is a Signal at x's Fs (a plain vector if x had none). The last block is short when the length does not divide evenly — passed as-is, never zero-padded. A duration is a length: 0.5 s at 8 Hz is 4 samples, one fewer than the inclusive slice s[0 s : 0.5 s] spans. Added in v0.2.4.

Examples

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

for b in s.blocks(250)
    print(rms(b))
end for

for b in s.blocks(100 ms)      # sized against s.Fs

    print(rms(b))
end for
output
0.705691
0.70852
0.705691
0.70852
0.679771
0.76825
0.62606
0.764603
0.686392
0.679771
0.76825
0.62606
0.764603
0.686392

from signal-processing

How it works

blocks(x, n, [hop=n]) — cut x into a LIST of consecutive blocks, so for b in s.blocks(4096) works with the for loop Qu already has. This is toolkit-signal.md §5's second surface ("[from specs.md §49]"), and it is deliberately a plain Value::List rather than a new lazy-iterator value: eval_for_iterable matches List/Vec/Signal/Num/range and errors on everything else, so a new variant would need its own arm there AND in collection_elems, to buy laziness nobody asked for over data that is already wholly in memory. n is a whole number of SAMPLES, or a duration (blocks(s, 100 ms)) resolved against the signal's own Fs. A duration is a LENGTH, so it is round(secs * fs) samples — NOT the inclusive s[0 s : 0.1 s] slice convention, which spans round(t1*fs) - round(t0*fs) + 1 and would make every block one sample too long. Probed 2026-09-18 against this binary: s[0.0 s : 0.25 s] on an Fs=8 signal returns 3 samples, not 2. The classifier is §1's own axis_coord, so a frequency handed in where a duration belongs is refused by SI dimension rather than by spelling. Each block is a Signal at the input's Fs (a plain Vec if the input had no rate), so a per-block fft/rms reads the rate off its own argument. The last block is short when the length is not a multiple of n, passed as-is and never zero-padded — same rule as block_process, for the same reason: padding invents samples the caller never had.

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