[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Simulating data smoothing on a 2D matrix. New to Octave/Matlab platf
From: |
Przemek Klosowski |
Subject: |
Re: Simulating data smoothing on a 2D matrix. New to Octave/Matlab platforms |
Date: |
Wed, 1 Jul 2009 09:44:51 -0400 (EDT) |
> I was looking for function to smooth noise variations from direct ADC
> readings. I have found savgol filters on the internet so I was wondering
> if octave had some similar filters
Savitzky-Golay filters are good for preserving peak widths and other 'sharp'
features in noisy data (they are designed to preserve second+ moments,
unlike simple filters which smear out the data, making real peaks smaller
and wider). Does your data have peaks that are close enough in frequency
to noise that SG is required?
Here's a function to calculate Savitzky-Golay coefficients:
# 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
for i = 1:(nl+nr+1) # -nl:nr, really
for j = 1:(m+1) # 0:m, really
A(i, j) = (i-nl-1)^(j-1) ;
endfor
endfor
A=(A'*A)^-1 * A';
retval=A(1,:);
else
error ("savgol: expecting scalar arguments, found something else");
endif
endfunction