function q = shiftpoly(p,d,s)
% q = shiftpoly(p,d,s)
%   p is a vector of coefficients defining a polynomial 
%    y(x) = p(1)*x^(n-1) + p(2)*x^(n-2) + ... + p(n)
%   return q as the vector of coefficients that shift the function
%   by d so that 
%    y2(x) = q(1)*x^(n-1) + q(2)*x^(n-2) + ... + q(n) = y(x + d)
%  Optional s also scales x by s.
% 2011-11-22 Dan Ellis dpwe@ee.columbia.edu

if nargin < 3
  s = 1;
end

n = length(p);

q = zeros(1, n);

pas = pascal(n,1);  % (1-x)^n coefficients, left-justified

dd = d.^[0:(n-1)];

for i = 1:n
  % start with lowest power of x (last value of p)
  ni = n-(i-1);
  cfs = [zeros(1,ni-1),dd(1,1:i).*pas(i,1:i)];
  q = q + p(ni)*cfs;
end

q = q .* (s.^[(n-1):-1:0]);
