34 views (last 30 days)

For curves in X-Y coordinate system where the Y axis is in decibels (dB), I want to calculate the mean squared error (MSE) between two curves? If the Y values were linear, I could use MSE equation. Can I use MSE when the y values are in dB or should I converts the Y values from dB to linear then compute the MSE and take it to dB?

For instance, Y1 is the y values (in dB) of one curve. Then, Y2 is Y1 values plus or minus a random number from -5 to 5. Therefore, Y2 samples are X dBs above or below those of Y1 where X is in the [-5,5] dB interval.

1) Now that two curves Y1 and Y2 are given in dB, to calculate their MSE, can I use the MSE equation directly or linear conversion is needed (script below)? If the Y values in dB, is there a better way to quantify the average difference than MSE?

Y1=-60:1:60; %y values in dB for curve 1

Y2=Y1+randi([-5,5],size(Y1)) %y values in dB for curve 2

MsedB1 = sum((Y1-Y2).^2)/numel(Y1); %is this MSE correct?

Y1_Lin = 10.^(Y1*0.1); %y values for curve 1 in linear

Y2_Lin = 10.^(Y2*0.1); %y values for curve 2 in linear

MsedB2 = 10*log10(sum((Y1_Lin-Y2_Lin).^2)/numel(Y1_Lin)); %or is this MSE correct?

David Goodmanson
on 1 Jan 2021

Hi MatG,

As with a lot of things, the answer depends. In your example, which is the first case below, the variation in power is not very extreme, which means that the two methods will not differ by very much. On the other hand, the second case is for a square wave that spends half its time around the value 1, and the other half of the time around the value 0, plus a bit of noise. Here the difference between the two methods is very large.

t = 0:.001:2*pi;

n = length(t);

r = 2*rand(1,n)-1; % random between +-1

% case 1

P = 10.^((5/10)*r);

figure(1)

plot(t,P)

grid on

mean_of_P_in_dB = 10*log10(mean(P))

mean_of_dB = mean(10*log10(P))

% mean_of_P_in_dB = 0.9531

% mean_of_dB = 0.0396

% case 2

y = (1+square(t))/2; % 1 or 0

P = (y +.001*r).^2; % add small amount of noise, 60 dB down

figure(2)

plot(t,P)

grid on

mean_of_P_in_dB = 10*log10(mean(P))

mean_of_dB = mean(10*log10(P))

% mean_of_P_in_dB = -3.0102

% mean_of_dB = -34.3295

In the second case, the average power is about 1/2, so the mean of P converted to dB is -3 dB. That is a reasonable evaluation of the situation. When direct averaging dB, the signal spends half its time at 0 dB and half down in the region of -60 dB, leading to the somewhat absurd conclusion that the average power is -34 dB. Make the noise 10 times smaller than that, and the supposed average power is now -44 dB. This is not reasonable. Most cases are like your example, though, and averaging dB can make sense.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/706078-means-squared-error-and-db#comment_1237728

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/706078-means-squared-error-and-db#comment_1237728

Sign in to comment.