I have a bad understanding why this optimization does not work. I have a raw signal which is the convolution between a signal X (which I would like to find by a fitting) and signal 2 which is known.
I need to optimize a convolution between a three-exponential terms function with signal sig2 (which is a response function that should not change, only the three-exponential fitting that shpould be optimized so that its convolution with sig2 should match the raw signal "sig1").
Data = xlsread('rawdata.xlsx','Sheet1','A2:C119000');
modelFun = @(p,time) - 1.8785e-01*exp(-6E6 *time) - p(2)*exp(-p(3)*time) + (1.8785e-01+p(2))*exp(-p(1)*time);
Now I give very good starting points for the fitting
p0 = [1.4531e+03 3.5868e-01 1.9509e+05];
No I use lsqcurvefit to construct the final target function which should optimize the final fitting so that the convolution of the fitting with sig2 should reproduce well the raw data (sig1).
[params,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(@(p,time) conv(sig2,modelFun(p,time), 'same'),p0,time,sig1);
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
Now if I display the output of this optimization (I display only the three-exponential term function):
tesfunc1 = - 1.8785e-01*exp(-6E6 *time) - params(2)*exp(-params(3)*time) + (1.8785e-01+params(2))*exp(-params(1)*time);
plot(time,sig1,'DisplayName' ,'raw signal' )
plot(time, tesfunc1, 'DisplayName' ,'optimized fitting')
It is defenitely a bad optmization, if I do convolution with sig2, it does not reproduce theraw data at all:
Objective = conv(sig2,tesfunc1);
plot(time,sig1,'DisplayName' ,'raw signal' )
plot(time,Objective(1:length(time))*(max(sig2)/max(Objective)),'DisplayName' ,'objective function' )
However, if I use exactly the initial parameters, it should be a very good fitting (apart fron the convolution changes the overall amplitude that I still do not know how to fix).:
tesfunc2 = - 1.8785e-01*exp(-6E6 *time) - p0(2)*exp(-p0(3)*time) + (1.8785e-01+p0(2))*exp(-p0(1)*time);
Objective = conv(sig2,tesfunc2);
plot(time,sig1,'DisplayName' ,'raw signal' )
plot(time,Objective(1:length(time))*(max(sig2)/max(Objective))*1.1,'DisplayName' ,'objective function' )
Of course for this testfunction I knew the parameters, and in reality I would like to find them. for the lsqcurvefit, even if I spoiled it with very good parameters giving the function above, it still missed the fitting completely!
I am wondering what can make this optimization better. And I have seen some use "fminunc" with a cost function that would be in my case (costFunction = @(params) mean((rawData - conv(signal2, fittingfunction(:, params))).^2);)), but I do not really know how to implement it in my case.
Many thanks in advance.