Skip to main content

calcDerivative.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function calculates the first derivative of the curve provided using
% piecewise polynomials.
%
% (Array of number, array of number) -> (Array of number)
% Returns first derivative


function [dataDerivative] = calcDerivative(time, data)

fittingType = fittype('smoothingspline');
fittingOptions = fitoptions('method', 'SmoothingSpline');
fittedModel = fit(time, data, fittingType, fittingOptions);
dataCoefficientStructure = fittedModel.p;
% Calculate first derivative of smoothed curve
dataDerivativeCoefficientStructure = dataCoefficientStructure;
dataDerivativeCoefficientStructure.coefs(:,1) = 0;
dataDerivativeCoefficientStructure.coefs(:,2) = ...
3*dataCoefficientStructure.coefs(:,1);
dataDerivativeCoefficientStructure.coefs(:,3) = ...
2*dataCoefficientStructure.coefs(:,2);
dataDerivativeCoefficientStructure.coefs(:,4) = ...
dataCoefficientStructure.coefs(:,3);
dataDerivative = ppval(dataDerivativeCoefficientStructure, time);
end