-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsweepFilterChooser.m
114 lines (86 loc) · 2.68 KB
/
sweepFilterChooser.m
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%function I_filtered = sweepFilterChooser(I,dv)
%
%Description: Handles the logic for choosing a good filter to use for LAP
%IV sweeps. Also caches the results so in a persistent function memory so
%that the CPU heavy filter function gets called fewer times.
%
%since an_LP_Sweep and an_LP_Sweep_with_assmpt are called after eachother,
%I_cache == I 50% of the calls
%
%
% % we have three or four cases for filtering:
% dv = 0.25 --> e.g. 604, 807 (probably burst mode)
% dv = 0.5 --> e.g. 506
% dv = 0.75 --> e.g. 505
% dv = 1 --> e.g 212 (rare)
% dv << 0.25 --> fine sweeps, (not implemented yet)
%
function I_filtered = sweepFilterChooser(I,dv)
persistent I_cache
persistent I_filtered_cache
persistent i
% persistent i2
% if isempty(i)
% i = 0;
% i2 = 0;
%
% end
% if ~isempty(I_cache)
% figure(7)
% plot(1:length(I),I,'bo',1:length(I_cache),I_filtered_cache,'r',1:length(I_cache),I_cache,'b+');
% title(sprintf('%i calls %i duplicates)',i,i2));
% end
% i = i +1;
%checks if the vectors are equal (I_cache is initialised as empty matrix by
%'persistent' variable declaration)
if isequalwithequalnans(I_cache,I)
I_filtered = I_filtered_cache;
% i2 = i2 + 1;
return
end
% tic
% ~isempty(I_cache) && floor(mean(I_cache == I)+0.5)
% toc
% tic
% isequaln(I_cache,I)
% toc
% tic
% isequalwithequalnans(I_cache,I) <--- winner. even though matlab
% complains
% toc
if dv < 0.27 %i.e. if dv ~ 0.25
sSpan = ceil(0.1*length(I)); % loose rloess filter
sSpan = max(sSpan,6); %HORRIBLE BUG IF SPAN == 5!!! (or 4)
% sSpan = 0.1001;
sMethod = 'rloess'; % loose rloess filter
elseif dv > 0.72
sSpan = ceil(0.2*length(I));
%sSpan = 0.2; %pretty heavy sgolay filter.
sMethod = 'sgolay';
else %i.e. if dv ~ 0.5
sSpan = ceil(0.1*length(I)); % loose rloess filter
sSpan =max(sSpan,6); %HORRIBLE BUG IF SPAN == 5!!! (or 4)
% sSpan = 0.1001; % loose loess filter
sMethod = 'loess';
end
I_filtered = smooth(I,sSpan,sMethod,1).'; %filter sweep NB transpose
Rsq = 1 - nansum(((I-I_filtered).^2))/nansum(((I-nanmean(I)).^2));
% Rsq
if Rsq < 0.92 % inexplicable horrible performance for certain super smooth sweeps.
if isempty(i)
i = 0;
end
i=i+1;
if mod(i,100) == 1
fprintf(1,'%i bad smoothening performance\n',i);
end
I_filtered = smooth(I,'rloess',1).';
Rsq = 1 - nansum(((I-I_filtered).^2))/nansum(((I-nanmean(I)).^2));
if Rsq < 0.92
i = i+10000;
I_filtered = I;
end
end
I_filtered_cache = I_filtered;
I_cache = I;
end