"gp wr" <gpwr9k95@yahoo.com> wrote in message <hrd92o$cdn$1@fred.mathworks.com>...
> I am trying to interpolate equidistant data points with trigonometric functions (cos and sin). "Trigonometric interpolation" of equidistant data points typically means Fourier interpolation with preset frequencies of 2*Pi*k/N. This is NOT what I am looking for. Imagine noisefree data consisting of 4 datapoints y(i) where i=0..3. Theoretically, this data can be described by the following model function with 4 variables:
> y(i) = a + b*cos(w*i + phi)
> The task is to find a, b, w and phi such that the model function fits the data with zero error. I could derive simple formulas for these four parameters in the case of 4 data points and "singletone" model. A "twotone" model
> y(i) = a + b1*cos(w1*i + phi1) + b2*cos(w2*i + phi2)
> can describe 7 data points with zero error. But the derivation of equations for a, b1, b2, w1, w2, phi1 and phi2 is way too complex. Is there a matlab routine that can solve this task? In other words, given 7 noisefree data points generated by two unknown tones, the task is to fins the parameters of these tones.
>
> Thanks.
This is in answer to your request for a solution to the "twotone" problem. It turns out there is a direct solution to the problem that avoids iterative methods. For convenience I have used slightly different notation than yours. My u is your w1, v is w2, p is 3*w1+phi1, q is 3*w2+phi2, a is a, b is b1, and c is b2. Given these definitions, the equations below are equivalent to yours. The understanding here is that the unknown quantities, u, v, p, and q, are regarded as lying anywhere in the full circle of values between pi and +pi.
y1 = a + b*cos(3*u+p) + c*cos(3*v+q);
y2 = a + b*cos(2*u+p) + c*cos(2*v+q);
y3 = a + b*cos(u+p) + c*cos(v+q);
y4 = a + b*cos(p) + c*cos(q);
y5 = a + b*cos(u+p) + c*cos(v+q);
y6 = a + b*cos(2*u+p) + c*cos(2*v+q);
y7 = a + b*cos(3*u+p) + c*cos(3*v+q);
Whenever the y's are such that a solution is possible, there are actually 32 different solutions within the above full circle limitation. The matlab code below finds just one of these. The remaining solutions are obtained from five different kinds of alterations. First the sign of F can be reversed which has the effect of interchanging u and v. Next, each of the 'acos' functions always yields a nonnegative answer. The sign of each of them can independently be made negative for valid solutions. Finally the output of the 'atan' functions always has a positive cosine  that is, it lies in the first or fourth quadrants. If it is positive, pi can be subtracted, and otherwise pi can be added, and this will also provide valid solutions for either of the 'atan's. In all, there are 2*2*2*2*2 = 32 combinations of these five alternatives, all of which lead to valid solutions.
The matlab code to find what could be regarded as the "principal" solution out of the thirtytwo is as follows:
A1 = y1+y3y5y7;
A2 = 2*(y2+y6);
A3 = 4*(y3y5);
B1 = y1+2*y23*y3+4*y43*y5+2*y6y7;
B2 = 2*(y22*y3+2*y42*y5+y6);
B3 = 4*(y3+2*y4y5);
C = A2*B3A3*B2;
D = A1*B3A3*B1;
E = A1*B2A2*B1;
F = sqrt(D^24*C*E);
u = acos((D+F)/(2*C));
v = acos((DF)/(2*C));
p = atan(((y2y6)2*(y3y5)*cos(v))*(cos(u)1)/...
(sin(u)*((y22*y4+y6)2*(y32*y4+y5)*(cos(v)+1))));
q = atan(((y2y6)2*(y3y5)*cos(u))*(cos(v)1)/...
(sin(v)*((y22*y4+y6)2*(y32*y4+y5)*(cos(u)+1))));
c = ((y2y6)(y3y5)*2*cos(u))/(4*sin(v)*sin(q)*(cos(v)cos(u)));
b = ((y2y6)(y3y5)*2*cos(v))/(4*sin(u)*sin(p)*(cos(u)cos(v)));
a = y4b*cos(p)c*cos(q);
It should be noted that not all sets of ynumbers possess solutions. If the quantity D^24*C*E in the square root is negative, there is no solution. Also if either argument in the 'acos' functions fails to fall between 1 and +1, there can be no solution.
The above solution was found by combining the yequations in various ways so as to first eliminate a, then b and c, and finally p and q, resulting in the two grand simultaneous equations
A1 + A2*(cos(u)+cos(v)) + A3*cos(u)*cos(v) = 0
B1 + B2*(cos(u)+cos(v)) + B3*cos(u)*cos(v) = 0
where these A's and B's refer to the quantities computed above. The solution to these two is a common quadratic equation in both cos(u) and cos(v), with these quantities being the two different roots of this quadratic. The remaining unknowns, a, b, c, p, and q, were determined from some of the intermediate equations obtained along the way prior to their being eliminated.
You can easily subject the above algorithm to a numeric test by first creating temporary random values for u, v, p, q, c, b, and a in order to produce yvalues which would then be guaranteed to have valid solutions. Then the above code could produce its own u, v, p, q, c, b, and a values, which would be different from the random ones first generated, about 31 times out of 32. The new unknown values from this last can then be used to generate a new set of yvalues which should be the same (within round off errors) as the original yvalues.
Roger Stafford
