Home > FMAToolbox > General > Diff.m

Diff

PURPOSE ^

Diff - Differentiate.

SYNOPSIS ^

function derivative = Diff(samples,varargin)

DESCRIPTION ^

Diff - Differentiate.

  USAGE

    derivative = Diff(samples,<options>)

    samples        <a href="matlab:help samples">samples</a> to differentiate
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'type'        'linear' if samples are linear values (default),
                   'circular' otherwise
     'smooth'      standard deviation for Gaussian kernel (default = 0)
    =========================================================================

  SEE

    See also Interpolate.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function derivative = Diff(samples,varargin)
0002 
0003 %Diff - Differentiate.
0004 %
0005 %  USAGE
0006 %
0007 %    derivative = Diff(samples,<options>)
0008 %
0009 %    samples        <a href="matlab:help samples">samples</a> to differentiate
0010 %    <options>      optional list of property-value pairs (see table below)
0011 %
0012 %    =========================================================================
0013 %     Properties    Values
0014 %    -------------------------------------------------------------------------
0015 %     'type'        'linear' if samples are linear values (default),
0016 %                   'circular' otherwise
0017 %     'smooth'      standard deviation for Gaussian kernel (default = 0)
0018 %    =========================================================================
0019 %
0020 %  SEE
0021 %
0022 %    See also Interpolate.
0023 %
0024 
0025 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0026 %
0027 % This program is free software; you can redistribute it and/or modify
0028 % it under the terms of the GNU General Public License as published by
0029 % the Free Software Foundation; either version 3 of the License, or
0030 % (at your option) any later version.
0031 
0032 % Defaults
0033 type = 'linear';
0034 smooth = 0;
0035 
0036 if nargin < 1,
0037   error('Incorrect number of parameters (type ''help <a href="matlab:help Diff">Diff</a>'' for details).');
0038 end
0039 
0040 if nargin < 1 | mod(length(varargin),2) ~= 0,
0041   error('Incorrect number of parameters (type ''help <a href="matlab:help Diff">Diff</a>'' for details).');
0042 end
0043 
0044 % Parse parameter list
0045 for j = 1:2:length(varargin),
0046     if ~ischar(varargin{j}),
0047         error(['Parameter ' num2str(j+7) ' is not a property (type ''help <a href="matlab:help Diff">Diff</a>'' for details).']);
0048     end
0049     switch(lower(varargin{j})),
0050         case 'type',
0051             type = lower(varargin{j+1});
0052             if ~isastring(type,'linear','circular'),
0053                 error('Incorrect value for ''type'' (type ''help <a href="matlab:help Diff">Diff</a>'' for details).');
0054             end
0055         case 'smooth',
0056             smooth = varargin{j+1};
0057             if ~isdvector(smooth,'>=0') || length(smooth) > 2,
0058                 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help Diff">Diff</a>'' for details).');
0059             end
0060         otherwise,
0061             error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help Diff">Diff</a>'' for details).']);
0062     end
0063 end
0064 
0065 % Circular?
0066 if strcmp(type,'circular'),
0067     samples(:,2:end) = unwrap(samples(:,2:end));
0068 end
0069 
0070 % Smooth
0071 if smooth ~= 0,
0072     if length(samples(1,2:end)) >= 2,
0073         smooth = [smooth 0];
0074     end
0075     samples(:,2:end) = Smooth(samples(:,2:end),smooth);
0076 end
0077 
0078 % Differentiate
0079 derivative = zeros(size(samples));
0080 derivative(:,1) = samples(:,1);
0081 t = samples(:,1);
0082 dt = diff(t);
0083 t_diff = t(1:end-1)+dt/2;
0084 for i = 2:size(samples,2),
0085     d0 = diff(samples(:,i))./dt;
0086     d1 = interp1(t_diff,d0,t(2:end-1,1));
0087     derivative(:,i) = [d0(1);d1;d0(end)];
0088 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005