-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsectChangepoint.m
166 lines (145 loc) · 5.07 KB
/
insectChangepoint.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
% insectChangepoint InsectAlgorithm in script format instead of function
%
% k is the image number. You want to manually load in the data before
% running the script
%
% The commented out for-loop is if you want to iterate through an entire
% mat file (all 100 images or something). Make sure to uncomment the end at
% the bottom of the script as well!
%
% The notes at the bottom were to keep track of changes I made
% for k = 1:length(adjusted_data_decembercal)
k = 47;
data = adjusted_data_decembercal(k);
img = data.normalized_data;
imgRow = 1:178;
insectPos = [];
imgAvg = mean(mean(img));
%% Remove empty rows
[img,imgRow] = removeEmptyRows(img,imgRow);
%% Remove hard point rows
[img,imgRow] = removeHardTarget(img,imgRow,imgAvg);
if ~isempty(img) % if statement #1 start
%% Look at change in each remaining row
[img,imgRow] = removeNoChange(img,imgRow);
if ~isempty(img) % if statement #2 start
%% Wavelet Transform to get rid of bad rows
[img,imgRow] = removeWaveletRows(img,imgRow,data);
if ~isempty(img) % if statement #3 start
%% Wavelet Transform on original image / changepoint detection
fs = 1/mean(diff(data.time));
% for row = 24:24 % Debugging purposes
for row = 1:size(img,1)
tempWavelet = cwt(img(row,:),fs);
tempWavelet = abs(tempWavelet);
tempMax = max(max(tempWavelet));
lesserValue = 0.225 * tempMax;
tempWavelet(tempWavelet < lesserValue) = 0;
tempWavelet = tempWavelet(1:40,:);
tempWavelet = normalize(tempWavelet,'range');
tempSignal = [];
for col = 1:size(tempWavelet,2)
colAvg = sum(tempWavelet(:,col)) / 40;
tempSignal = [tempSignal colAvg];
end
tempSignal = smoothdata(tempSignal,'sgolay',40);
tempSignal = normalize(tempSignal,'range');
ipt = findchangepts(tempSignal,'Statistic','mean','MinThreshold',3.9);
if ~isempty(ipt)
% Additional check by plotting original signal and seeing if there is still
% a changepoint at around the same spot
ipt2 = findchangepts(normalize(smoothdata(img(row,:),'sgolay',20),'range'),'Statistic','mean','MinThreshold',3);
if isempty(ipt2)
% Do nothing
else
% Check if time domain changepoints line up with wavelet changepoints
newIpt = [];
for i = 1:length(ipt2)
iptDiff = abs(ipt - ipt2(i));
[minDiff,idx] = min(iptDiff);
if minDiff < 21 % < 21 means < 2% difference from wavelet changepoint, might need to change this tho
newIpt = [newIpt ipt(idx)];
end
end
if ~isempty(newIpt)
ipt = sort(newIpt);
% Implement iptFilter to verify changepoint and add/not add to insectPos
insectPos = iptFilter(imgRow(row),tempSignal,ipt,insectPos);
end
end
end
end
% Conditional to filter out fake changepoints
if ~isempty(insectPos)
rowDiff = diff(insectPos(:,1));
multiRows = unique(insectPos(rowDiff == 0,1));
posDiff = diff(insectPos');
tooBig = posDiff(2,:) > 605;
tooSmall = posDiff(2,:) < 15;
too = tooBig | tooSmall;
tooRows = insectPos(too,1);
insectPos(too,:) = [];
for i = 1:length(multiRows)
rows = insectPos(:,1);
if ismember(multiRows(i),tooRows)
insectPos(rows == multiRows(i),:) = [];
tooRows(tooRows == multiRows(i)) = [];
end
end
end
end % if statement #3 end
end % if statement #2 end
end % if statement #1 end
% disp(k)
% disp(insectPos)
% end
%% 07/06/2022
%{
Put the conditionals into a function called iptFilter
Deleted the unused resize, fourier transform, second wavelet transform.
Also got rid of unused if-statement (3 left)
Want to add something that plots original signal and verifies that there is
a spike/change where the detected changepoint is
%}
%% 07/05/2022
%{
Put all of the cropping into functions
%}
%% 07/01/2022
%{
Added if statements 1-4 to hopefully decrease performance time and stuffs
and also ensure that it always gives an output when put into a function
Normalized tempSignal to increase contrast and adjusted changepoint
threshold accordingly
%}
%% 06/30/2022
%{
Commented out everything related to Fourier transform, I don't think it's
needed
%}
%% 06/29/2022
%{
If looking at difference between wavelet transforms of img and fourierImg,
take the absolute value of the difference matrix, and then threshold it to
60-70% of the max value to try to isolate the insect signal a bit more
Stuff above the threshold right before insect signal???
^^NOT TRUE
%}
%% 06/28/2022
%{
Added Wavelet transform section
Added look at change in remaining rows
Added variable imgRow that keeps track of which insect rows are kept from
original image
%}
%% 06/24/2022
%{
Re-organized order to: use resize, remove empty rows, remove hardpoints,
Fourier transform
Added findchangepts
%}
%% 06/22/2022
%{
Created file
Wrote remove empty rows, Fourier transform, remove hardpoints
%}