Decibel Conversion — Control Systems/Spectral Measurements
Control_Systems/Spectral_Measurements/Decibel_Conversion · 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.
Decibel Conversion
Control Systems / Spectral Measurements
Converts elementwise between a linear quantity and decibels, in either
direction, covering MATLAB's db, mag2db,
pow2db, db2mag and db2pow in one block:
- Amplitude → dB: y = 10·log₁₀(a²/R)
- Power → dB: y = 10·log₁₀(a)
- dB → Amplitude: y = √(R·10u/10)
- dB → Power: y = 10u/10
with 30 dB added on the way out (or removed on the way in) when the reference is dBm, since a milliwatt is 30 dB below a watt.
The two directions are not symmetric about zero, and it is worth knowing which is which. Amplitude squares before the logarithm, so a negative input is read as its magnitude and −2 gives the same +6.02 dB as +2. Power does not: it takes the logarithm of the input itself, so a negative input is NaN and a zero is −∞.
Ports
- u – the quantity to convert, any size [m,n]. What it means depends on the mode: an amplitude, a power, or a value in decibels.
- y – the converted value, the same size as the input, element for element.
Parameters
- Conversion – which of the four maps above runs. The default is
Amplitude to dB.
- Amplitude to dB –
db/mag2db, through the load resistance. - Power to dB –
pow2db. The resistance is ignored here: a power is already a power. - dB to Amplitude –
db2mag, the exact inverse of the first mode. It returns the positive root, so a signal that went in negative comes back positive. - dB to Power –
db2pow, the exact inverse of the second.
- Amplitude to dB –
- dB Reference – dB (relative to the unit) or dBm (relative to a milliwatt), which shifts the decibel side by exactly 30.
- Load Resistance (ohms) – R, the resistance an amplitude
is developed across, so that a²/R is a power. A strictly positive number;
1 is the default and leaves the amplitude modes equal to
mag2db/db2mag. It has no effect in the two power modes. - Guard Against Log Of Zero – on adds one machine epsilon (2.22×10−16) to the input before the logarithm, so an exact zero returns a large finite number instead of −∞. It is added to the input, once, before the square – so a zero amplitude reads −313.07 dB rather than −156.54. Off by default.
- 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 mode, the reference offset, the resistance and the guard are all decided at export time and inlined, so the emitted core carries one logarithm or one exponential per element and no branch. 10x is emitted as ex·ln10 rather than as a power operator, because that spelling exists in every target.
The three HDL targets are simulation-only and say so: they carry
the arithmetic in real and quantize only at the port boundary. That
is not a tuning choice – a logarithm has no Q16.16 form, and the answer is
unbounded below near a zero input. Use a software target to deploy this block.
Simulink bridge
Both directions, mapping to dspmathops/dB Conversion from
the DSP System Toolbox, with Conversion → intype,
dB Reference → dBtype, Load Resistance (ohms)
→ R and Guard Against Log Of Zero →
fuzz.
Only the two forward modes cross. The Simulink block converts to
decibels and has no inverse at all, so dB to Amplitude and dB to
Power are an ICore superset: exporting a block set to either is reported
– "value ... has no Simulink equivalent; left at default" – rather
than translated into a mode that does something else. The Simulink block has
no SampleTime parameter (measured: set_param on it is a hard
error), so an explicit rate stays on the ICore side.
⚠ The parameter is intype, not the convert a reader
would guess, and dspmathops/dB Gain is a different block
(a gain expressed in decibels), not this one.
Notes
- Algebraic and elementwise, with no state. One step is one conversion and nothing carries over.
- Measured against R2026a. The Simulink block was simulated over all sixteen combinations of its four parameters and reproduced exactly, and over negative and zero inputs to establish the two asymmetries above.
- No state space. A logarithm is not y = D·u, so the block carries none and model reduction correctly declines to merge it.
Code facts#
| Fact | Value |
|---|---|
| registered type | Control_Systems/Spectral_Measurements/Decibel_Conversion |
| family | Control_Systems/Spectral_Measurements |
| solver environment class | ICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_Decibel_Conversion |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Spectral_Measurements/Decibel_Conversion/ICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_Decibel_Conversion.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Spectral_Measurements/Decibel_Conversion/ICoreBlock_0_Control_Systems_1_Spectral_Measurements_2_Decibel_Conversion.h |
| default size on canvas | 150 × 70 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 | u |
| 2 | out | ICoreDouble | y |
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 |
|---|---|---|
Conversion | Amplitude to dB%~%Power to dB%~%dB to Amplitude%~%dB to P… | intype |
dB Reference | dB%~%dBm~~dB | dBtype |
Load Resistance (ohms) | 1 | R |
Guard Against Log Of Zero | off%~%on~~off | fuzz |
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::Both |
| Simulink path | dspmathops/dB Conversion |
| port-count rule | PortsParam::None |
SampleTime parameter | no — the counterpart defines none; the rate stays on the ICore side |
| ICore config | Simulink parameter | Value translation |
|---|---|---|
Conversion | intype | Amplitude to dB → Amplitude, Power to dB → Power |
dB Reference | dBtype | dB → dB, dBm → dBm |
Load Resistance (ohms) | R | passes through |
Guard Against Log Of Zero | fuzz | off → off, on → on |
Caveat (shown to the user): maps to the DSP System Toolbox's dB Conversion, simulated over all sixteen combinations of its four parameters and reproduced exactly. ONLY THE TWO FORWARD MODES CROSS: the Simulink block converts to decibels and has no inverse, so "dB to Amplitude" and "dB to Power" have no counterpart and are reported rather than translated. Two measured details worth carrying: the load resistance is IGNORED in the Power direction on both sides, and the block has no SampleTime parameter, so an explicit rate does not cross the bridge. Note dspmathops/dB Gain is a different block (a gain expressed in decibels) and is not this one
Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.
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).
Decibel Conversion -- the five MATLAB decibel functions as one elementwise block Amplitude to dB y = 10*log10(a*a / R) [+30 for dBm] db, mag2db Power to dB y = 10*log10(a) [+30 for dBm] pow2db dB to Amplitude y = sqrt(R * 10^((u [-30]) / 10)) db2mag dB to Power y = 10^((u [-30]) / 10) db2pow
a is the input, or the input plus one machine epsilon when the log-of-zero guard is on.
⚠ EVERYTHING BELOW WAS SIMULATED IN R2026a, NOT READ OFF A PAGE. dspmathops/dB Conversion was driven with [0.25 0.5 1 2 4 10 0.001 100] over all sixteen combinations of its four parameters, and then with [-2 -0.5 0 0.5 2 -1e-8] to find the edges. Three facts came out of that and each of them is a way to be wrong:
- THE TWO DIRECTIONS ARE NOT SYMMETRIC ABOUT ZERO. Amplitude squares before the logarithm,
so -2 gives the same +6.02 dB as +2. Power does NOT: it takes the log of the input, so a negative input is NaN and a zero is -Inf, and Simulink prints "Log of a negative number" rather than refusing. Writing Amplitude as 20*log10(u) -- the form mag2db uses, and the one a reader reaches for -- would give NaN on half of any zero-mean signal.
- THE LOAD RESISTANCE IS IGNORED IN THE POWER DIRECTION. R = 1 and R = 50 give the same
numbers to the last bit there. A power is already a power; only an amplitude needs a resistance to become one.
- THE GUARD ADDS eps TO THE INPUT, NOT TO THE LOGARITHM'S ARGUMENT. Measured at u = 0:
Amplitude gives -313.07 dB, which is 10*log10(eps*eps) and not 10*log10(eps); Power gives -156.54, which is 10*log10(eps). So the addition happens once, before the square.
TWO OF THE FOUR MODES CROSS AND TWO CANNOT: Simulink's block is the forward pair and has no inverse. The catalog maps the two forward values of the mode and leaves the two inverse ones unmapped, which the exporter REPORTS rather than translating into something wrong.
⚠ THE THREE HDL TARGETS ARE SIMULATION-ONLY real ARITHMETIC, quantizing only at the port boundary -- the same choice Recursive IIR makes and for a sharper reason: a logarithm has no Q16.16 form at all, and the answer's range is unbounded below (a zero input is -Inf before the guard and -313 dB after it). VHDL gets LOG10/EXP from ieee.math_real; Verilog and SystemVerilog get $log10/$exp. 10^x is emitted as exp(x*ln10) rather than as a power operator, because that spelling exists in all three.
ALGEBRAIC and elementwise: the output has the input's shape, one step is one conversion and nothing is held between samples. No state space -- a logarithm is not y = D*u.
Sample results#
8 sample(s) were non-finite (nan/inf) and are absent from the plot; they are in the table below and in the JSON.
| t | in ICoreDouble-Out-0 | out ICoreDouble-Out-0 |
|---|---|---|
| 0 | -2 | 6.021 |
| 0.4 | 0.5 | -6.021 |
| 0.8 | -2 | 6.021 |
| 1.2 | 0.5 | -6.021 |
| 1.6 | -2 | 6.021 |
| 2 | 0.5 | -6.021 |
| 2.4 | -2 | 6.021 |
| 2.8 | 0.5 | -6.021 |
| 3.2 | -2 | 6.021 |
| 3.6 | 0.5 | -6.021 |
| 4 | -2 | 6.021 |
| 4.4 | 0.5 | -6.021 |
| 4.8 | -2 | 6.021 |
| 5.2 | 0.5 | -6.021 |
Every 4th of 60 samples, from the table stimulus.
The same rig also ran:
| Stimulus | What it is | Output range |
|---|---|---|
impulse | Impulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair) | 0 … 0 |
ramp | Ramp: slope 1 from t = 0 | -20 … 15.42 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | -32.12 … -8.507e-5 |
step | Step: 0 -> 1 at t = 1 s | 0 … 0 |
Plotted: table — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample
Category static · sample time 0.1 · 60 steps · commit 3c100aff6f27235305db4ad4d572f32e342718ad · produced by docsSample --out <folder> --blocks Decibel_Conversion --steps 60 · data docs/generated/samples/Control_Systems__Spectral_Measurements__Decibel_Conversion.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).