db — Convert voltage magnitude or power to decibels.
db(y) and db(y, 'voltage') compute 20*log10(abs(y)). db(y, 'power') computes 10*log10(y) for nonnegative real power. db(y, R) computes 10*log10(abs(y).^2 ./ R) for finite positive real resistance. The resistance form uses implicit expansion.
Syntax
yDb = db(y)
yDb = db(y, "voltage")
yDb = db(y, "power")
yDb = db(y, R)Inputs
y- Type
Any- Required
- Yes
- Description
- Input signal magnitude or power quantity.
modeOrR- Type
Any- Required
- No
- Default
"voltage"- Description
- Mode string ('voltage' or 'power') or positive real resistance reference.
Returns
yDb- Type
NumericArray- Description
- Real single or double decibel output, following the input or broadcast shape.
Errors
RunMat:db:InvalidArgument- When
- Inputs do not match supported db invocation forms.
- Message
db: invalid argument
RunMat:db:InvalidMode- When
- Mode string is not recognized or is not a scalar text value.
- Message
db: invalid mode
RunMat:db:InvalidInput- When
- Input signal cannot be interpreted as numeric magnitude data.
- Message
db: invalid input
Modes and values
Voltage mode accepts real or complex numeric signal data and uses magnitude. Power mode accepts only real, nonnegative signal values; negative or complex values raise an error. Resistance must be finite, positive, and real, and may be an array broadcast-compatible with the signal. Zero signal values produce negative infinity.
The result is real single if the signal or resistance has native single precision, and real double otherwise. Unary and text-mode results follow the signal shape; resistance-mode results have the broadcast shape. Integer, logical, complex-integer signals and typed-integer resistance are RunMat extensions gated by compat = 'runmat'; they are rejected in MATLAB compatibility mode.
Execution and placement
Decibel conversion executes on the host and is a fusion boundary. Resident input arrays are gathered from their owning provider before conversion. There is no GPU kernel for db; the result is host-resident.
The operation may suspend during a provider gather and may throw for invalid mode, signal, resistance, or incompatible array sizes. It has no workspace, filesystem, network, callback, user-interface, randomness, or time effect.
Examples
Voltage decibels
y = db(10); fprintf('%.0f\n', y);Expected output:
20Power decibels
p = db(100, 'power'); fprintf('%.0f\n', p);Expected output:
20Voltage across reference resistance
p = db(10, 50); fprintf('%.4f\n', p);Expected output:
3.0103Complex signal magnitude
z = db(3 + 4i); fprintf('%.4f\n', z);Expected output:
13.9794Preserve single precision
y = db(single([1 10]));RunMat integer signal extension
y = db(uint16([1 10]));Using db with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how db changes the result.
Run a small db example, explain the result, then change one input and compare the output.
FAQ
What is the default mode?⌄
Voltage mode, equivalent to db(y, 'voltage').
How are complex inputs handled?⌄
Voltage and resistance modes use the complex magnitude and return real values. Power mode rejects complex input.
What happens for zero input?⌄
The logarithm yields negative infinity.
Can resistance be an array?⌄
Yes. It must be real, finite, positive, and broadcast-compatible with the signal.
Related Control functions
Source you can read
Every RunMat function's source is public. Read exactly how db is executed, line by line, in Rust.
- View the source for db in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.