-
Notifications
You must be signed in to change notification settings - Fork 0
/
Onset_Det_n.m
223 lines (184 loc) · 9.59 KB
/
Onset_Det_n.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function [ grp_delay , signal] = Onset_Det_n( FOUT,sampling_rate, smoothening_factor, winScaleFactor, thres )
Mlf_file = 'result_1_ss.mlf';
%FOUT=ones(length(FOUT),1).*abs(min(FOUT)) +FOUT +1;%*25;
FOUT=abs(FOUT)+5;
% winScaleFactor = 5:5:45;
% Begin of paramterization
winScaleFactor=10;
for indexB = 1:length(smoothening_factor)
for indexD = 1:length(thres)
%fprintf('Paramterization details : dwnsmpl_rate = %d\n smth_factor = %d, thres = %f\n',smoothening_factor(indexB),thres(indexD));
%======================================================================
% Part1: Using Amplitude Demodulation, and applying Group delay on it
S=FOUT;
grp_delay = ones(length(S),1);
gd_sum = ones(length(S),1);
for wsfIndex = 1:length(winScaleFactor)
tempDir = sprintf('temp_%d',wsfIndex);
warning('off','all')
mkdir(tempDir); cd(tempDir);
energy_file_name = strcat(sprintf('neuron'),'.en');
dlmwrite(energy_file_name,S,'\n');
display(size(S));
spec_file_name = energy_file_name(1:end-2);
spec_file_name =strcat(spec_file_name,'spec');
% Invoking the binary
copyfile('../fe-words.base','fe-words.base');
ctrl_file = 'fe-words.base';
temp_ctrl_file = strcat('temp.base');
% Changing the winscalefactor parameter in config file
a = importdata(ctrl_file);
a = struct2cell(a);
a{1}(3) = winScaleFactor(wsfIndex);
%fprintf('Window scale factor is %d\n',winScaleFactor(wsfIndex));
fid0 = fopen(temp_ctrl_file,'w');
for i = 1:length(a{1})
fprintf(fid0,'%s %s %f\n',char(a{2}(i,1)),char(a{2}(i,2)),a{1}(i));
end
copyfile(temp_ctrl_file,ctrl_file);
%delete(temp_ctrl_file);
fclose(fid0);
%disp('here');
dummy1 = 'b';
dummy2 = 'c';
dummy3 = 'd';
dummy4 = 'e';
dump = 'dump.txt';
system(sprintf('../WordSegmentWithSilenceRemoval %s %s %s %s %s %s %s > %s 2>&1',ctrl_file,energy_file_name,spec_file_name,dummy1,dummy2,dummy3,dummy4,dump));
%disp('here');
delete(energy_file_name);
temp = load(spec_file_name);
% temp
% display(size(grp_delay));
%delete(spec_file_name);
temp = temp(:,5);
temp(length(S)+1:end) = [];
%disp('here');
%figure;
%plot(temp);
grp_delay = grp_delay.*temp;
temp = temp - mean(temp);
gd_sum = gd_sum + cumsum(temp);
%disp('here');
cd ..;
end
grp_delay = diff(gd_sum);
%grp_delay = temp;
%grp_delay = smooth(grp_delay,2*smoothening_factor(indexB),'moving'); % A moving average with 1 ms interval
grp_delay = grp_delay/max(grp_delay);
grp_delay=[grp_delay(1:end-20);grp_delay(end-20).*ones(20,1)];
grp_delay=smooth(grp_delay,5);
%grp_delay = (grp_delay-mean(grp_delay))./std(grp_delay);
assignin('base','grp_delay',grp_delay);
%figure;
%======================================================================
%======================================================================
% Part2: Reading the contents of group delay file, and getting the
% onsets
threshold = thres(indexD);
stroke_loc = zeros(1,length(grp_delay));
% Go to each minima, and calculate height till next maxima. Keep a
% threshold on this to decide if stro
% display('GD dimension before stroke_loc');
%grp_delay;
t = 1:length(grp_delay);
[ymax,imax,ymin,imin] = extrema(grp_delay);
% sort the minimas and maximas;
% imin
% ymin
temp_min = sortrows([imin ymin]);
imin = temp_min(:,1)';
ymin = temp_min(:,2)';
clear temp_min;
temp_max = sortrows([imax ymax]);
imax = temp_max(:,1)';
ymax = temp_max(:,2)';
clear temp_max;
if (imin(1) < imax(1) ) % fine, just truncate the maximum
imin(1) = []; ymin(1) = [];
if (length(imin) > length(imax) )
imin(length(imax)+1:end) = [];
ymin(length(imax)+1:end) = [];
elseif (length(imin) < length(imax) )
imax(length(imin)+1:end) = [];
ymax(length(imin)+1:end) = [];
end
else
if (length(imin) > length(imax) )
disp('this shouldnt have come');
imin(length(imax)+1:end) = [];
ymin(length(imax)+1:end) = [];
elseif (length(imin) < length(imax) )
imax(length(imin)+1:end) = [];
ymax(length(imin)+1:end) = [];
end
end
assignin('base','ymax',ymax);
assignin('base','imax',imax);
assignin('base','ymin',ymin);
assignin('base','imin',imin);
assignin('base','grp_delay',grp_delay);
%==================================================================
% Algorithm1 for stroke location
index_stroke = 1;
peak_valley_heights = ymax - ymin;
peak_valley_heights = peak_valley_heights(1:length(peak_valley_heights));
for index = 1:1:length(peak_valley_heights)
if (peak_valley_heights(index) > threshold)
%stroke_loc(index_stroke) = ceil((imin(index) + imax(index))/2);
stroke_loc(index_stroke) = imin(index) ;
index_stroke = index_stroke + 2;
end
end
%==================================================================
stroke_loc(stroke_loc==0) = [];
assignin('base','stroke_loc',stroke_loc);
assignin('base','peaks',peaks);
%======================================================================
%======================================================================
% Printing in standard MLF format
dangerflag = 0;
X=S;
Fs=sampling_rate;
fid3 = fopen(Mlf_file,'a');
length_wav_file = length(X)*1/Fs;
stroke_loc = stroke_loc*sampling_rate/Fs;
time=length(S)/Fs;% Converting into seconds
gdans=zeros(length(S),1);
gdans(stroke_loc)=1;
isort = sort([imax imin]);
exts=0;
exte=0;
iexts=1;
iexte=1;
ind=1;
for ind= 1:length(isort)-1
iexts=isort(ind);
iexte=isort(ind+1);
exts = grp_delay(iexts);
exte = grp_delay(iexte);
valley_lenght=abs(exts-exte);
signal(iexts)=0;
signal(iexte)=0;
intermediate_samples=iexte-iexts-2;
increment_value=valley_lenght/(floor((intermediate_samples+1)/2));
sum=0;
if valley_lenght>0.1*max(grp_delay)
for j=iexts+1:iexts+floor((intermediate_samples+1)/2)
sum=sum+increment_value;
signal(j)=sum;
end
sum=0;
for j=iexte-1:-1:iexts+floor((intermediate_samples+1)/2)+1
sum=sum+increment_value;
signal(j)=sum;
end
else
signal(iexts:iexte)=0;
end
end
signal(isort(length(isort)):time*Fs)=0;
% display(size(signal));
end
end
end