function [Dx,Dy] = DxDy(I)
%DXDY Calculates the sum of second derivative magnitudes of a postal image block.
%   [DX,DY] = DXDY(I) sums second derivative magnitudes in the x and y direction 
%   to enable classification of I.  
%
%   The second derivative magnitudes are obtained from the Savitzky-Golay filter.  
%
%   I is the matrix of pixel values of a grayscale image block.  
%
%   See also SGOLAY, SGOLAYFILT, FEATURES

%   Author(s): K. R. Varshney
%   School of Electrical and Computer Engineering, Cornell University
%   $Date: 2002/08/15 $

%use the values k=2 and F=5
F=5;
%s = fliplr(vander(-(F-1)./2:(F-1)./2));
%S = s(:,1:k+1);   % Compute the Vandermonde matrix
%M = (S'*S)\(S');
M = ...        
[   -3/35        12/35        17/35        12/35        -3/35    
    -1/5         -1/10          0           1/10         1/5     
     1/7         -1/14        -1/7         -1/14         1/7             ];

Iav_x = mean(I);
Iav_y = mean(I');

Iav_xb = buffer(Iav_x,F,F-1,'nodelay');
Iav_yb = buffer(Iav_y,F,F-1,'nodelay');

ax = M*Iav_xb;
ay = M*Iav_yb;

a2x = ax(3,:);
a2y = ay(3,:);

Dx = sum(abs(a2x));
Dy = sum(abs(a2y));
