-
Notifications
You must be signed in to change notification settings - Fork 3
/
createTrainingSet.m
152 lines (116 loc) · 4.56 KB
/
createTrainingSet.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
function [trainingSet,trainingSet_origins,thresholds] = ...
createTrainingSet(data,trainingSetLength,minFromEachDataSet,options)
%Creates a training set from the cell array 'data'
%
%Inputs:
% data -> N x 1 cell array of data or strings pointing to .wav files (can be a mixture)
% trainingSetLength -> minimum length of returned training set
% minFromEachDataSet -> minumum number of data points from each element in 'data'
% options -> options structure (see makeParameterStructure.m for details)
%
%Outputs:
% trainingSet -> training set data
% trainingSet_origins -> L x 3 array.
% First column -> beginning of region
% Second column -> end of region
% Third column -> data set # of region
% thresholds -> thresholds for determining signal from noise for each data set
addpath(genpath('./utilities/'));
addpath(genpath('./subroutines/'));
if ~iscell(data)
data = {data};
end
N = length(data);
masks = cell(N,1);
thresholds = zeros(N,1);
numSegments = zeros(N,1);
if nargin < 3 || isempty(minFromEachDataSet)
minFromEachDataSet = trainingSetLength / (N*10);
end
if nargin < 4 || isempty(options)
options.setAll = true;
else
options.setAll = false;
end
options = makeParameterStructure(options);
Fs = options.fs;
sigma = 10*options.smoothingLength_noise * Fs / 1000;
maxNumGaussians = options.maxNumGaussians_noise;
replicates = options.replicates_GMM;
maxNumPoints = options.maxNumPeaks;
noise_posterior_threshold = options.noise_posterior_threshold;
if options.min_noise_threshold > 0
min_noise_threshold = log10(options.min_noise_threshold.^2);
else
min_noise_threshold = [];
end
high_pass_filter_cutoff = options.high_pass_filter_cutoff / (Fs/2);
butterworth_order = options.butterworth_order;
if high_pass_filter_cutoff > 0
[b,a] = butter(butterworth_order,high_pass_filter_cutoff,'high');
end
CCs = cell(N,1);
for i=1:N
if ischar(data{i})
[data{i},~] = audioread(data{i});
end
if high_pass_filter_cutoff > 0
data{i} = filter(b,a,data{i});
end
y = log10(gaussianfilterdata(data{i}.^2,sigma));
obj = findBestGMM_AIC(y,maxNumGaussians,replicates,maxNumPoints);
idx = argmax(obj.mu);
minIdx = argmin(obj.mu);
posts = posterior(obj,y);
posts = posts(:,idx);
masks{i} = (posts >= noise_posterior_threshold | y > obj.mu(idx)) & y > obj.mu(minIdx);
thresholds(i) = min(y(masks{i}));
if thresholds(i) < min_noise_threshold
thresholds(i) = min_noise_threshold;
masks{i} = y > min_noise_threshold;
end
CCs{i} = bwconncomp(masks{i});
numSegments(i) = CCs{i}.NumObjects;
end
totalNumSegments = sum(numSegments);
d = zeros(totalNumSegments,3);
count = 0;
for i=1:N
d((1:numSegments(i)) + count,1) = i;
d((1:numSegments(i)) + count,2) = 1:numSegments(i);
d((1:numSegments(i)) + count,3) = returnCellLengths(CCs{i}.PixelIdxList);
count = count + numSegments(i);
end
dataSetMins = zeros(N,1);
idx = zeros(totalNumSegments,1);
count = 0;
for i=1:N
dataSetMins(i) = min([sum(d(d(:,1)==i,3)),minFromEachDataSet]);
if dataSetMins(i) > 0
idx2 = find(d(:,1) == i);
q = randperm(length(idx2));
lengths = d(idx2(q),3);
cSum = cumsum(lengths);
idx3 = find(cSum >= dataSetMins(i),1,'first');
idx(count + (1:idx3)) = idx2(q(1:idx3));
count = count + idx3;
end
end
remainingIdx = setdiff(1:totalNumSegments,idx(1:count))';
idx = [idx(1:count);remainingIdx(randperm(length(remainingIdx)))];
d = d(idx,:);
cumSums = cumsum(d(:,3));
idx = find(cumSums >= trainingSetLength,1,'first');
if isempty(idx)
idx = totalNumSegments;
end
d = d(1:idx,:);
d = d(randperm(length(d(:,1))),:);
trainingSet = zeros(sum(d(1:idx,3)),1);
trainingSet_origins = zeros(idx,3);
count = 0;
for i=1:idx
trainingSet((1:d(i,3)) + count) = data{d(i,1)}(CCs{d(i,1)}.PixelIdxList{d(i,2)});
trainingSet_origins(i,:) = [count+1,count+d(i,3),d(i,1)];
count = count + d(i,3);
end