Is there any matlab function to calculate moving mean square error?
7 views (last 30 days)
Show older comments
I am looking for a way to calculate mean square error for every 'n' sample in a signal of length N (total number of samples)
2 Comments
Jonas
on 30 Nov 2022
please make clear: do you calculate the least square line once and first and then you want the sliding window of mean error per n sample
OR
do you take a window of n samples, calculate least square line and want to measure the error of that part?
Answers (2)
Bruno Luong
on 30 Nov 2022
Assuming you have 2 signals S1 and S2 in 1 x N arrays:
N = 1000;
S1 = randn(1,N);
S2 = randn(1,N);
n = 10;
dS = S1 - S2;
RMS = sqrt(conv(dS.^2, ones(1,n)/n, 'valid'))
0 Comments
Mathieu NOE
on 30 Nov 2022
hello
I doubt that there is a code for that
try this :
(based on formula) :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1213343/image.png)
% dummy data
n=300;
x=linspace(0,2*pi,n);
f = cos(x) + 0.1*randn(1,n); % values of the model
y = smoothdata(f,'gaussian',30); % actual data
buffer = 10; % nb of samples in one buffer (buffer size)
overlap = 9; % overlap expressed in samples
%%%% main loop %%%%
m = length(f);
shift = buffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
mse(ci) = my_mse(f(start_index:stop_index) - y(start_index:stop_index)); %
end
xx = x(time_index); % new x axis
figure(1),
plot(x,f,xx,mse,'r*');
figure(1),
plot(x,f,'k',x,y,'b',xx,mse,'r');
legend('f data','y data','MSE');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function x_mse = my_mse(x)
x_mse = mean(x.^2);
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!