0001 function [dt1,dt2,travel,index] = FitCCG(t,ccg,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
0034 show = 'off';
0035
0036
0037 if nargin < 2,
0038 error('Incorrect number of parameters (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).');
0039 end
0040
0041 if ~isdvector(t) || ~isdvector(ccg),
0042 error('Variables must be vectors (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).');
0043 end
0044 if length(t) ~= length(ccg),
0045 error('Variables are of different lengths (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).');
0046 end
0047
0048
0049 for i = 1:2:length(varargin),
0050 if ~ischar(varargin{i}),
0051 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).']);
0052 end
0053 switch(lower(varargin{i})),
0054 case 'show',
0055 show = varargin{i+1};
0056 if ~isastring(show,'off','on'),
0057 error('Incorrect value for property ''show'' (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).');
0058 end
0059 otherwise,
0060 error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help FitCCG">FitCCG</a>'' for details).']);
0061 end
0062 end
0063
0064 global FitCCG_t FitCCG_ccg;
0065 FitCCG_t = t;
0066 FitCCG_ccg = ccg;
0067
0068 ccg = ccg(:);
0069
0070
0071 smoothed = Smooth(ccg,3);
0072 maxima = IsExtremum([t smoothed]);
0073 peaksPos = t(t>0&maxima);
0074 peaksNeg = t(t<0&maxima);
0075
0076 if isempty(peaksPos),
0077 if isempty(peaksNeg),
0078 dt1 = NaN;
0079 else
0080 dt1 = peaksNeg(size(peaksNeg,1));
0081 end
0082 else
0083 if isempty(peaksNeg) || peaksPos(1) < abs(peaksNeg(size(peaksNeg,1))),
0084 dt1 = peaksPos(1);
0085 else
0086 dt1 = peaksNeg(size(peaksNeg,1));
0087 end
0088 end
0089
0090
0091
0092
0093
0094
0095
0096 tau = 0.5;
0097 mu = 0;
0098 omega = 7;
0099 b = mean(ccg);
0100 phi = 2*pi*omega*dt1;
0101 a = b;
0102
0103 beta = [a tau mu omega phi b];
0104
0105
0106 [beta,~,code] = fminsearch(@Fit,beta);
0107 a = abs(beta(1));
0108 tau = -abs(beta(2));
0109 mu = beta(3);
0110 omega = beta(4);
0111 phi = beta(5);
0112 b = beta(6);
0113
0114
0115 [~,fit] = Fit(beta);
0116
0117
0118
0119 maxima = IsExtremum([t fit]);
0120 peaksPos = t(t>0&maxima);
0121 peaksNeg = t(t<0&maxima);
0122
0123 if isempty(peaksPos),
0124 if isempty(peaksNeg),
0125 dt2 = NaN;
0126 else
0127 dt2 = peaksNeg(size(peaksNeg,1));
0128 end
0129 else
0130 if isempty(peaksNeg) || peaksPos(1) < abs(peaksNeg(size(peaksNeg,1))),
0131 dt2 = peaksPos(1);
0132 else
0133 dt2 = peaksNeg(size(peaksNeg,1));
0134 end
0135 end
0136
0137
0138 index = a/b;
0139
0140
0141 carrier = Smooth(fit,5);
0142 [~,i] = max(carrier);
0143 travel = t(i);
0144
0145
0146 if strcmp(show,'on'),
0147 printf('a = %.3f',a);
0148 printf('tau = %.3f',tau);
0149 printf('mu = %.3f',mu);
0150 printf('omega = %.3f',omega);
0151 printf('phi = %.3f',phi);
0152 printf('b = %.3f',b);
0153
0154 figure;
0155 hold on;
0156 bar(t,ccg);
0157 plot(t,fit,'r','linewidth',5);
0158 end
0159
0160
0161
0162
0163 function [error,model] = Fit(beta)
0164
0165 global FitCCG_t FitCCG_ccg;
0166 t = FitCCG_t;
0167 ccg = FitCCG_ccg;
0168
0169 a = beta(1);
0170 tau = beta(2);
0171 mu = beta(3);
0172 omega = beta(4);
0173 phi = beta(5);
0174 b = beta(6);
0175
0176
0177 model = (abs(a)*(sin(2*pi*omega*t + phi)+1) + b) .* exp(-abs(tau)*abs(t-mu));
0178
0179 error = sum((ccg-model).^2);
0180
0181
0182
0183
0184
0185
0186
0187
0188