time | calls | line |
---|
| | 1 | function [p1_a,p1_b,p1_c, p2_a,p2_b,p2_c, p3_a,p3_b,p3_c, skew_factor] = fit_Gaussian_model_disomy_2(workingDir, saveName, data,locations,init_width,func_type)
|
| | 2 | % attempt to fit a 3-gaussian model to data.
|
| | 3 |
|
| 9 | 4 | show = true;
|
| 9 | 5 | p1_a = nan; p1_b = nan; p1_c = nan;
|
| 9 | 6 | p2_a = nan; p2_b = nan; p2_c = nan;
|
| 9 | 7 | p3_a = nan; p3_b = nan; p3_c = nan;
|
| 9 | 8 | skew_factor = 1;
|
| | 9 |
|
| 9 | 10 | if isnan(data)
|
| | 11 | % fitting variables
|
| | 12 | return
|
| | 13 | end
|
| | 14 |
|
| | 15 | % find max height in data.
|
| 9 | 16 | datamax = max(data);
|
| | 17 | %datamax(data ~= max(datamax)) = [];
|
| | 18 |
|
| | 19 | % if maxdata is final bin, then find next highest p
|
| 9 | 20 | if (find(data == datamax) == length(data))
|
| | 21 | data(data == datamax) = 0;
|
| | 22 | datamax = data;
|
| | 23 | datamax(data ~= max(datamax)) = [];
|
| | 24 | end;
|
| | 25 |
|
| | 26 | % a = height; b = location; c = width.
|
| 9 | 27 | p1_ai = datamax; p1_bi = locations(1); p1_ci = init_width;
|
| 9 | 28 | p2_ai = datamax; p2_bi = locations(2); p2_ci = init_width;
|
| 9 | 29 | p3_ai = datamax; p3_bi = locations(3); p3_ci = init_width;
|
| | 30 |
|
| 9 | 31 | initial = [p1_ai,p1_ci,p2_ai,p2_ci,p3_ai,p3_ci, skew_factor,skew_factor,skew_factor];
|
0.03 | 9 | 32 | options = optimset('Display','off','FunValCheck','on','MaxFunEvals',100000);
|
| 9 | 33 | time = 1:length(data);
|
| | 34 |
|
| 9 | 35 | saveFileName = [workingDir saveName '.png']
|
| | 36 |
|
3.00 | 9 | 37 | [Estimates,~,exitflag] = fminsearch(@fiterror, ... % function to be fitted.
|
| | 38 | initial, ... % initial values.
|
| | 39 | options, ... % options for fitting algorithm.
|
| | 40 | time, ... % problem-specific parameter 1.
|
| | 41 | data, ... % problem-specific parameter 2.
|
| | 42 | func_type, ... % problem-specific parameter 3.
|
| | 43 | locations ... % problem-specific parameter 4.
|
| | 44 | );
|
| 9 | 45 | if (exitflag > 0)
|
| | 46 | % > 0 : converged to a solution.
|
| 2 | 47 | else
|
| | 48 | % = 0 : exceeded maximum iterations allowed.
|
| | 49 | % < 0 : did not converge to a solution.
|
| | 50 | % return last best estimate anyhow.
|
| 2 | 51 | end;
|
| 9 | 52 | p1_a = abs(Estimates(1));
|
| 9 | 53 | p1_b = locations(1);
|
| 9 | 54 | p1_c = abs(Estimates(2));
|
| 9 | 55 | p2_a = abs(Estimates(3));
|
| 9 | 56 | p2_b = locations(2);
|
| 9 | 57 | p2_c = abs(Estimates(4));
|
| 9 | 58 | p3_a = abs(Estimates(5));
|
| 9 | 59 | p3_b = locations(3);
|
| 9 | 60 | p3_c = abs(Estimates(6));
|
| 9 | 61 | skew_factor1 = abs(Estimates(7));
|
| 9 | 62 | skew_factor2 = 1;
|
| 9 | 63 | skew_factor3 = abs(Estimates(9));
|
| 9 | 64 | if (skew_factor1 < 0); skew_factor1 = 0; end; if (skew_factor1 > 2); skew_factor1 = 2; end;
|
| 9 | 65 | if (skew_factor2 < 0); skew_factor2 = 0; end; if (skew_factor2 > 2); skew_factor2 = 2; end;
|
| 9 | 66 | if (skew_factor3 < 0); skew_factor3 = 0; end; if (skew_factor3 > 2); skew_factor3 = 2; end;
|
| 9 | 67 | c1_ = p1_c/2 + p1_c*skew_factor1/(100.5-abs(100.5-p1_b))/2;
|
| 9 | 68 | p1_c = p1_c*p1_c/c1_;
|
| 9 | 69 | c3_ = p3_c/2 + p3_c*skew_factor3/(100.5-abs(100.5-p3_b))/2;
|
| 9 | 70 | p3_c = p3_c*p3_c/c3_;
|
| 9 | 71 | end
|
Other subfunctions in this file are not included in this listing.