View Raw SPL
/*****************************************************************************
*                                                                            *
*   LFIT.SPL     Copyright (C) 1999-2011 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Fits a line to a series using end points                    *
*                                                                            *
*   Revisions:   14 May 1999  RRR  Creation                                  *
*                 9 Jun 2011  RRR  interval series length fix                *
*                                                                            *
*****************************************************************************/

#include 

#if @HELP_LFIT

    LFIT

    Purpose: Fits a line to a series using the end points

    Syntax:  LFIT(s)

              s - a series, the input data

    Returns: A series

    Example:
             W1: Integ(gnorm(1000,1))
             W2: Lfit(w1);overp(w1, lred)

             W2 contains a linear fit of the data that exactly
             passes through the first and last point of W1.
             The resulting line is plotted with the original data.

             W3: Xy(w1, deriv(w1))
             W4: Lfit(w3);overp(w3, lred)

             W4 contains the end point linear fit to the XY data in W3.

    Remarks:

             The first and last points of the resulting line will always
             match the first and last points of the input series.


    See Also:
             Polyfit
             Trend
#endif


/* fit a line using just the endpoints */
ITERATE lfit(s)
{
        local b, m, out;

        if (argc < 1)
        {
                error("lfit - input series required");
        }

        /* line equation y = mx + b */

        if (isxy(s))
        {
                /* deal with explicit xvals */
                x = xvals(s);

                /* slope */
                m = (s[length(s)] - s[1]) / (x[length(s)] - x[1]);

                /* intercept */
                b = s[1] - m * x[1];

                /* yvals */
                out = m * x + b;

                /* convert to XY */
                out = xy(x, out);
        }
        else
        {
                /*
                 *  interval series - implicit xvals
                 */

                /* slope - dy/dx */
                m = (s[length(s)] - s[1]) / ((length(s) - 1) * deltax(s));

                /* offset */
                b = s[1];

                /* create line of same length as input */
                out = gline(length(s), deltax(s), m, b);

                /* set xvalue dx */
                setxoffset(out, xoffset(s));
        }

        return(out);
}