MATLAB: Does the SGOLAYFILT function not accept zeros as elements of the weighting vector w, in Signal Processing Toolbox 6.1 (R13+)

checkingerrorprocessingsgolayfiltsignalSignal Processing Toolboxvectorweightingzero

I am looking at the Savitzky-Golay (SGOLAY) filters to try and remove anomalous spikes from experimental data. To do so, I need to ignore the anomalous point and want to use a Savitzky-Golay filter to approximate the new point value.
I found that the function rejected any weighting vector that had a zero element. Looking at the code, I do not understand why this restriction is necessary. Is this just an overly strict error message or does the algorithm break down in some way if a zero weighting is given to a point?
If I remove the error checking that checks for zeros in the SGOLAY and SGOLAYFILT functions, the results seem reasonable. Is there any reason why the error checking on line 45 of SGOLAYFILT is:
if min(W) <= 0, error('All the elements of the weight vector must be greater than zero.'), end
instead of:
if min(W) < 0, error('All the elements of the weight vector must be greater than zero.'), end
(Note the replacement of "<=" with "<")

Best Answer

This is a weighted least-squares problem. The requirement for the weights to be positive is needed in order to ensure the "least-squares matrix" is positive definite and the system has a solution.
For example, if you change the error checking as suggested above, to allow for zero weights, and try this simple case:
[b,g]=sgolay(3,5,[1,0,0,0,1]);
you will run into matrix singularity issues as indicated by the following warning messages:
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
> In I:\1186207\sgolay.m at line 59
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
> In I:\1186207\sgolay.m at line 59
This is the reason we have this requirement.
You can make some of the weights "very small" if you want to achieve a similar effect as is required here. But it is important to keep in mind that the smaller the weight, the more ill-conditioned the problem will become.
This is due to the so-called inertia theorem for matrices; if
A = C'*D*C
then A will have the same number of positive eigenvalues as D, the same number of negative eigenvalues, and the same number of zero eigenvalues.
If D (in this case a diagonal matrix with the weights on the diagonal) has a zero eigenvalue (a weight equal to zero in this case), then A will be positive semidefinite (assuming no negative weights) and a solution to the least-squares problem is not guaranteed.
Related Question