download smooth.m
Language: Matlab
License: GPL
Copyright: Copyright 1998-2000 Julian Andrew de Marchi, Ph.D. (julian@matlinks.net)
LOC: 38
Project Info
MatLinks/Chorus(matlinks)
Server: SourceForge
Type: cvs
...opyleft\gpl\chorale\signal\
   average.m
   beatfreq.m
   Contents.m
   envelope.m
   findpeak.m
   morlet.m
   phdiff.m
   smooth.m
   zcross.m

% SMOOTH  ( MatLinks) Smooth data using a moving-average window filter.
%
%    SMOOTH(X,FILT,W,...) smooths data by convolving X with a W-length window/
%    filter specified by FILT.  Some filters (marked *** below) accept a second
%    (numerical) parameter, which can be specified following W.  The default for
%    W=1.
%
%    The possible filters are:
%      'bartlett'   - Bartlett window.
%      'blackman'   - Blackman window.
%      'boxcar'     - Rectangular window.
%      'chebwin'    - Chebyshev window.      ***
%      'hamming'    - Hamming window.
%      'hanning'    - Hanning window.
%      'kaiser'     - Kaiser window.         ***
%      'spline'     - Spline smoothing.
%      'triang'     - Triangular window.
%
%    You can also specify a custom filter by name.
%
%    SMOOTH(...) by itself plots X along with the smoothed version of X.
%
%    Y=SMOOTH(...) returns smoothed data in Y, which will be LENGTH(X).
%
%    Type HELP MATLINKS for a full listing of all MatLinks ToolChest functions.
%
function [data] = smooth(x, filt, w, parm)
%===============================================================================
%  Copyright  1998-2000 Julian Andrew de Marchi, Ph.D. (julian@matlinks.net)
%  Use & distribution covered by GNU General Public License (www.gnu.org)
%===============================================================================

%----------------------------------
% check syntax and supply defaults
%----------------------------------
if (nargin==0), error('No data vector X supplied.');
elseif (nargin==1), error('No filter FILT specified.');
else
  filt=num2str(filt);
  if (nargin==2),
    if (filt(1)>='0' & filt(1)<='9'), w=str2num(filt); filt=[]; else w=1; end;
  end;
  if (length(filt)),
    for ix=length(filt)+1:8, filt(ix)=' '; end;
    if (nargin==3 & (filt(1:7)=='chebwin' | [filt(1:6)=='kaiser' 1])),
      error(['The " ' filt(1:7) '" filter requires a second parameter.']);
    end;
  end;
end;
if (w~=fix(w) | w<=0),
  error('The window size W must be a positive integer.');
elseif (length(x)<w+1) error('Not enough data in X--peak detection would be senseless.');
end;

%-----------------
% smooth the data
%-----------------
if (filt(1:6)=='spline'),
  data=spline(1:length(x),x,1:w:length(x));
  data=resample(data,length(x),length(data));
else
  if (nargin==4),
    f=eval([filt '(' num2str(w) ',' num2str(parm) ')']);
  else
    f=eval([filt '(' num2str(w) ')']);
  end;
  data=conv(x,f); data=data(round(length(f)/2)+1:length(data)-round(length(f)/2)-1);
end;
data=spline(1:length(data),data,1:length(x))*max(abs(x))/max(abs(data));

%------------------------------------------------------
% plot the smoothed data if there's no output variable
%------------------------------------------------------
if (nargout==0),
  hold off,plot(x),hold on,plot(1:length(data),data,'r:'),
  xlabel('i'),ylabel('x(i)');
  for ix=length(filt):-1:1, if (filt(ix)==' '), filt=filt(1:ix-1); end; end;
  if (length(filt)) title([filt '-smoothed Data (w=' num2str(w) ')']);
  else title('Data Smoothing'); end;
end;


%===============================================================================
% End-of-File
%===============================================================================

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us