35 lines
866 B
Matlab
35 lines
866 B
Matlab
function showMarkovDiagram(sequence,M)
|
|
|
|
|
|
if isa(sequence,'Signal')
|
|
sequence = sequence.signal;
|
|
end
|
|
sequence = sequence(2+mod(1,length(sequence)):end); %filtered sequences often have one "old" sample at idx=1
|
|
|
|
x = sequence;
|
|
|
|
levels = sort(unique(x)).'; % or provide known 1x6 level values
|
|
|
|
[~,ix] = min(abs(x - levels),[],2);
|
|
x = levels(ix);
|
|
|
|
K = numel(levels);
|
|
% map to state indices 1..K
|
|
[tf, idx] = ismember(x, levels);
|
|
idx = idx(:);
|
|
from = idx(1:end-1);
|
|
to = idx(2:end);
|
|
from = idx(1:2:end);
|
|
to = idx(2:2:end);
|
|
|
|
% counts C(from,to)
|
|
C = accumarray([from,to], 1, [K K], @sum, 0);
|
|
% row-stochastic transition matrix P(to|from)
|
|
rowSums = sum(C,2);
|
|
P = C ./ max(rowSums,1);
|
|
|
|
mc = dtmc(P, 'StateNames', string(levels.*PAMmapper(M,0).get_scaling));
|
|
figure('Name','Markov Graph (dtmc)');
|
|
gp = graphplot(mc, 'ColorEdges',true, 'LabelEdges',true);
|
|
|
|
end |