# runline

## PURPOSE

Running line fit (local linear regression)

## SYNOPSIS

function y_line=runline(y,n,dn)

## DESCRIPTION

     Running line fit (local linear regression)

     Usage: y_line=runline(y,n,dn);

     Inputs:
     y: input 1-d time series (real)
     n: length of running window in samples
     dn: stepsize of window in samples

     Outputs:
     y_line: local line fit to data

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- FAnalyze FANALYZE
- locdetrend Remove running line fit (using local linear regression)-continuous
- locsmooth Running line fit (using local linear regression) - 1d only, continuous

## SOURCE CODE

    0001 function y_line=runline(y,n,dn)
    0002 % Running line fit (local linear regression)
    0003 %
    0004 % Usage: y_line=runline(y,n,dn);
    0005 %
    0006 % Inputs:
    0007 % y: input 1-d time series (real)
    0008 % n: length of running window in samples
    0009 % dn: stepsize of window in samples
    0010 %
    0011 % Outputs:
    0012 % y_line: local line fit to data
    0013 y=y(:);
    0014 nt=length(y);
    0015 y_line=zeros(nt,1);
    0016 norm=y_line;
    0017 nwin=ceil((nt-n)/dn);
    0018 yfit=zeros(nwin,n);
    0019 xwt=((1:n)-n/2)/(n/2);
    0020 wt=(1-abs(xwt).^3).^3;
    0021 for j=1:nwin,
    0022     tseg=y(dn*(j-1)+1:dn*(j-1)+n);
    0023     y1=mean(tseg);
    0024     y2=mean((1:n)'.*tseg)*2/(n+1);
    0025     a=(y2-y1)*6/(n-1); b=y1-a*(n+1)/2;
    0026     yfit(j,:)=(1:n)*a+b;
    0027     y_line((j-1)*dn+(1:n))=y_line((j-1)*dn+(1:n))+(yfit(j,:).*wt)';
    0028     norm((j-1)*dn+(1:n))=norm((j-1)*dn+(1:n))+wt';
    0029 end
    0030 mask=find(norm>0); y_line(mask)=y_line(mask)./norm(mask);
    0031 indx=(nwin-1)*dn+n-1;
    0032 npts=length(y)-indx+1;
    0033 y_line(indx:end)=(n+1:n+npts)'*a+b;