help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

averaging/smoothing data


From: Przemek Klosowski
Subject: averaging/smoothing data
Date: Wed, 13 Jul 2016 12:58:29 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1

[sorry for repost: maybe my original post gave an impression that I am not asking seriously, but I think it's an important and frequent use case, so here it is again]


Often, I need to smooth/average a data vector. I usually do a running average using filter(), e.g. for 5-value average:

    filter(ones(1:5)/5,eye(5,1),x)

I've been doing this for years, so I got used to it even though I have to think a little every time I need to do again, but maybe there's a better way? What do y'all use?



By the way, sometimes I'd use a Savitzky-Golay peak/moment-preserving filter coefficients instead of a simple rectangular averaging window:


# Copyright (C) 1993 Przemek Klosowski ; licensed under GPL
#
# You should have received a copy of the GNU General Public License
# along with Octave; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

function retval = savgol (m,nl,nr)

# usage: savgol (m,n) or (m,nl,nr) 
# m - order -nl..nr - smoothing window
#
# Return the Savitzky-Golay filter coeffs for 2n window, order m. 
# Ref.: Num.Rec. (2nd edition) p. 650
# Try plot([savgol(2,8);savgol(4,8);savgol(6,8)]'); sometime

# filter coeffs are in first row of (A'*A)^-1 * A'

  if (nargin == 2)
    nr = nl; nl = nl;
  elseif (nargin!=3)
    error ("usage: savgol (n,m)");
  endif

  nmax = length(nl) * length(nr) * length(m);
  if (nmax == 1)
   if (nl<0 || nr<0 )
     error (" n must be positive in  savgol()");
   endif
   ii = -nl:nr;
   jj = 0:m;
   A  = repmat(ii',1,m+1).^repmat(jj,nl+nr+1,1) ;
   A=(A'*A)^-1 * A';
   retval=A(1,:);
  else
    error ("savgol: expecting scalar arguments, found something else");
  endif

endfunction


reply via email to

[Prev in Thread] Current Thread [Next in Thread]