from Adaptive-Degree Polynomial Filter (Savitzky-Golay Filter) by Jianwen Luo
Obtain the polynomial degree selected in the adaptive-degree polynomial filter.

hnstm=sgsdf_gram_poly_closed_form(n,s,t,m)
function hnstm=sgsdf_gram_poly_closed_form(n,s,t,m)
% n:      polynomial degree
% s:      derivative(differentiation) order (0=smoothing)
% t:      evaluation point (commonly,t=0,i.e.,smoothing or differentiation
%               at the central point of the 2*m+1 points)
% 2*m+1:  data point number
% hnstm:  convoluction weights  

global Pkmi_array

hnstm=[];
% i:      convolution weight of the i-th point (-m<=i<=m)
for i=-m:m
    hnstm=[hnstm hinstm(i,n,s,t,m)];
end

function value=hinstm(i,n,s,t,m)
global Pkmi_array
value=0;
for k=0:n
    value=value+(2*k+1)*gff(2*m,k)/gff(2*m+k+1,k+1)*Pkmi_array(k+2,m,i+m+1)*Pkmsi(k,m,s,t);%(see ref.[2])
end
 
function value=gff(a,b)
%generalized factorial function
if b==0
    value=1;
else
    value=prod(a-(0:b-1));
end

function value=Pkmsi(k,m,s,i)
global Pkmi_array
%s-th derivative of Gram polynomials
if s==0
    value=Pkmi_array(k+2,m,i+m+1);%Gram polynomials
else    
    if k==-1
        value=0;
    elseif k==0
        value=0;
    else
        value=2*(2*k-1)/k/(2*m-k+1)*(i*Pkmsi(k-1,m,s,i)+s*Pkmsi(k-1,m,s-1,i))-...
            (k-1)*(2*m+k)/k/(2*m-k+1)*Pkmsi(k-2,m,s,i);       
    end   
end

Contact us at files@mathworks.com