Qu

mag2db

Builtin index · Signal processing

result = mag2db(x)

Input: x · Output: result

What it is

x: a number or vector (amplitude/voltage ratio).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = mag2db(x)
x number or vectornot documented yet
result —
Returns the same shape: 20*log10(x). AMPLITUDE/field-quantity convention.

Examples

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

  print("{mag2db(2)} dB")   # 6.0206 -- a factor-of-2 voltage ratio

  print("{pow2db(2)} dB")   # 3.0103 -- a factor-of-2 power ratio

output
6.0206 dB
3.0103 dB

from numerics-dsp

fs = 1000
b = butter(4, "low", 100, fs)
h = freqz(b, 512)
f = linspace(0, fs / 2, 512)
plot(f, mag2db(abs(h)), color = "#4169e1", width = 2)
xscale("log")
ylim(-80, 5)
xlabel("frequency (Hz)")
ylabel("magnitude (dB)")
title("4th-order Butterworth, 100 Hz cutoff")
output
· plot: figure 9 (512 points)
-80 -60 -40 -20 0 0 150 300 450 Magnitude Response Frequency (Hz) Magnitude (dB) -150 -100 -50 0 50 100 150 0 150 300 450 Phase Response Frequency (Hz) Phase (deg) 0 4 8 12 16 0 150 300 450 Group Delay Frequency (Hz) Delay (samples) -120 -80 -40 0 40 10−272 10−204 10−136 10−68 100 x=-0.135, y=0.991 x=-0.135, y=-0.991 x=0.57, y=0.821 x=0.57, y=-0.821 4th-order Butterworth, 100 Hz cutoff frequency (Hz) magnitude (dB)

from signal-processing

fs     = 1000
t      = (0 to 999) / fs
signal = sin(2 * pi * 50 * t) + 0.5 * sin(2 * pi * 220 * t) + 0.1 * randn(1000, seed = 2)
lp = butter(4, "low", 100, fs)             # 4th-order lowpass, 100 Hz cutoff

notch = butter(2, "notch", [45, 55], fs)   # removes 45-55 Hz mains hum


filtered = sosfilt(lp, signal)             # causal, one pass

zero_phase = filtfilt(lp, signal)          # zero-phase, double effective order

print("lp is stable: {is_stable(lp)}, notch is stable: {is_stable(notch)}")

H = freqz(lp, 512)
plot(linspace(0, fs/2, 512), mag2db(abs(H)))
output
lp is stable: true, notch is stable: true
· plot: figure 4 (512 points)
-200 -100 0 100 200 300 400 500 -200 0 200 400 600 800 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=$10^{-9}$ x=0.21, y=$1.1\times10^{-9}$ x=0.21, y=$1.2\times10^{-9}$ x=0.21, y=$1.3\times10^{-9}$ x=0.21, y=$1.4\times10^{-9}$ x=0.21, y=$1.5\times10^{-9}$ x=0.21, y=$1.7\times10^{-9}$ x=0.21, y=$1.8\times10^{-9}$ x=0.21, y=$2\times10^{-9}$ x=0.21, y=$2.1\times10^{-9}$ x=0.21, y=$2.3\times10^{-9}$ x=0.21, y=$2.5\times10^{-9}$ x=0.21, y=$2.7\times10^{-9}$ x=0.21, y=$2.9\times10^{-9}$ x=0.21, y=$3.2\times10^{-9}$ x=0.21, y=$3.4\times10^{-9}$ x=0.21, y=$3.7\times10^{-9}$ x=0.21, y=$4\times10^{-9}$ x=0.21, y=$4.4\times10^{-9}$ x=0.21, y=$4.8\times10^{-9}$ x=0.21, y=$5.2\times10^{-9}$ x=0.21, y=$5.6\times10^{-9}$ x=0.21, y=$6.1\times10^{-9}$ x=0.21, y=$6.6\times10^{-9}$ x=0.21, y=$7.1\times10^{-9}$ x=0.21, y=$7.7\times10^{-9}$ x=0.21, y=$8.4\times10^{-9}$ x=0.21, y=$9.1\times10^{-9}$ x=0.21, y=$9.9\times10^{-9}$ x=0.21, y=$1.1\times10^{-8}$ x=0.21, y=$1.2\times10^{-8}$ x=0.21, y=$1.3\times10^{-8}$ x=0.21, y=$1.4\times10^{-8}$ x=0.21, y=$1.5\times10^{-8}$ x=0.21, y=$1.6\times10^{-8}$ x=0.21, y=$1.7\times10^{-8}$ x=0.21, y=$1.9\times10^{-8}$ x=0.21, y=$2\times10^{-8}$ x=0.21, y=$2.2\times10^{-8}$ x=0.21, y=$2.4\times10^{-8}$ x=0.21, y=$2.6\times10^{-8}$ x=0.21, y=$2.8\times10^{-8}$ x=0.21, y=$3.1\times10^{-8}$ x=0.21, y=$3.3\times10^{-8}$ x=0.21, y=$3.6\times10^{-8}$ x=0.21, y=$3.9\times10^{-8}$ x=0.21, y=$4.2\times10^{-8}$ x=0.21, y=$4.6\times10^{-8}$ x=0.21, y=$5\times10^{-8}$ x=0.21, y=$5.4\times10^{-8}$ x=0.21, y=$5.9\times10^{-8}$ x=0.21, y=$6.4\times10^{-8}$ x=0.21, y=$6.9\times10^{-8}$ x=0.21, y=$7.5\times10^{-8}$ x=0.21, y=$8.1\times10^{-8}$ x=0.21, y=$8.8\times10^{-8}$ x=0.21, y=$9.5\times10^{-8}$ x=0.21, y=$10^{-7}$ x=0.21, y=$1.1\times10^{-7}$ x=0.21, y=$1.2\times10^{-7}$ x=0.21, y=$1.3\times10^{-7}$ x=0.21, y=$1.4\times10^{-7}$ x=0.21, y=$1.5\times10^{-7}$ x=0.21, y=$1.7\times10^{-7}$ x=0.21, y=$1.8\times10^{-7}$ x=0.21, y=$2\times10^{-7}$ x=0.21, y=$2.1\times10^{-7}$ x=0.21, y=$2.3\times10^{-7}$ x=0.21, y=$2.5\times10^{-7}$ x=0.21, y=$2.7\times10^{-7}$ x=0.21, y=$3\times10^{-7}$ x=0.21, y=$3.2\times10^{-7}$ x=0.21, y=$3.5\times10^{-7}$ x=0.21, y=$3.8\times10^{-7}$ x=0.21, y=$4.1\times10^{-7}$ x=0.21, y=$4.4\times10^{-7}$ x=0.21, y=$4.8\times10^{-7}$ x=0.21, y=$5.2\times10^{-7}$ x=0.21, y=$5.7\times10^{-7}$ x=0.21, y=$6.1\times10^{-7}$ x=0.21, y=$6.7\times10^{-7}$ x=0.21, y=$7.2\times10^{-7}$ x=0.21, y=$7.8\times10^{-7}$ x=0.21, y=$8.5\times10^{-7}$ x=0.21, y=$9.2\times10^{-7}$ x=0.21, y=$10^{-6}$ x=0.21, y=$1.1\times10^{-6}$ x=0.21, y=$1.2\times10^{-6}$ x=0.21, y=$1.3\times10^{-6}$ x=0.21, y=$1.4\times10^{-6}$ x=0.21, y=$1.5\times10^{-6}$ x=0.21, y=$1.6\times10^{-6}$ x=0.21, y=$1.8\times10^{-6}$ x=0.21, y=$1.9\times10^{-6}$ x=0.21, y=$2.1\times10^{-6}$ x=0.21, y=$2.2\times10^{-6}$ x=0.21, y=$2.4\times10^{-6}$ x=0.21, y=$2.6\times10^{-6}$ x=0.21, y=$2.9\times10^{-6}$ x=0.21, y=$3.1\times10^{-6}$ x=0.21, y=$3.4\times10^{-6}$ x=0.21, y=$3.6\times10^{-6}$ x=0.21, y=$4\times10^{-6}$ x=0.21, y=$4.3\times10^{-6}$ x=0.21, y=$4.7\times10^{-6}$ x=0.21, y=$5\times10^{-6}$ x=0.21, y=$5.5\times10^{-6}$ x=0.21, y=$5.9\times10^{-6}$ x=0.21, y=$6.4\times10^{-6}$ x=0.21, y=$7\times10^{-6}$ x=0.21, y=$7.6\times10^{-6}$ x=0.21, y=$8.2\times10^{-6}$ x=0.21, y=$8.9\times10^{-6}$ x=0.21, y=$9.6\times10^{-6}$ x=0.21, y=$10^{-5}$ x=0.21, y=$1.1\times10^{-5}$ x=0.21, y=$1.2\times10^{-5}$ x=0.21, y=$1.3\times10^{-5}$ x=0.21, y=$1.4\times10^{-5}$ x=0.21, y=$1.6\times10^{-5}$ x=0.21, y=$1.7\times10^{-5}$ x=0.21, y=$1.8\times10^{-5}$ x=0.21, y=$2\times10^{-5}$ x=0.21, y=$2.2\times10^{-5}$ x=0.21, y=$2.3\times10^{-5}$ x=0.21, y=$2.5\times10^{-5}$ x=0.21, y=$2.8\times10^{-5}$ x=0.21, y=$3\times10^{-5}$ x=0.21, y=$3.2\times10^{-5}$ x=0.21, y=$3.5\times10^{-5}$ x=0.21, y=$3.8\times10^{-5}$ x=0.21, y=$4.1\times10^{-5}$ x=0.21, y=$4.5\times10^{-5}$ x=0.21, y=$4.9\times10^{-5}$ x=0.21, y=$5.3\times10^{-5}$ x=0.21, y=$5.7\times10^{-5}$ x=0.21, y=$6.2\times10^{-5}$ x=0.21, y=$6.7\times10^{-5}$ x=0.21, y=$7.3\times10^{-5}$ x=0.21, y=$7.9\times10^{-5}$ x=0.21, y=$8.6\times10^{-5}$ x=0.21, y=$9.3\times10^{-5}$ x=0.21, y=$10^{-4}$ x=0.21, y=$1.1\times10^{-4}$ x=0.21, y=$1.2\times10^{-4}$ x=0.21, y=$1.3\times10^{-4}$ x=0.21, y=$1.4\times10^{-4}$ x=0.21, y=$1.5\times10^{-4}$ x=0.21, y=$1.6\times10^{-4}$ x=0.21, y=$1.8\times10^{-4}$ x=0.21, y=$1.9\times10^{-4}$ x=0.21, y=$2.1\times10^{-4}$ x=0.21, y=$2.3\times10^{-4}$ x=0.21, y=$2.5\times10^{-4}$ x=0.21, y=$2.7\times10^{-4}$ x=0.21, y=$2.9\times10^{-4}$ x=0.21, y=$3.1\times10^{-4}$ x=0.21, y=$3.4\times10^{-4}$ x=0.21, y=$3.7\times10^{-4}$ x=0.21, y=$4\times10^{-4}$ x=0.21, y=$4.3\times10^{-4}$ x=0.21, y=$4.7\times10^{-4}$ x=0.21, y=$5.1\times10^{-4}$ x=0.21, y=$5.5\times10^{-4}$ x=0.21, y=$6\times10^{-4}$ x=0.21, y=$6.5\times10^{-4}$ x=0.21, y=$7\times10^{-4}$ x=0.21, y=$7.6\times10^{-4}$ x=0.21, y=$8.2\times10^{-4}$ x=0.21, y=$8.9\times10^{-4}$ x=0.21, y=$9.7\times10^{-4}$ x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.209, y=0.002 x=0.209, y=0.002 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.004 x=0.208, y=0.004 x=0.208, y=0.004 x=0.208, y=0.004 x=0.207, y=0.004 x=0.207, y=0.005 x=10.059, y=0 x=10.091, y=0.039 x=10.006, y=-0.006 x=10.007, y=-0.066 x=10.032, y=-0.139 x=9.955, y=-0.088 x=10.166, y=0.138 x=10.235, y=0.047 x=10.035, y=-0.037 x=9.958, y=-0.063 x=9.992, y=-0.016 x=10.041, y=0.004 x=9.963, y=-0.055 x=9.91, y=-0.022 x=9.93, y=-0.028 x=9.984, y=-0.042 x=10.01, y=0.018 x=10, y=0.013 x=9.987, y=0.009 x=9.94, y=-0.068 x=9.936, y=0.037 x=10.01, y=0.152 x=9.972, y=0.1 x=9.988, y=0.047 x=9.935, y=0.037 x=9.981, y=0.026 x=9.987, y=-0.086 x=9.924, y=-0.042 x=9.918, y=-0.055 x=10.037, y=-0.15 x=10.068, y=-0.003 x=9.951, y=0.01 x=10.04, y=-0.031 x=10.087, y=-0.038 x=9.849, y=0.041 x=9.908, y=0.014 x=10.006, y=0.012 x=10.059, y=-0.062 x=9.993, y=-0.065 x=9.973, y=0.022 x=9.929, y=0.059 x=9.965, y=-0.002 x=9.953, y=-0.086 x=9.9, y=-0.097 x=9.962, y=0.03 x=9.972, y=-0.116 x=10.012, y=-0.043 x=10.028, y=-0.004 x=9.924, y=0.081 x=9.839, y=0.07 x=9.858, y=0.049 x=9.977, y=-0.097 x=10.045, y=-0.004 x=9.965, y=0.081 x=10.04, y=0.049 x=10.018, y=-0.039 x=9.858, y=0.01 x=9.942, y=0.008 x=9.934, y=0.052 x=9.96, y=0.109 x=10.048, y=0.073 x=10.06, y=0.107 x=9.992, y=0.093 x=10.026, y=0.031 x=9.978, y=-0.018 x=10.023, y=$2.8\times10^{-4}$ x=10.092, y=0.013 x=10.028, y=0.037 x=9.94, y=0.119 x=9.926, y=-0.023 x=10.001, y=0.021 x=9.998, y=0.032 x=10.056, y=-0.067 x=10.024, y=-0.025 x=9.99, y=-0.024 x=9.898, y=-0.066 x=9.973, y=0.015 x=9.952, y=0.04 x=9.984, y=-0.009 x=9.994, y=-0.044 x=9.996, y=-0.102 x=10.108, y=-0.073 x=10.029, y=0.05 x=9.994, y=-0.027 x=9.997, y=-0.104 x=10.051, y=-0.076 x=10.023, y=-0.043 x=9.89, y=-0.008 x=9.8, y=-0.017 x=9.961, y=-0.019 x=10.104, y=-0.046 x=10.095, y=0.115 x=10.092, y=0.113 x=10.08, y=0.151 x=10, y=-0.008 x=9.949, y=-0.002 x=9.95, y=-0.027 x=10.042, y=0.006 x=10.067, y=-0.041 x=10.005, y=-0.024 x=10.029, y=-0.044 x=10.013, y=-0.034 x=10.063, y=0.037 x=9.963, y=-0.045 x=10.013, y=-0.022 x=10.054, y=0.061 x=10.003, y=0.062 x=9.965, y=0.053 x=9.889, y=0.017 x=9.95, y=-0.068 x=9.974, y=-0.065 x=9.929, y=0.087 x=9.95, y=0.067 x=9.965, y=-0.008 x=9.932, y=-0.009 x=9.967, y=0.025 x=9.942, y=0.014 x=9.961, y=0.034 x=9.915, y=0.045 x=10.019, y=-0.026 x=10.008, y=0.022 x=10.045, y=0.017 x=9.918, y=0.001 x=9.947, y=0.004 x=9.904, y=0.061 x=10.003, y=0.099 x=9.998, y=0.088 x=10.033, y=-0.029 x=10.051, y=0 frequency (Hz)

from signal-processing

fs = 2000
t = linspace(0, 1, fs)
x = sin(2 * pi * 120 * t) + 0.4 * sin(2 * pi * 350 * t) + 0.2 * randn(fs, seed = 4)
X = rfft(x)
f = linspace(0, fs / 2, len(X))
print("largest peak at {round(f[argmax(abs(X))], 1)} Hz")
plot(f, mag2db(abs(X)), color = "#4169e1")
xlabel("frequency (Hz)")
ylabel("magnitude (dB)")
title("two tones in noise")
output
largest peak at 120 Hz
· plot: figure 10 (1001 points)
-80 -60 -40 -20 0 0 150 300 450 Magnitude Response Frequency (Hz) Magnitude (dB) -150 -100 -50 0 50 100 150 0 150 300 450 Phase Response Frequency (Hz) Phase (deg) 0 4 8 12 16 0 150 300 450 Group Delay Frequency (Hz) Delay (samples) -120 -80 -40 0 40 10−272 10−204 10−136 10−68 100 x=-0.135, y=0.991 x=-0.135, y=-0.991 x=0.57, y=0.821 x=0.57, y=-0.821 two tones in noise frequency (Hz) magnitude (dB)

from signal-processing

How it works

dB conversions — MATLAB's own naming (mag2db/db2mag for amplitude/voltage ratios, pow2db/db2pow for power ratios), chosen deliberately over a single ambiguous db(x) — whether a ratio is a power or an amplitude changes the factor of 10 vs. 20, and a bare db() would hide which one it picked.

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