-
Notifications
You must be signed in to change notification settings - Fork 2
/
UNM_despike.m
120 lines (96 loc) · 3.54 KB
/
UNM_despike.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
114
115
116
117
118
119
120
function [FLAG,removed]=UNM_despike(x,nstds,xmin,xmax,signal,var)
%to run as test from despiketest.m, substitute delta_x for removed
FLAG = ones(size(x));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Flag values that are already NaN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idum = find(isnan(x));
FLAG(idum) = 0; %sets record value to 0 if it is NaN and retains 1's for good values
if length(find(FLAG)) > 0
nans = length(idum);
if nargin > 4
%fprintf('%-5s%2.0f',['[' signal ']: # NaNs ='],nans);
else
%fprintf('%-20s%6.0f','# OF SPIKES FOUND:',nans);
end
else
nans = 0;
%fprintf('\n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Locate and flag greater than max values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idum = find(x > xmax);
maxs = length(idum);
FLAG(idum) = 0; %sets flag value to 0 if x is greater than max allowed
%fprintf('%-5s%2.0f',' ... # >max =',maxs);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Locate and flag less than min values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idum = find(x < xmin);
mins = length(idum);
FLAG(idum) = 0; %sets flag value to 0 if x is less than min allowed
%fprintf('%-5s%2.0f',' ... # <min =',mins);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Locate and flag spikes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta_x = zeros(1,length(x)); %open an empty vector to fill with delta x's
for i = 2:length(x) % open a loop for that half hour
delta_x(i-1) = x(i) - x(i-1); %calculate the change in x for successive time periods
end
idum = find(abs(delta_x) > nstds * std(delta_x));
test = find(abs(delta_x(idum + 1)) > nstds * std(delta_x));
uptonow = length(FLAG(find(FLAG ~= 1)));
FLAG(idum(test)+1) = 0;
std_delta_x = std(delta_x);
if std(delta_x(find(FLAG == 1))) < 0.8 * std_delta_x
while std(delta_x(find(FLAG == 1))) < std_delta_x
std_delta_x = std(delta_x(find(FLAG == 1)));
idum = find(abs(delta_x(find(FLAG == 1))) > nstds * std(delta_x(find(FLAG == 1))));
test = find(abs(delta_x(idum + 1)) > nstds * std(delta_x));
FLAG(idum(test)+1) = 0;
sum(FLAG);
end
else
end
spikes = length(FLAG(find(FLAG ~= 1))) - uptonow;
%fprintf('%-5s%2.0f',' ... # spikes = ',spikes);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Locate windows with unacceptable variance
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if var == 1 || var == 2
stdbin = [];
for i = 1:floor(length(x)/500)
if i == 1
startbin = 1;
elseif i >= 2
startbin = (i - 1)*500;
end
endbin = 500 + startbin;
stdbin(i) = std(delta_x(startbin:endbin));
end
idum = find(stdbin > 6 * mean(stdbin));
if length(idum) == 1
FLAG(1:500) = 0;
elseif length(idum) > 1
if idum(1) == 1
FLAG(1:500) = 0;
for i = 2:length(idum)
FLAG(((idum(i) - 1)*500):(500 + (idum(i) - 1)*500)) = 0;
end
elseif idum(1) ~= 1
for i = 1:length(idum)
FLAG(((idum(i) - 1)*500):(500 + (idum(i) - 1)*500)) = 0;
end
end
end
badvariance = 500*length(idum);
%fprintf('%-5s%2.0f',' ... # w/ bad variance = ',badvariance);
else
end
if var == 1
removed = [nans, maxs, mins, spikes, badvariance];
else
removed = [nans, maxs, mins, spikes];
end
%fprintf('%-5s%2.0f',' ... Total removed =',length(find(~FLAG))); fprintf('\n');