www.pudn.com > asr.rar > HMMGEN.M, change:2004-11-06,size:1442b


function [output,states] = hmmgen(a,b,pai,T) 
 
%% generate an output sequence of an HMM model 
%% 
%%  [output,states] = hmmgen(a,b,pai,T) 
%% 
%%  output  --- output sequence 
%%  states  --- state sequence 
%%  a  --- state transition probabilities 
%%  b  --- observation symbol probabilities 
%%  pai --- initial state probabilities 
%%  T --- length of observation seq. 
 
%% initialization 
N = size(a,1); 
M = size(b,1); 
output = zeros(T,1); 
states = zeros(T,1); 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% map probabilities to line segment [0,1] %% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
pai_rng=zeros(N,1); 
start=0; 
for i=1:N 
   start = start + pai(i); 
   pai_rng(i) = start; 
end 
 
a_rng=zeros(N,N); 
for i=1:N 
   start=0; 
   for j=1:N 
     start = start + a(i,j); 
     a_rng(i,j)=start; 
   end 
end 
   
b_rng=zeros(M,N); 
for i=1:N 
   start=0; 
   for j=1:M 
     start = start + b(j,i); 
     b_rng(j,i)=start; 
   end 
end 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% generate an output sequence %% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 
%% generate initial state and observation 
det = find(pai_rng > rand); 
states(1) = det(1); 
det = find(b_rng(:,states(1)) > rand); 
output(1) = det(1); 
 
%% generate the rest of the sequence 
for t=2:T 
    det = find(a_rng(states(t-1),:) > rand); 
    states(t) = det(1); 
    det = find(b_rng(:,states(t)) > rand); 
    output(t) = det(1);    
end 
 
 
return