function y = deseas1(x,freq,pad);
%DESEAS1   seasonal adjustment using procedure in Sims (JASA, 1974)
%          y = deseas1(x,freq,pad) seasonally adjusts the series x which
%          is sampled at frequency freq times per year (e.g., monthly data 
%          has freq=12, quarterly data has freq=4).   The series is padded
%          with pad zeros before a fast Fourier transform is run. If
%          y = deseas1(x,freq) is used, then pad is set equal to the
%          smallest integer m such that 2^m > 2*length(x)/freq.

%          Ellen McGrattan, 10-06-97

n          = length(x);
if nargin < 3;
  i        = ceil(log(2*n/freq)/log(2));
  pad      = 2^i*freq - n;
end;
m          = n+pad;
k          = m/freq;
l          = freq/2;
cnst       = ones(n,1);
t          = [1:n]';
[b,s,d]    = ols([cnst,t],x);
removed    = b(1)*cnst+b(2)*t;
y          = fft([d;zeros(pad,1)]);
z          = ones(m,1);
z(1:l+1)   = zeros(l+1,1);
z(m-l+1:m) = zeros(l,1);
for j=1:freq-1;
  z(k*j-l+1:k*j+l+1) = zeros(freq+1,1);
end; 
y          = y.*z;
y          = ifft(y);
y          = real(y(1:n))+removed;
