Daniel@0: function draw_hmm(A, varargin) Daniel@0: % DRAW_HMM Make a picture of the HMM using dotty Daniel@0: % function draw_hmm(A, ...) Daniel@0: % Daniel@0: % For details on dotty, see http://www.research.att.com/sw/tools/graphviz Daniel@0: % Daniel@0: % If A(i,j) > thresh, we draw and arc from state i to state j. Daniel@0: % Daniel@0: % Optional arguments (name/value pairs) [default] Daniel@0: % Daniel@0: % thresh - [1e-1] Daniel@0: % obsprob - If B(i,o) > 0, we include "o" in the name of state i. Daniel@0: % e.g., if state 5 emits 1,3,7, its label becomes "5: 1 3 7". Daniel@0: % startprob - ifstartprob(i) > 0, the state name will be prefixed with "+". Daniel@0: % endprob - if endprob(i) > 0, the state name will be appended with "-". Daniel@0: % filename - if [], we write to 'tmp.dot', convert this to 'tmp.ps' Daniel@0: % using 'dot -Tps tmp.dot -o tmp.ps', and then call ghostview to display the result. Daniel@0: % dot and gv must be on your system path. Daniel@0: % If filename ~= [], we just generate the dot file, and do not Daniel@0: % convert it to postscript or call ghostview. Daniel@0: Daniel@0: [thresh, B, startprob, endprob, filename] = ... Daniel@0: process_options(varargin, 'thresh', 1e-1, 'obsprob', [], 'startprob', [], 'endprob', [], ... Daniel@0: 'filename', []); Daniel@0: Daniel@0: Q = length(A); Daniel@0: Daniel@0: arclabel = cell(Q,Q); Daniel@0: G = zeros(Q,Q); Daniel@0: for i=1:Q Daniel@0: for j=1:Q Daniel@0: if A(i,j) < thresh Daniel@0: arclabel{i,j} = ''; Daniel@0: else Daniel@0: G(i,j) = 1; Daniel@0: arclabel{i,j} = sprintf('%5.3f', A(i,j)); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: nodelabel = cell(1,Q); Daniel@0: for i=1:Q Daniel@0: % annotate start/stop states Daniel@0: if ~isempty(startprob) & ~approxeq(startprob(i), 0) Daniel@0: start = '+'; Daniel@0: else Daniel@0: start = ''; Daniel@0: end Daniel@0: if ~isempty(endprob) & ~approxeq(hmm.endprob(i), 0) Daniel@0: stop = '-'; Daniel@0: else Daniel@0: stop = ''; Daniel@0: end Daniel@0: label = sprintf('%s%d%s :', start, i, stop); Daniel@0: Daniel@0: if ~isempty(B) Daniel@0: output_label = mk_output_label(B); Daniel@0: label = strcat(label, output_label); Daniel@0: end Daniel@0: Daniel@0: nodelabel{i} = label; Daniel@0: end Daniel@0: Daniel@0: Daniel@0: if isempty(filename) Daniel@0: filename = 'tmp.dot'; Daniel@0: %mkdot(G, filename, arclabel, nodelabel) Daniel@0: graph_to_dot(G, 'filename', filename, 'arc_label', arclabel, 'node_label', nodelabel); Daniel@0: fprintf('converting from .ps to .dot\n') Daniel@0: !dot -Tps tmp.dot -o tmp.ps Daniel@0: !gv tmp.ps & Daniel@0: else Daniel@0: graph_to_dot(G, 'filename', filename, 'arc_label', arclabel, 'node_label', nodelabel); Daniel@0: %mkdot(G, filename, arclabel, nodelabel) Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %%%%%%%%% Daniel@0: Daniel@0: function label = mk_output_label(B) Daniel@0: Daniel@0: [Q O] = size(B); Daniel@0: label = ''; Daniel@0: Daniel@0: if 0 Daniel@0: % print most probable symbols Daniel@0: for i=1:Q Daniel@0: m = max(B(i,:)); Daniel@0: ndx = find(abs(B(i,:) - repmat(m,1,O)) < 1e-2); Daniel@0: %ndx = find(B(i,:)==m); Daniel@0: %label = sprintf('%d,', ndx); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if 0 Daniel@0: % print prob distrib over all symbols Daniel@0: for o=1:O Daniel@0: if approxeq(B(i,o), 0) Daniel@0: % Daniel@0: else Daniel@0: label = strcat(label, sprintf('%d(%3.2f),', o, B(i,o))); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if 1 Daniel@0: % print all non-zero symbols Daniel@0: chars = ['a' 'b' 'c']; Daniel@0: for o=1:O Daniel@0: if approxeq(B(i,o), 0) Daniel@0: % Daniel@0: else Daniel@0: label = strcat(label, sprintf('%s', chars(o))); Daniel@0: end Daniel@0: end Daniel@0: end