-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget_optimal_SMAF.m
More file actions
72 lines (63 loc) · 1.92 KB
/
Copy pathget_optimal_SMAF.m
File metadata and controls
72 lines (63 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% get optimal simple moving average filter, based on mean-squared error minimization
function [W,sigma_est,its] = get_optimal_SMAF(x,fx_obs,max_points,init_m_fac,max_filter_fac,expand_fac,maxits,deriv_tol,verbose)
if isempty(max_points)
max_points = 10^5;
end
if isempty(init_m_fac)
init_m_fac = 200;
end
if isempty(max_filter_fac)
max_filter_fac = 8;
end
if isempty(expand_fac)
expand_fac = 2;
end
if isempty(maxits)
maxits = 100;
end
if isempty(deriv_tol)
deriv_tol = 10^-6;
end
if isempty(verbose)
verbose = 0;
end
sigma_est = estimate_sigma(fx_obs);
subsamp = max(floor(length(x)/max_points),1);
fx_subsamp = fx_obs(1:subsamp:end,:);
M = size(fx_subsamp,1);
dx_subsamp = mean(diff(x(1:subsamp:end)));
m = ceil(M/init_m_fac);
max_filter_width=floor(M/max_filter_fac);
its = 1; check=1;
m = min(m,max_filter_width);
while and(check>0,its<maxits)
if verbose
tic
end
[~,A] = build_poly_kernel(2,@(x) x*0+1,min(max(floor(m*expand_fac),3),floor((M-1)/2)),dx_subsamp,0);
if size(fx_subsamp,2)==1
d = 2*mean(abs(conv(fx_subsamp,A(3,:),'valid')));
else
d = 2*mean(reshape(abs(conv2(A(3,:),1,fx_subsamp,'valid')),[],1));
end
C = sigma_est^2/((d+deriv_tol)^2*dx_subsamp^4/144);
mnew = min( floor((fzero(@(n) n.^5-n.^3-C,1)-1)/2), max_filter_width);
check = abs(m-mnew);
m = mnew;
its = its+1;
if verbose
disp([toc m d])
end
end
m = m*subsamp;
W = 1./(2*m+1)*ones(2*m+1,1);
end
function [f,A]=build_poly_kernel(deg,k,n,dx,max_dx)
x = (-n:n)'*dx;
X = x.^(0:deg);
K = k(x/(n*dx));
K = K/norm(K,1);
A = pinv(sqrt(K).*X).*sqrt(K)';
M = [diag(factorial(0:max_dx)) zeros(max_dx+1,deg-max_dx)];
f = M*A;
end