function [begs,ends,scores] = max_finder(x,len) % function [begs,ends,scores] = max_finder(x,len) % % finds maxima in a likelihood vector. Assumes the word has length len % if there's a conflict btw 2 overlapping words the one with better max % wins. N = length(x); % first find all local maxima delayed = [0 x(1:N-1)]; advanced = [x(2:N) 0]; ilm = find (x > delayed & x > advanced); lm = x(ilm); % no local maximum must lie between 1 and len, (otherwise we % would get beg < 0) iaux = find (ilm > len); ilm = ilm(iaux); lm = lm(iaux); Nlm = length(lm); % number of local maxima % allocate big arrays, then we'll shorten begs = zeros (1,Nlm); ends = zeros (1,Nlm); scores = zeros (1,Nlm); % ok, first local max will be a candidate. xend = ilm(1); xbeg = xend - len; xscore = lm(1); cnt = 1; % now run the cycle for ii=2:Nlm, yend = ilm(ii); ybeg = yend - len; yscore = lm(ii); % if x too far, write it and make x from y; if (xend < ybeg) begs(cnt) = xbeg; ends(cnt) = xend; scores(cnt) = xscore; cnt = cnt+1; xbeg = ybeg; xend = yend; xscore = yscore; continue; end % ok, so the 2 guys overlap, or at least touch. Keep the stronger. % e.g. if xscore is better, let x untouched, if not, overwrite % with y. if (yscore > xscore) xbeg = ybeg; xend = yend; xscore = yscore; end end % should write the last one ... begs(cnt) = xbeg; ends(cnt) = xend; scores(cnt) = xscore; % and shorten the output matrices not to output shit. begs = begs (1:cnt); ends = ends(1:cnt); scores = scores (1:cnt);