MSE is known as Mean Squared Error. Basically, it is an optimization problem that is predominately used in image processing, signal processing, etc. Also, it is used as a loss function in machine learning and deep learning.

MSE is calculated by squaring the difference between the two identical points (error). It is the comparative measurement between two images or signals.

To calculate MSE, we must need two signals or images as I mentioned before that it is for performance comparison.

Calculate MSE for 1D Signal

Now we will see how to calculate the MSE value for a 1D signal. The formula –

MSE=\frac{1}{n}\sum_{i=1}^{n}\left ( x_{i}-y_{i} \right )^{2}

Here, n is the number of elements, x_{i} is the original signal and y_{i} is the newly generated signal or a completely different signal.

Explanation of 1D MSE Calculation:

Assuming we have a 1D signal –


and another one –


So, the MSE value would be –

MSE=\frac{1}{4}{  (10-10)^{2}+(20-19)^{2}+(30-32)^{2}+(40-43)^{2}}=3.5

Python Code:

x = [10,20,30,40]
y = [10,19,32,43]
summation = 0  #variable to store the summation of differences
n = len(x) #finding total number of items in list
for i in range (0,n):  #looping through each element of the list
  difference = x[i] - y[i]  #finding the difference between observed and predicted value
  squared_difference = difference**2  #taking square of the differene 
  summation = summation + squared_difference  #taking a sum of all the differences
mse= summation/n  #dividing summation by total values to obtain average
print("Mean Square Error: ", mse)

Calculate MSE for 2D Signal

In the above example, we have seen MSE calculation for 1D signal. Suppose we need to calculate MSE for an image; in this situation we need to calculate MSE for 2D signal. The formula is –

 MSE=\frac{\sum \left [ I_{1}(m,n)-I_{2}(m,n)) \right ]^{2}}{M*N}

Here, M and N are the number of rows and columns in the input image or the calculated 2D signal. Those two images must be the same dimensional.