Generated reference › SNR — Control Systems/Spectral Measurements
kind: generated#block#control-systems-spectral-measurements

SNR — Control Systems/Spectral Measurements

Control_Systems/Spectral_Measurements/SNR · 1 input / 2 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.

SNR

Control Systems / Spectral Measurements

The signal-to-noise ratio of a one-sided power spectral density, in decibels: the power of the fundamental tone over the power of everything that is neither DC, the fundamental, nor one of its harmonics. r = 10·log₁₀(Pfund / Pnoise). This is MATLAB's snr(Pxx, F, 'psd'), transcribed from its source.

Each tone is measured over a band rather than a bin – the run of bins that climbs to its peak and falls away from it – and each band is removed once counted. What is left is the noise; its floor is the median of the surviving bins, written back into the removed ones and then capped bin by bin at the original density so the estimate cannot hide a low peak.

Ports

  • p – the one-sided power spectral density, an [N,1] column or a [1,N] row of 3 to 32 bins. Bin k sits at frequency k·Δ, so the first bin is DC. Values are expected non-negative, as MATLAB requires.
  • snr – the ratio in dB, a scalar.
  • npow – the integrated noise power in dB, a scalar: the denominator above, reported on its own because it is what moves when the harmonic count changes.

Parameters

  • Bin Spacing – Δ, the frequency step between neighbouring bins, a single positive number. The whole frequency axis follows from it.
  • Harmonics – how many harmonics of the fundamental are removed from the noise, counting the fundamental itself as the first. A whole number between 2 and 12; MATLAB's default is 6, which is this block's default too. A harmonic whose frequency lands past the end of the axis is skipped, not clamped, so a coarse axis simply removes fewer of them.
  • 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. The whole decomposition is unrolled at export time – the bin count, the spacing and the harmonic count are all settled before the run – so no emitted core contains a loop, an array index that moves, or a sort. The median is found by rank selection instead, which costs N² comparisons and is the reason the window is capped at 32 bins rather than the 64 the rest of the family allows.

The three HDL targets are simulation-only: they carry the arithmetic in real and quantize only at the port boundary. A logarithm and a ratio of two run-time quantities do not belong in a Q16.16 datapath, and VHDL has no real minimum before 2008, so the smaller of two values is spelled arithmetically there – exact enough for a simulation and not bit-exact, which is why those three columns are compared in a band.

Simulink bridge

No equivalent, so nothing crosses in either direction. Measured rather than assumed: snr is a Signal Processing Toolbox function, that toolbox ships no Simulink library at all, and a find_system sweep of 2102 blocks across twenty DSP System Toolbox and Simulink library roots – at depth 6, under masks – matches nothing for snr, sinad, thd, sfdr, distortion, spurious or noise ratio. Mixed-Signal Blockset, which does carry measurement blocks of this kind, is not installed and is not one of the eight toolboxes this work covers.

Notes

  • Algebraic and stateless: the reading depends only on the spectrum presented this step.
  • It is not SINAD. That block leaves the harmonics IN the noise, so on a distorting signal it reads lower. They are one decomposition with one stage switched off, and THD is the third reading of it.
  • The DC bin is doubled before anything looks at the spectrum, and the DC component is then the larger of bins 0 and 1 rather than bin 0. On a spectrum whose tallest bin is DC that alone decides where the fundamental search begins.
  • A spectrum with nothing left after the removals is handled the way MATLAB handles it: the median of an empty set is not a number, MATLAB's min ignores one, and every removed bin therefore falls back to its original density. An ordinary eight-bin spectrum reaches this.
  • A spectrum with no power at all has no fundamental. Both readings are then floored rather than left as a division by zero, because a real division by zero aborts a VHDL simulation instead of producing an infinity.

Code facts#

FactValue
registered typeControl_Systems/Spectral_Measurements/SNR
familyControl_Systems/Spectral_Measurements
solver environment classICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_SNR
sourcesrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Spectral_Measurements/SNR/ICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_SNR.cpp
headersrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Spectral_Measurements/SNR/ICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_SNR.h
default size on canvas140 × 80 px
ports at insert1 in, 2 out
code generators implementedPython, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Ports#

#DirectionSignal typeDescription label
1inICoreDoublep
2outICoreDoublesnr
3outICoreDoublenpow

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 variableDefaultSimulink parameter
Bin Spacing1—
Harmonics6—

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.

supportSupport::None
Simulink path—
port-count rulePortsParam::None
SampleTime parameteryes

Caveat (shown to the user): no Simulink equivalent. snr is a Signal Processing Toolbox MATLAB function, not a block; that toolbox ships no Simulink library, and a sweep of 2102 blocks across twenty DSP System Toolbox and Simulink library roots carries no signal-to-noise, SINAD, THD or SFDR measurement block

Catalog contract: src/ICoreBlocks/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:

  • B0 every 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).

SNR -- the signal-to-noise ratio of a one-sided spectrum (MATLAB snr(Pxx, F, 'psd')) r = 10*log10( Pfund / totalNoise )

The fundamental's integrated power over the power of everything that is neither DC, the fundamental, nor one of its first nHarm harmonics. The decomposition that produces those two numbers is shared with SINAD and THD and lives in ICoreSpectralDistortionSupport.h, which carries the transcription facts, the MATLAB sources they came from, and the reason the frequency axis is uniform here.

⚠ MEASURED AGAINST R2026a BEFORE ANY OF THIS WAS WRITTEN, and twice over. The five spectra below were run through snr/sinad/thd in the installed R2026a, reproduced by a Python stand-in, and then reproduced again by compiling the shared reference OUTSIDE the app: 35 reported readings, worst disagreement 9.9e-16 relative, all of it summation order in a dot product. Then the EMITTED statement list was compiled as a C program and diffed against that same reference over 140 values: bit-identical, 0.

p1 = [0.05 0.10 3.00 0.40 0.90 0.12 0.30 0.08 0.20 0.06 0.15 0.04], delta = 0.25 nHarm 2 -> 3.7311642494700550 dB noise -4.4369749923271273 dB nHarm 4 -> 5.1236262659518239 dB noise -5.8294370088088954 dB nHarm 6 -> -2.0091484278071343 dB noise 1.3033376849500613 dB p2 = [0.12 0.85 2.40 1.10 0.35 0.60 0.18 0.07], delta = 0.5 nHarm 2, 4 and 6 all -> -1.6812232938440590 dB p3 = [0 0 3 0 0 1 0 0], delta = 0.5 -> -1.2493873660829993 dB at every nHarm p4 = sixteen bins at delta = 0.2 -> 3.4697 / 6.9730 / 6.9730 dB p5 = [2.50 0.80 0.30 1.90 0.25 0.40 0.15 0.06], delta = 0.5 -> -4.7101865573280852 dB

⚠ THREE OF THOSE ROWS ARE THERE BECAUSE EACH PINS A RULE THAT A REWRITE GETS WRONG:

  • p1 is the only one whose answer MOVES with nHarm in both directions -- it rises from

nHarm 2 to 4 and then falls by 7 dB at 6. Removing a harmonic takes power out of the noise AND takes a bin out of the set the median is drawn from, and the second effect can be the larger one. A block that only ever subtracts would pass the first two rows.

  • p2 answers the same number at every nHarm, and not because it has no harmonics: the

second harmonic's band swallows the WHOLE axis, so nothing is left, the median is empty, and every zeroed bin falls back to its original value. That is the NaN-omitting branch, and it is reached by an utterly ordinary spectrum.

  • p5's largest bin is DC. The DC component is doubled before anything looks at the

spectrum, so whether bin 0 or bin 1 is the DC "tone" -- and therefore where its band ends and which bin the fundamental search starts from -- depends on that doubling.

Sample results#

No stimulus produced a sampled output in this rig — Invalid input size at: ICore Blocks/Home/SNR. 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 477e53523 · produced by docsSample --out <folder> --blocks SNR SINAD THD SFDR --steps 60

Sample data: docs/generated/samples/Control_Systems__Spectral_Measurements__SNR.json