MATLAB: Difference between movmad and med(abs(x-med(x)))

hampelMATLABmedfilt1movmad

Hello everyone,
I'm trying to implement my own version of the hampel filter and I'm having problems with the movmad (Moving Median Absolute Deviation) calculation, used in the original to define the threshold with the variable xmad.
Original hampel.m code (lines 74-84)
% compute the median absolute deviations and the corresponding medians over
% the size of the filter: ignore samples that contain NaN and truncate
% at the borders of the input
[xmad,xmedian] = movmadmed(x,2*k+1,1,'omitnan','central');
% scale the MAD by ~1.4826 as an estimate of its standard deviation
scale = -1 /(sqrt(2)*erfcinv(3/2));
xsigma = scale*xmad;
% identify points that are either NaN or beyond the desired threshold
xi = ~(abs(x-xmedian) <= nsigma*xsigma);
I have tested that xmad is equal to:
xmad=movmad(x,2*k+1);
As far as I understand, the documentation of movmad says that, in the below code, xmad and my_xmad should be equal:
size=2*k+1;
xmad=movmad(x,size);
my_xmad=medfilt1(abs(x-medfilt1(x,size)),size);
However, as you can see, I obtain different results:
Does anyone know what's the difference between movmad and the operations to obtain my_xmad?
Thanks in advance!

Best Answer

You are comparing 2 different equations. The documentation says the equation used in movmad is
You are using medfilt1, which is different than median (applies a third-order one-dimensional median filter). Test your comparison using median and see if it is closer.