Home > asr > macroRead.m

macroRead

PURPOSE ^

macroRead: Read a macro file

SYNOPSIS ^

function hmm = macroRead(macroFile)

DESCRIPTION ^

 macroRead: Read a macro file
    Usage: hmm = macroRead(macroFile)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function hmm = macroRead(macroFile)
0002 % macroRead: Read a macro file
0003 %    Usage: hmm = macroRead(macroFile)
0004 
0005 if nargin<1, hmm=selfdemo; return; end
0006 
0007 fid=fopen(macroFile);
0008 i = 0;
0009 flag4oneStreamPerState = 0;  %% 1:讀到的macro中state為one stream, 預設為0
0010 while ~feof(fid)
0011     i = i + 1;
0012     tmp = fgetl(fid);
0013     while isempty(findstr(tmp, '~h')),  %% find hmm's name
0014         tmp = fgetl(fid);
0015         %% 若讀到的macro檔,STREAMINFO為1(即每個state只會有一STREAM)時
0016         %% 在未來讀取STREAM則可省略(因為會讀不到STREAM字串)
0017         if findstr(tmp, '<STREAMINFO> 1'),
0018             streamInd = 1;
0019             flag4oneStreamPerState = 1;
0020         end
0021         %% codes are added by JiaMin @20090210
0022     end
0023     [T, R] = strtok(tmp);
0024     hmm(i).pinyin = strrep(strrep(R, '"' , ''), ' ', '');
0025 %fprintf('i=%d, model=%s\n', i, hmm(i).pinyin);
0026 
0027     tmp = fgetl(fid);
0028     while isempty(findstr(tmp, '<NUMSTATES>')),  %% find states # in hmm
0029         tmp = fgetl(fid);
0030     end
0031     [T, R] = strtok(tmp);
0032     hmm(i).numstates = str2num(R);
0033 %fprintf('\tnumstates=%d\n', hmm(i).numstates);
0034     
0035     tmp = fgetl(fid);
0036     while isempty(findstr(tmp, '<TRANSP>')),  %% read mixture info. of each state
0037         while isempty(findstr(tmp, '<STATE>')),
0038             tmp = fgetl(fid);
0039         end
0040         [T, R] = strtok(tmp);
0041         stateInd = str2num(R);
0042 %fprintf('\tstateInd=%d\n', stateInd);
0043 
0044         while isempty(findstr(tmp, '<NUMMIXES>')),
0045             tmp = fgetl(fid);
0046         end
0047         [T, R] = strtok(tmp);
0048         hmm(i).state(stateInd).nummixes = eval(['[' R ']']);
0049 
0050         while isempty(findstr(tmp, '<SWEIGHTS>')),
0051             tmp = fgetl(fid);
0052         end
0053         %% tmp = fgetl(fid);    %% commend by Jia-Min @ 20081201
0054         [T, R] = strtok(tmp);   %% added by Jia-Min @ 20081201
0055         %% hmm(i).state(stateInd).sweights = eval(['[' tmp ']']); %% commend
0056         %% by Jia-Min @ 20081201
0057         hmm(i).state(stateInd).sweights = eval(['[' R ']']); %% added by Jia-Min @ 20081201
0058 
0059         tmp = fgetl(fid);
0060         while isempty(findstr(tmp, '<STATE>'))&isempty(findstr(tmp, '<TRANSP>')),
0061             %% 若為多個stream的macro file,才需要進一步讀取每個stream
0062             %% 否則直接跳過此步驟
0063             %% if added by JiaMin @20090210
0064             if ~(flag4oneStreamPerState)
0065                 while isempty(findstr(tmp, '<STREAM>')),
0066                     tmp = fgetl(fid);
0067                 end
0068                 [T, R] = strtok(tmp);
0069                 streamInd = str2num(R);
0070             end
0071     
0072             tmp = fgetl(fid);
0073             while isempty(findstr(tmp, '<STREAM>')) & isempty(findstr(tmp, '<STATE>'))&isempty(findstr(tmp, '<TRANSP>')),
0074                 while isempty(findstr(tmp, '<MIXTURE>')),
0075                     tmp = fgetl(fid);
0076                 end
0077                 [T, R] = strtok(tmp);
0078                 [T, R] = strtok(R);
0079                 mixInd = str2num(T);
0080                 hmm(i).state(stateInd).stream(streamInd).mix(mixInd).weight = str2double(R);
0081                 
0082                 tmp = fgetl(fid);
0083                 while isempty(findstr(tmp, '<MEAN>')),
0084                     tmp = fgetl(fid);
0085                 end
0086                 %[T, R] = strtok(tmp);
0087                 %meanNum = str2num(R);
0088 
0089                 tmp = fgetl(fid);
0090                 hmm(i).state(stateInd).stream(streamInd).mix(mixInd).mean = eval(['[' tmp ']']);
0091                 
0092                 %[T, R] = strtok(tmp);
0093                 %hmm(i).state(stateInd).stream(streamInd).mix(mixInd).mean(1).value = str2double(T);
0094                 %for j=2:meanNum,
0095                 %    [T, R] = strtok(R);
0096                 %    hmm(i).state(stateInd).stream(streamInd).mix(mixInd).mean(j).value = str2double(T);
0097                 %end
0098 
0099                 tmp = fgetl(fid);
0100                 while isempty(findstr(tmp, '<VARIANCE>')),
0101                     tmp = fgetl(fid);
0102                 end
0103                 %[T, R] = strtok(tmp);
0104                 %varianceNum = str2num(R);
0105 
0106                 tmp = fgetl(fid);
0107                 hmm(i).state(stateInd).stream(streamInd).mix(mixInd).variance = eval(['[' tmp ']']);                
0108                 %[T, R] = strtok(tmp);
0109                 %hmm(i).state(stateInd).stream(streamInd).mix(mixInd).variance(1).value = str2double(T);
0110                 %for j=2:varianceNum,
0111                 %    [T, R] = strtok(R);
0112                 %    hmm(i).state(stateInd).stream(streamInd).mix(mixInd).variance(j).value = str2double(T);
0113                 %end
0114     
0115                 tmp = fgetl(fid);
0116                 while isempty(findstr(tmp, '<GCONST>')),
0117                     tmp = fgetl(fid);
0118                 end
0119                 [T, R] = strtok(tmp);
0120                 hmm(i).state(stateInd).stream(streamInd).mix(mixInd).gconst = str2double(R);
0121                 tmp = fgetl(fid);
0122             end
0123         end
0124     end
0125     while isempty(findstr(tmp, '<TRANSP>')),
0126         tmp = fgetl(fid);
0127     end
0128     [T, R] = strtok(tmp);
0129     transpNum = str2num(R);
0130     tmp = fgetl(fid);
0131     for j=1:transpNum-1,
0132         tmp = strcat(tmp, ';', fgetl(fid));
0133     end
0134     hmm(i).transp = eval(['[' tmp ']']);
0135     
0136     fgetl(fid);
0137 end    
0138 fclose(fid);
0139 
0140 % ====== Self demo
0141 function hmm=selfdemo
0142 [parentDir, junk, junk, junk]=fileparts(which(mfilename));
0143 macroFile=[parentDir, '\exe\macro\taihua.mac'];
0144 macroFile='d:\users\jang\app\asr\macro\taihua.mac';
0145 fprintf('It will take about 15 seconds to load 526 acoustic models...\n');
0146 tic
0147 hmm=feval(mfilename, macroFile);
0148 fprintf('%g seconds!\n', toc);

Generated on Tue 01-Jun-2010 09:50:19 by m2html © 2003