Home > atmlab > math > polysinfit.m

polysinfit

PURPOSE ^

POLYSINFIT Combined polynomial and sinusiodal fit

SYNOPSIS ^

function [p,s,A] = polysinfit(x,y,n,ls,w)

DESCRIPTION ^

 POLYSINFIT   Combined polynomial and sinusiodal fit

    The function works as *polyfit*, but sinusiodal fits can be made in
    parallel. Data and polynomial varaibles exactly as for *polyfit*. The
    sinusiodal part is defined by specifying the period length of each
    component. A sin and cosine term for each period length is fitted, where
    the results is returned as s=[s1,c1,s2,c2,...] where s1 is the amplitude
    for first sine term, c1 amplitude for first cosine term etc.

    The fit of y(x) is obtained as A*[p;s].

    The optional argument *w* allows to consider the uncertainty for each
    data point. The function *lscov* is then used, and *w* fits the third
    argument of that function. If the uncertainties are uncorrelated, *w*
    can be set to be a vector where the standard choice is to set the
    weights to 1/sigma^2 (the inverse variance). 

 FORMAT   [p,s,A] = polysinfit(x,y,n,ls)
        
 OUT   p    As for *polyfit*.
       s    Sine and cosine amplitudes.
       A    Fitting matrix.
 IN    x    As for *polyfit*.
       y    As for *polyfit*.
       n    As for *polyfit*.
       ls   Period length of sinusoidal components.
 OPT   w    Fitting weight for each data point in *y*. As passed on as 
            argument three of *lscov* (there called W or V).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

polysinfit.m

SOURCE CODE ^

0001 % POLYSINFIT   Combined polynomial and sinusiodal fit
0002 %
0003 %    The function works as *polyfit*, but sinusiodal fits can be made in
0004 %    parallel. Data and polynomial varaibles exactly as for *polyfit*. The
0005 %    sinusiodal part is defined by specifying the period length of each
0006 %    component. A sin and cosine term for each period length is fitted, where
0007 %    the results is returned as s=[s1,c1,s2,c2,...] where s1 is the amplitude
0008 %    for first sine term, c1 amplitude for first cosine term etc.
0009 %
0010 %    The fit of y(x) is obtained as A*[p;s].
0011 %
0012 %    The optional argument *w* allows to consider the uncertainty for each
0013 %    data point. The function *lscov* is then used, and *w* fits the third
0014 %    argument of that function. If the uncertainties are uncorrelated, *w*
0015 %    can be set to be a vector where the standard choice is to set the
0016 %    weights to 1/sigma^2 (the inverse variance).
0017 %
0018 % FORMAT   [p,s,A] = polysinfit(x,y,n,ls)
0019 %
0020 % OUT   p    As for *polyfit*.
0021 %       s    Sine and cosine amplitudes.
0022 %       A    Fitting matrix.
0023 % IN    x    As for *polyfit*.
0024 %       y    As for *polyfit*.
0025 %       n    As for *polyfit*.
0026 %       ls   Period length of sinusoidal components.
0027 % OPT   w    Fitting weight for each data point in *y*. As passed on as
0028 %            argument three of *lscov* (there called W or V).
0029 
0030 
0031 % 2009-11-19   Created by Patrick Eriksson.
0032 
0033 function [p,s,A] = polysinfit(x,y,n,ls,w)
0034 
0035 if ~isequal(size(x),size(y))
0036   error( 'The *x* and *y* vectors must have the same size.' );
0037 end
0038 
0039 x = x(:);
0040 y = y(:);
0041   
0042 A = zeros( length(y), n+1+length(ls)*2 );
0043 
0044 for i = 1:n+1
0045   o = n + 1 - i;
0046   if o == 0
0047     A(:,i) = 1;
0048   elseif o == 1
0049     A(:,i) = x;
0050   else
0051     A(:,i) = x.^o;
0052   end
0053 end
0054 
0055 for i = 1 : length(ls)
0056   A(:,n+1+(i-1)*2+1) = sin( x*(2*pi)/ls(i) );
0057   A(:,n+1+(i-1)*2+2) = cos( x*(2*pi)/ls(i) );
0058 end
0059 
0060 if nargin == 4
0061   b = A \ y;
0062 else
0063   p = lscov( A, y, w );
0064 end
0065 
0066 p = b(1:n+1);
0067 s = b(n+2:end);

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005