LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
kpcfit_init.m
1function trace = kpcfit_init(S, varargin)
2% trace = kpcfit_init(S,'option1','val1','option2','val2',...)
3%
4% DESCRIPTION
5% Prepare trace S for KPC fitting
6%
7% OUTPUT
8% trace.S - original trace
9% trace.E - moments of the trace, e.g., E(3) is E[X^3]
10% trace.ACFull - autocorrelation coefficients of the trace. Each coefficient is computed at least from 10 samples.
11% trace.AC - autocorrelation coefficients selected for fitting
12% trace.BC - bicovariance coefficients selected for fitting (BCFull is too expensive to compute)
13% trace.BCGridLags - bicovariance lags (2-dimensional sampling square grid)
14% trace.BCLags - bicovariance lags (1-dimensional side of the sampling square grid)
15%
16% OPTION LIST
17% 'ACLags' - sets lags of of the autocorrelation coefficients
18% 'BCGridLags' - sets lags of of the sampling square grid
19% 'Smooth' - smooths autocorrelation coefficients using the specified window size
20
21%% options
22OptionNames = [
23 'ACLags ';
24 'BCGridLags';
25 'Smooth '
26 ];
27
28OptionTypes = [
29 'numeric';
30 'numeric';
31 'numeric'];
32
33OptionValues = [];
34for i = 1:size(OptionNames,1)
35 options.(deblank(OptionNames(i,:)))=[];
36end
37
38% Default settings
39n = length(S);
40options.MaxMoment = 3; % fit 3 moments (this value is currently unused)
41nMinSupportAC = 10; % mininum number of points needed to estimate an AC coefficient
42options.ACLags = unique(logspacei(1,ceil(n/nMinSupportAC),500)); % AC lags used for fitting
43options.BCGridLags = unique(logspacei(1,max(options.ACLags),5)); % lags used to generate square BC grid for fitting
44options.Smooth = 0;
45options.LogSmooth = 0;
46% Parse optional parameters
47options=ParseOptPara(options,OptionNames,OptionTypes,OptionValues,varargin);
48
49%% trace preprocessing
50fprintf(1,'init: computing moments from the trace\n');
51% moments
52E=zeros(1,options.MaxMoment);
53for j=1:options.MaxMoment
54 E(j)=mean(S.^j);
55end
56
57if options.Smooth > 0
58 fprintf(1,'init: computing smoothed autocorrelations from the trace\n');
59 if exist('smooth')==0
60 warning('MATLAB:kpcfit_init:cftoolbox','curve fitting toolbox not installed, smooth function unavailable\n');
61 fprintf(1,'init: computing autocorrelations from the trace\n');
62 AC=trace_acf(S,options.ACLags);
63 ACFull=trace_acf(S,1:ceil(length(S)/nMinSupportAC));
64 else
65 AC=smooth(trace_acf(S,options.ACLags),options.Smooth);
66 ACFull=smooth(trace_acf(S,1:ceil(length(S)/nMinSupportAC)),options.Smooth);
67 end
68else
69 fprintf(1,'init: computing autocorrelations from the trace\n');
70 AC=trace_acf(S,options.ACLags);
71 ACFull=trace_acf(S,1:ceil(length(S)/nMinSupportAC));
72end
73% autocorrelations
74
75%
76
77% estimate cut point of AC
78posmax = options.ACLags(find(abs(AC) < 10^-6, 1 ));
79%fprintf(1,'init: autocorrelations beyond lag %d ignored (abs. val. <10^-6)\n',posmax);
80if ~isempty(posmax)
81 todel = find(options.ACLags>posmax);
82 options.ACLags(todel)=[]; % lags of AC used for fitting
83 AC(todel)=[];
84 todel = options.BCGridLags>posmax;
85 options.BCGridLags(todel)=[]; % lags of AC used for fitting
86end
87fprintf(1, "Using %d AC Lags\n", length(options.ACLags));
88
89
90fprintf(1,'init: computing bicovariances from the trace\n');
91[BC,BCLags]=trace_bicov(S,options.BCGridLags); % bispectrum coefficients
92
93%% generate trace structure
94trace = struct('S',S(:),'E',E,'AC',AC(:),'ACFull',ACFull(:),'ACLags',options.ACLags,'BC',BC,'BCGridLags',options.BCGridLags,'BCLags',BCLags);
95end