function yt = ftrunc(y,M)
% gh = ftrunc(fh,M)
% truncate frequency vector to length M by removing higher frequencies
% corresponds to least squares approximation with lower frequencies

N = length(y);
s = size(y);
y = y(:);             % use column vector
if M>N
  error('must have M<=length(fh)')
end
k = floor(M/2);
yt = [y(1:k+1); zeros(M-k-1,1)] + [zeros(M-k,1); y(N-k+1:N)];
if s==[1,N]           % if y was row vector return row vector
  yt = yt.';
end
