0001 function derivative = Diff(samples,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
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
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
0066 if strcmp(type,'circular'),
0067 samples(:,2:end) = unwrap(samples(:,2:end));
0068 end
0069
0070
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
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