# RMS value of a signal with a DC offset in MATLAB

t = 0:0.001:1;
y1 = 400 * sin(2*pi*20*t);
y2 = 200 + 400 * sin(2*pi*20*t);
plot(t,y1)
hold on
plot(t,y2,'-g')
grid on;
rms_y1 = sqrt(mean(y1.^2))
rms_y2 = sqrt(mean(y2.^2))


In the above MATLAB code there are two signals y1 and y2.

y1 is a sinusoidal signal 20 Hz with 400V amplitude.

y2 is a signal which is nothing but y1 + 200V DC offset.

So I was thinking the "rms of y2" would be equal to "rms of y1 + 200". Since rms of a constant is the constant itself.

But rms of y1 is 282.7014; and rms of y2 turns out to be 346.2948 (not 282 + 200).

What am I doing wrong here?

• you are possibly confusing RMS with variance. These two signals do have the same variance, but their RMS also depends on their DC points. Hence the computation is right according to $X_{RMS} = \sqrt { \frac{1}{T} { \int_{0}^{T}{x^2(t) dt} } }$ – Fat32 Jun 8 '16 at 13:35

RMS values don't simply add up. The RMS value of $N$ discrete samples $x[n]$ (assuming $0\le n<N$) is

$$x_{RMS}=\sqrt{\frac{1}{N}\sum_{n=0}^{N-1}x^2[n]}\tag{1}$$

Now if you have $y[n]=x[n]+c$ with some constant $c$, the RMS value of $y[n]$ is

\begin{align}y_{RMS}&=\sqrt{\frac{1}{N}\sum_{n=0}^{N-1}y^2[n]}\\&=\sqrt{\frac{1}{N}\sum_{n=0}^{N-1}(x[n]+c)^2}\\&=\sqrt{\frac{1}{N}\sum_{n=0}^{N-1}(x^2[n]+2cx[n]+c^2)}\\&=\sqrt{\frac{1}{N}\sum_{n=0}^{N-1}x^2[n]+\frac{2c}{N}\sum_{n=0}^{N-1}x[n]+\frac{1}{N}\sum_{n=0}^{N-1}c^2}\tag{2}\end{align}

If we define the mean value of $x[n]$ as

$$m_x=\frac{1}{N}\sum_{n=0}^{N-1}x[n]\tag{3}$$

then from $(2)$ the square of the RMS value of $y[n]$ can be expressed as

$$y^2_{RMS}=x^2_{RMS}+2cm_x+c^2\tag{4}$$

So if you assume that the mean of $x[n]$ equals zero, the squares of the RMS values add up:

$$y^2_{RMS}=x^2_{RMS}+c^2\tag{5}$$

You can easily verify Eq. $(5)$ using your example:

$$(282.7014)^2+(200)^2=(346.2948)^2\tag{6}$$

• so the eq. sqrt(mean(y.^2)) is correct ? thanks – user16307 Jun 8 '16 at 14:07
• @user16307: Yes, it is (for real-valued vectors), but there's actually also the Matlab command 'rms'. – Matt L. Jun 8 '16 at 14:13
• mine is Matlab 6 it doesnt have that function – user16307 Jun 8 '16 at 14:15

If your y2 was more like:
y2 = 200*sign(sin(2*pi*20*t)) + 400 * sin(2*pi*20*t);