comparison _misc/general/.svn/text-base/dpfast.m.svn-base @ 8:b5b38998ef3b

added all that other stuff
author matthiasm
date Fri, 11 Apr 2014 15:54:25 +0100
parents
children
comparison
equal deleted inserted replaced
7:12abff5474c8 8:b5b38998ef3b
1 function [p,q,D,sc] = dpfast(M,C,T,G)
2 % [p,q,D,sc] = dpfast(M,C,T,G)
3 % Use dynamic programming to find a min-cost path through matrix M.
4 % Return state sequence in p,q; full min cost matrix as D and
5 % local costs along best path in sc.
6 % This version gives the same results as dp.m, but uses dpcore.mex
7 % to run ~200x faster.
8 % C is a step matrix, with rows (i step, j step, cost factor)
9 % Default is [1 1 1.0;0 1 1.0;1 0 1.0];
10 % Another good one is [1 1 1;1 0 1;0 1 1;1 2 2;2 1 2]
11 % T selects traceback origin: 0 is to any edge; 1 is top right (default);
12 % T > 1 finds path to min of anti-diagonal T points away from top-right.
13 % Optional G defines length of 'gulleys' for T=0 mode; default 0.5
14 % (i.e. accept path to only 50% of edge nearest top-right)
15 % 2003-04-04,2005-04-04 dpwe@ee.columbia.edu $Header: /Users/dpwe/projects/dtw/RCS/dpfast.m,v 1.6 2008/03/14 14:40:50 dpwe Exp $
16
17 % Copyright (c) 2003 Dan Ellis <dpwe@ee.columbia.edu>
18 % released under GPL - see file COPYRIGHT
19
20 if nargin < 2
21 % Default step / cost matrix
22 C = [1 1 1.0;0 1 1.0;1 0 1.0];
23 end
24
25 if nargin < 3
26 % Default: path to top-right
27 T = 1;
28 end
29
30 if nargin < 4
31 % how big are gulleys?
32 G = 0.5; % half the extent
33 end
34
35 if sum(isnan(M(:)))>0
36 error('dpwe:dpfast:NAN','Error: Cost matrix includes NaNs');
37 end
38
39 if min(M(:)) < 0
40 disp('Warning: cost matrix includes negative values; results may not be what you expect');
41 end
42
43 [r,c] = size(M);
44
45 % Core cumulative cost calculation coded as mex
46 [D,phi] = dpcore(M,C);
47
48 p = [];
49 q = [];
50
51 %% Traceback from top left?
52 %i = r;
53 %j = c;
54
55 if T == 0
56 % Traceback from lowest cost "to edge" (gulleys)
57 TE = D(r,:);
58 RE = D(:,c);
59 % eliminate points not in gulleys
60 TE(1:round((1-G)*c)) = max(max(D));
61 RE(1:round((1-G)*r)) = max(max(D));
62 if (min(TE) < min(RE))
63 i = r;
64 j = max(find(TE==min(TE)));
65 else
66 i = max(find(RE==min(RE)));
67 j = c;
68 end
69 else
70 % Traceback from min of antidiagonal
71 %stepback = floor(0.1*c);
72 stepback = T;
73 slice = diag(fliplr(D),-(r-stepback));
74 [mm,ii] = min(slice);
75 i = r - stepback + ii;
76 j = c + 1 - ii;
77 end
78
79 p=i;
80 q=j;
81
82 sc = M(p,q);
83
84 while i > 1 & j > 1
85 % disp(['i=',num2str(i),' j=',num2str(j)]);
86 tb = phi(i,j);
87 i = i - C(tb,1);
88 j = j - C(tb,2);
89 p = [i,p];
90 q = [j,q];
91 sc = [M(i,j),sc];
92 end