function [y, yd , ydd]  = polyval_STderiv(p,pNew,x,Th0,Th1,Th2,Th3)

% function [y, yd, ydd] = polyval_STderiv(p,pNew,x,Th0,Th1,Th2,Th3)
%
% uses polyval to evaluate the derivative of the polynomial p and pNew at values of pixels of image 
% x that are between Th0 and Th1 and between Th1 and Th2. It creates the derivative soft tissue segmentation function
%
% Written to be used with sir_bh_os.m
%
% Jeff Fessler, University of Michigan
% Monica Abella and Cristobal Martinez, Universidad Carlos III de Madrid

 N=length(p);

 pdot = (N-1:-1:1).*p(1:N-1);
 pddot = (N-2:-1:1).*pdot(1:N-2);
 x2=x;

 y = polyval(p,x2).*(x>=Th2 & x<=Th3)+polyval(pNew,x2).*(x>=Th0 & x<=Th1);
 yd = polyval(pdot,x2).*(x>Th2 & x<Th3);
 ydd = polyval(pddot,x2).*(x>Th2 & x<Th3);
