# How to convert magnitude to dB in matlab

I exported a diagram from FDTD simulation, and now I want to change the magnitude to dB, here is the exported codes:

ax1 = axes;

plot(lum.x0,lum.y0)

set(ax1, 'XLim', [1.49806e-06 1.60194e-06])

set(ax1, 'YLim', [-0.0232806 1.22328])

set(ax1,'XGrid', 'on')

set(ax1,'YGrid', 'on')


and the ploted spectrum is shown below

I just wondering how to convert the magnitude to dB? I know there is a function called mag2dB, but i just dont know how to use it. any help would be very appreciate.

• try semilogy(lum.x0,lum.y0) FYI, there is also semilogx() – AlexTP Sep 1 '20 at 17:40

valueInDB = 10*log10(magnitude.*conj(magnitude));


Direct conversion from magnitude to dB is done by $$20\text{log}_{10}\big(|x|\big)$$.

I will highlight the parallel to Hilmar's answer by saying that the conversion from power to dB is done by $$10\text{log}_{10}\big(|x|^2\big)$$, so by following Hilmar you do $$10\text{log}_{10}\big(xx^*\big)=10\text{log}_{10}\big(|x|^2 \big)$$ which can be simplified to $$20\text{log}_{10}\big(|x| \big)$$.

MATLAB has a function mag2db which accomplishes this conversion, or you can simply do 20*log10(abs(lum.y0)).

ax1 = axes;

plot(lum.x0, mag2db(lum.y0))

set(ax1, 'XLim', [1.49806e-06 1.60194e-06])

set(ax1, 'YLim', [-0.0232806 1.22328])

set(ax1,'XGrid', 'on')

set(ax1,'YGrid', 'on')

• thank you so much for your help! – Dong junkai Sep 2 '20 at 2:20