# 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
    0014 y=y(:);
    0015 nt=length(y);
    0016 y_line=zeros(nt,1);
    0017 norm=y_line;
    0018 nwin=ceil((nt-n)/dn);
    0019 yfit=zeros(nwin,n);
    0020 xwt=((1:n)-n/2)/(n/2);
    0021 wt=(1-abs(xwt).^3).^3;
    0022 for j=1:nwin,
    0023     tseg=y(dn*(j-1)+1:dn*(j-1)+n);
    0024     y1=mean(tseg);
    0025     y2=mean((1:n)'.*tseg)*2/(n+1);
    0026     a=(y2-y1)*6/(n-1); b=y1-a*(n+1)/2;
    0027     yfit(j,:)=(1:n)*a+b;
    0028     y_line((j-1)*dn+(1:n))=y_line((j-1)*dn+(1:n))+(yfit(j,:).*wt)';
    0029     norm((j-1)*dn+(1:n))=norm((j-1)*dn+(1:n))+wt';
    0030 end
    0031 mask=find(norm>0); y_line(mask)=y_line(mask)./norm(mask);
    0032 indx=(nwin-1)*dn+n-1;
    0033 npts=length(y)-indx+1;
    0034 y_line(indx:end)=(n+1:n+npts)'*a+b;