Spectral Features — Machine Learning/Feature Engineering
Machine_Learning/Feature_Engineering/Spectral_Features · 1 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Spectral Features
Machine Learning / Feature Engineering
Reduces a magnitude spectrum to the four descriptors a classifier is actually trained on. With bin k carrying mass mk at frequency fk = k·Δ, and T = Σ mk over the K bins used:
- Centroid – (Σ fkmk) ÷ (T + ε), the spectrum's centre of mass: where the energy sits.
- Spread – √((Σ (fk − centroid)² mk) ÷ (T + ε)), its standard deviation about that centre: a pure tone is narrow, noise is wide.
- Roll-off – Δ × the number of bins whose cumulative mass is still below ρ·T, i.e. the frequency below which fraction ρ of the energy lies.
- Flatness – the geometric mean over the arithmetic mean, exp(meank ln(mk + ε)) ÷ (T÷K + ε). Near 1 is noise-like, near 0 is tonal.
Its natural upstream is FFT Magnitude. Four numbers per step instead of N bins is what makes a spectrum usable by Dense Layer, SVM Predictor or any of the classifiers in this family.
Ports
- m – the magnitude spectrum, a column [M,1]. Values are read as nonnegative masses; whether they are magnitudes or magnitudes squared is the upstream block's choice and changes only what the numbers mean.
- f – the features, a [4,1] column in the order centroid, spread, roll-off, flatness. Always four rows, whatever M is.
Parameters
- Input Spectrum – which bins are used.
- Two-sided (fold to first half) – uses bins 0 … M/2 only, so K = M/2 + 1. This is the setting for a spectrum straight out of FFT Magnitude, which emits the full length N with both halves: for a real input those halves are mirror images, and a centroid taken over both reports the mirror rather than the signal. M must be even.
- One-sided (use every bin) – uses all M bins, for a caller who has already folded the spectrum or is describing something that is not a two-sided transform.
- Roll-off Fraction – ρ, the mass fraction the roll-off point must reach; a number in (0, 1]. 0.85 is the usual choice.
- Bin Spacing – Δ, the frequency step between bins. Left at 1 the centroid, spread and roll-off come out in bins; set it to fs÷N to get Hz. Flatness is dimensionless and does not scale with it.
- Epsilon – ε, the guard added to the totals and inside the logarithm. It is what makes an all-zero spectrum return zeros instead of NaN, and what keeps ln() away from an empty bin. Keep it small relative to the masses you expect.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text. K is known at export time, so every sum is unrolled and no backend contains a loop or a bounds check. ρ, Δ and ε are baked into the arithmetic rather than exposed as tunable parameters: they define what the features mean, so retuning one on the target would silently produce a different feature than the model was trained on.
The three HDL targets are simulation-only real
arithmetic, quantizing only at the port boundary. A division, a square root, a
logarithm and an exponential do not belong in a Q16.16 datapath, and this block
needs all four. ⚠ Note also that a magnitude-squared spectrum grows as the
square of the window energy, and Q16.16 saturates past ±32768 at the port
itself – feed the HDL targets the Magnitude form rather than
Magnitude squared if the window is large.
Simulink bridge
None, and measured rather than assumed. R2026a on this machine has no
block in any of DSP System Toolbox's 42 library files whose name matches
centroid, roll-off, flatness or spectral; Audio Toolbox – whose
spectralCentroid, spectralSpread and
spectralRolloffPoint these four follow – is not installed;
and the spectral descriptors that do exist here
(spectralFlatness, spectralCrest,
spectralEntropy, spectralKurtosis,
spectralSkewness) are Signal Processing Toolbox functions,
which no ParamRule can carry across the bridge. The block therefore
has no parity testbench, which is the documented consequence of
Support::None rather than a gap.
Notes
- Algebraic and stateless: every feature comes from the current spectrum alone. Nothing is averaged across steps – put Rolling Statistics after it if you want that.
- The flatness convention was verified against MATLAB: R2026a's
spectralFlatnessis exactly geometric mean over arithmetic mean of the bins given, with no frequency weighting, and agrees with this block to the last digit. - Roll-off is computed as a count, not a search: the number of bins still below ρ·T is the index of the first bin that reaches it. That keeps all ten backends branch-free and identical, and an all-zero spectrum lands on bin 0 rather than on a NaN.
- The spread uses the two-pass form rather than √(E[f²] − centroid²). The identity form is cheaper to generate but can go slightly negative under rounding when the mass sits in one bin, and a negative under a square root is a NaN in ten backends at once.
Code facts#
| Fact | Value |
|---|---|
| registered type | Machine_Learning/Feature_Engineering/Spectral_Features |
| family | Machine_Learning/Feature_Engineering |
| solver environment class | ICoreBlock_0_Machine_Learning_1_Feature_Engineering_2_Spectral_Features |
| source | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Feature_Engineering/Spectral_Features/ICoreBlock_0_Machine_Learning_1_Feature_Engineering_2_Spectral_Features.cpp |
| header | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Feature_Engineering/Spectral_Features/ICoreBlock_0_Machine_Learning_1_Feature_Engineering_2_Spectral_Features.h |
| default size on canvas | 132 × 84 px |
| ports at insert | 1 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | m |
| 2 | out | ICoreDouble | f |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Input Spectrum | Two-sided (fold to first half)%~%One-sided (use every bin… | — |
Roll-off Fraction | 0.85 | — |
Bin Spacing | 1 | — |
Epsilon | 1e-12 | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): no Simulink equivalent, and this was MEASURED on R2026a here rather than assumed: no block in any of DSP System Toolbox's 42 library files has a name matching centroid/roll-off/flatness/spectral, Audio Toolbox (which owns spectralCentroid, spectralSpread and spectralRolloffPoint) is not installed, and the descriptors that do exist -- spectralFlatness, spectralCrest, spectralEntropy, spectralKurtosis, spectralSkewness -- are Signal Processing Toolbox FUNCTIONS, which no ParamRule can carry across the bridge
Catalog contract: src/ICoreSDK/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The checker has a blind spot here — it could not resolve something (a grouped port bullet, a computed config name), which is reported and never counted as a pass. A reader has to settle it:
B0every stimulus in the sample errored — cross-checks skipped
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Spectral Features — centroid, spread, roll-off and flatness of a magnitude spectrum y = [ centroid ; spread ; roll-off ; flatness ] one [4,1] vector per step
The natural consumer of FFT_Magnitude: a classifier is trained on these four numbers, not on 512 bins. Everything is unrolled at export time -- K is known once the model is built -- so no backend emits a loop, an index type or a bounds check.
⚠ The formula is written ONCE, in the six *Expr() members, and each generator supplies only a Dialect saying how its language spells a bin read, a literal, sqrt, ln and exp. Ten hand-written copies of four statistical expressions is precisely where one backend ends up with a parenthesis or an epsilon the other nine do not have.
The three HDL targets are SIMULATION-ONLY
real: this is division, a square root, a logarithm and an exponential, none of which belongs in a Q16.16 datapath.
Sample results#
No stimulus produced a sampled output in this rig — Odd spectrum width at: ICore Blocks/Home/Spectral Features. That is a fact about the single-block rig, not a verdict on the block: an offline batch fit, a block whose output only appears at onSolverFinish, or one that needs a driven environment cannot be exercised alone.
Category unsampled · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60
Sample data: docs/generated/samples/Machine_Learning__Feature_Engineering__Spectral_Features.json