function [A,B] = tpcoeff(X,Y,m)
% [A,B] = tpcoeff(X,Y,m)
% X vector of equally spaced abscissax in [-pi,pi], input.
% Y vector of ordinates, input.
% m degree of the trigonomitric polynomial, input.
% A coefficients of cos(nx), output.
% B coefficients of sin(nx), output.
n = length(X)-1;
max1 = fix((n-1)/2);
if m>max1, m=max1; end
A = zeros(1,m+1);
B = zeros(1,m+1);
Yends = (Y(1)+Y(n+1))/2;
Y(1) = Yends;
Y(n+1) = Yends;
A(1) = sum(Y);
for j = 1:m,
A(j+1) = cos(j*X)*Y';
B(j+1) = sin(j*X)*Y';
end
A = 2*A/n;
B = 2*B/n;
A(1) = A(1)/2;