Is there a computationally less expensive way to calculate RMS error between two signals? Root-mean-square error (RMS error) between two signals can be calculated as given:
${\text{RMS}(x_\textrm{ actual}(t)-x_\textrm{ reference}(t))}$
When you want to calculate within a sliding window, you iterate the equation above within a loop.
However, this is super expensive. Is there a way to calculate RMS error in a less costly way?
 A: For ease of notation call one of the signals (which may be a time series, image, or other regular dataset of interest) $x$ and the other $y$.  By definition, the root mean square (RMS) of any set of $n$ values $(z_i)$ satisfies
$$\text{RMS}(z)^2 = \frac{1}{n}\sum_i^n (z_i - \bar{z})^2$$
where $\bar{z}$ is the mean difference,
$$\bar{z} = \frac{1}{n}\sum_i^n z_i.$$
(Thus you would take the square root of the right hand side to obtain the RMS.)  Algebraically the RMS formula equals
$$\text{RMS}(z)^2 = \left(\frac{1}{n}\sum_i^n z_i^2\right) - \bar{z}^2 = \bar{z^2} - (\bar{z})^2.$$
The expression $\bar{z^2}$ is the mean of the $z_i^2$.
In the present case, $z_i = x_i - y_i$ represent the differences of the signals, point by point (or time by time), and the subscripts range over moving windows.  The last formula shows that we only need an efficient way to compute averages of two signals, $z_i$ itself and its square $z_i^2$.  A moving average is a convolution with a kernel and that is efficiently carried out using a Fast Fourier Transform (FFT).  Whence an efficient workflow is


*

*Compute the FFT of the kernel representing the moving window. (This can be done once and for all when processing multiple signals.)

*Subtract $y$ from $x$ (pointwise) to form $z$.

*Compute FFTs of $z$ and $z^2$.

*Multiply (pointwise) each of the FFTs in (2) with the kernel FFT from (1).

*Obtain the inverse FFT (which is just a scaled version of the FFT itself) of the two products.

*Subtract the square of the first from the second.

*Take the square root (pointwise).
An example, in R, of using the FFT in this way to obtain moving window averages of a 2D raster appears at https://gis.stackexchange.com/a/16143/664: see the two lines of the transition function.
In one dimension, using a discrete signal of $N$ sequential values, the computational effort is $O(N\log(N))$ for each FFT and its inverse and for all the other (pointwise) operations of squaring, multiplying, subtracting, and taking the square root is $O(N)$--all with small implicit constants of proportionality--making the whole procedure $O(N\log(N))$.  The direct way to compute these moving-window RMS values with a window containing $K$ places is $O(NK)$.  Thus, the FFT method excels when $\log(N) \ll K$. Typically the transition occurs somewhere around $K\approx 10$ to $100$ or so, depending on $N$, the number of dimensions of the signal (one for time series, two for images, and so on), how well the FFT is implemented, and -- to a small extent -- on whether $N$ is just less than or just greater than a power of $2$.  (Ideally you want it just less than or equal to a power of $2$.)
