-
Notifications
You must be signed in to change notification settings - Fork 3
/
assignDataToTemplates.m
186 lines (137 loc) · 6.03 KB
/
assignDataToTemplates.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function [groupings,peakIdxGroup,likes,allPeakIdx,allNormalizedPeaks,noiseThreshold,freqIdxGroup,peakAmplitudes] = ...
assignDataToTemplates(data,outputData,options)
%Inputs:
%data -> 1-D array of data points
%outputData -> structure containing information about templates
%options -> self-explanatory
%Outputs:
%groupings -> L x 1 cell array, each cell containing an N_i x d array
% of grouped peaks
%peakIdxGroup -> L x 1 cell array of index values for each
% corresponding group
%likes -> N x L array of the log-likelihood values that each peak
% belongs to the corresponding model
%allPeakIdx -> all found peak locations
%allNormalizedPeaks -> N x d array containing all found peaks
% corresponding to the locations in allPeakIdx
%coeffs -> L x 1 cell array of model PCA bases
%projStds -> L x 1 cell array of model projection standard deviations
%freqIdxGroup -> L x 1 cell array of carrier frequencies for each
% template group
%peakAmplitudes -> N x 1 array of peak amplitudes
addpath(genpath('./utilities/'));
addpath(genpath('./subroutines/'));
templates = outputData.templates;
projStds = outputData.projStds;
coeffs = outputData.coeffs;
means = outputData.means;
L = length(templates);
d = length(templates{1}(1,:));
if nargin < 3 || isempty(options)
options.setAll = true;
else
options.setAll = false;
end
options = makeParameterStructure(options);
Fs = options.fs;
dt = 1/Fs;
%find all potential peaks in the new data set
fprintf(1,' Finding Peak Locations\n');
diffThreshold = d;
maxNumGaussians_noise = options.maxNumGaussians_noise;
maxNumPeaks_GMM = options.maxNumPeaks;
replicates_GMM = options.replicates_GMM;
smoothingLength_noise = options.smoothingLength_noise * Fs / 1000;
minRegionLength = round(options.minRegionLength * Fs / 1000);
min_noise_threshold = options.min_noise_threshold;
median_filter_length = round(options.median_filter_length * Fs / 1000);
noise_posterior_threshold = options.noise_posterior_threshold;
diff_threshold_multiplier = options.diff_threshold_multiplier;
maxF = options.maxCarrierFrequency;
if min_noise_threshold > 0
min_noise_threshold = log10(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');
data = filter(b,a,data);
end
if median_filter_length > 0
if mod(median_filter_length,2) == 0
median_filter_length = median_filter_length + 1;
end
data = medfilt1(data,median_filter_length);
end
[newData,~,noiseThreshold,peakIdx] = filterDataAmplitudes(data,...
smoothingLength_noise,minRegionLength,maxNumGaussians_noise,...
replicates_GMM,maxNumPeaks_GMM,diffThreshold*diff_threshold_multiplier,[],...
min_noise_threshold,noise_posterior_threshold);
fprintf(1,' Finding Carrier Frequencies\n');
r = (diffThreshold-1)/2;
peakIdx = peakIdx(peakIdx > r & peakIdx < length(newData)-r);
N = length(peakIdx);
normalizedPeaks = zeros(N,diffThreshold);
peakAmplitudes = zeros(N,1);
carrierFrequencies = zeros(N,1);
minF = 5*ceil(1 / (2*r*dt*5));
freqs = minF:5:maxF;
freqs = freqs(freqs < .5/dt)';
medfiltLength = ceil((1/maxF)/dt);
parfor i=1:N
a = newData(peakIdx(i) + (-r:r));
peakAmplitudes(i) = sqrt(mean(a.^2));
normalizedPeaks(i,:) = a./peakAmplitudes(i).*sign(newData(peakIdx(i)));
a = medfilt1(a,medfiltLength);
q = fastWavelet_morlet_convolution_parallel(a,freqs,2*pi,dt);
maxQ = q(:,r+1);
maxIdxs = find(imregionalmax(maxQ));
[~,maxIdx] = max(maxQ(maxIdxs));
maxIdx = maxIdxs(maxIdx);
if isempty(maxIdx) || maxIdx <= 2 || maxIdx >= length(freqs) - 2
[~,maxIdx] = max(maxQ);
carrierFrequencies(i) = freqs(maxIdx);
else
idx = maxIdx + (-2:2);
carrierFrequencies(i) = findExtremumParabola(freqs(idx),maxQ(idx),2);
end
end
allPeakIdx = peakIdx;
allNormalizedPeaks = normalizedPeaks;
%find likelihood values
fprintf(1,' Finding Likelihoods\n');
likes = zeros(N,L);
for i=1:L
fprintf(1,' Template #%2i\n',i);
projections = bsxfun(@minus,normalizedPeaks,means{i})*coeffs{i};
likes(:,i) = findDataSetLikelihoods(projections,zeros(size(means{i})),projStds{i});
end
clear projections
%assign peaks to the template with maximum likelihood
[~,idx] = max(likes,[],2);
groupings = cell(L,1);
peakIdxGroup = cell(L,1);
freqIdxGroup = cell(L,1);
for i=1:L
q = idx == i;
groupings{i} = allNormalizedPeaks(q,:);
peakIdxGroup{i} = allPeakIdx(q);
freqIdxGroup{i} = carrierFrequencies(q);
end
if isfield(outputData,'templateGroupings') && ~isempty(outputData.templateGroupings)
k = unique(outputData.templateGroupings);
M = length(k);
groupings2 = cell(M,1);
peakIdxGroup2 = cell(M,1);
freqIdxGroup2 = cell(M,1);
for i=1:M
groupings2{i} = cell2mat(groupings(outputData.templateGroupings==k(i)));
peakIdxGroup2{i} = cell2mat(peakIdxGroup(outputData.templateGroupings==k(i)));
freqIdxGroup2{i} = cell2mat(freqIdxGroup(outputData.templateGroupings==k(i)));
end
groupings = groupings2;
peakIdxGroup = peakIdxGroup2;
freqIdxGroup = freqIdxGroup2;
end