kwspot.cpp
上传用户:avbj512
上传日期:2013-09-18
资源大小:6239k
文件大小:12k
源码类别:

DSP编程

开发平台:

Visual C++

  1. // DHMM_MFC.cpp:
  2. // Defines the entry point for the console application.
  3. //
  4. // Created 2001/08, By DongMing, MDSR.
  5. //
  6. /////////////////////////////////////////////////////////////////////////////
  7. #include "stdafx.h"
  8. #include "kwspot.h"
  9. #include "DHMM_VQ_MFC.h"
  10. #include "DHMM_Model_MFC.h"
  11. #include "DHMM_Recog_MFC.h"
  12. #include <direct.h>
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Private functions
  20. int TheMain(void);
  21. void Init(void);
  22. void KittyInit(void);
  23. void Init_Pro_Config(void);
  24. CString FindProp(CString Prop);
  25. void Run(void);
  26. void Done(void);
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Global variables
  29. PRO_CONFIG u_Pro_Config;
  30. /////////////////////////////////////////////////////////////////////////////
  31. // The one and only application object
  32. CWinApp theApp;
  33. using namespace std;
  34. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  35. {
  36. int nRetCode = 0;
  37. // initialize MFC and print and error on failure
  38. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  39. {
  40. // TODO: change error code to suit your needs
  41. cerr << _T("Fatal Error: MFC initialization failed") << endl;
  42. nRetCode = 1;
  43. }
  44. else
  45. {
  46. // TODO: code your application's behavior here.
  47. nRetCode = TheMain();
  48. }
  49. return nRetCode;
  50. }
  51. // 因为MFC程序框架的原因,此处是程序的main()
  52. static int TheMain(void)
  53. {
  54. char sz_Date_Time[64];
  55. int n_Loop_Index, n_Rotate_Test_Index;
  56. // n_Loop_Index = 0x6789;
  57. Init_Pro_Config();
  58. // 写Result.txt,程序结果纪录
  59. _strdate(sz_Date_Time);
  60. RESULT_LOG("Current Date: %s, ", sz_Date_Time);
  61. _strtime(sz_Date_Time);
  62. RESULT_LOG("Time: %s.n", sz_Date_Time);
  63. RESULT_LOG("Total persons Num = %3d, Training set persons Num = %3d.n", u_Pro_Config.n_Feature_Person_Num, u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num);
  64. RESULT_LOG("ttCoefficientttStart_PersonttAccurate_Raten", u_Pro_Config.d_Feature_DT_MFCC_Coefficient / 3, u_Pro_Config.n_DHMM_Model_Person_Start_Index, u_Pro_Config.d_Recognition_Accurate_Rate);
  65. RESULT_LOG("================================================================================n");
  66. // 第一层循环,循环DT MFCC的倍乘系数
  67. for (n_Loop_Index = 0; n_Loop_Index < u_Pro_Config.n_Loop_Num; n_Loop_Index++)
  68. {
  69. u_Pro_Config.d_Feature_DT_MFCC_Coefficient = 3 * (u_Pro_Config.d_Feature_DT_MFCC_Coefficient_Start + 0.03F * n_Loop_Index);
  70. // 写DHMM.log,程序运行信息纪录
  71. TRACE("nProgram Started ...n");
  72. PRO_LOG("nProgram Started ...n");
  73. _strdate(sz_Date_Time);
  74. PRO_LOG("tCur Date: %s, ", sz_Date_Time);
  75. _strtime(sz_Date_Time);
  76. PRO_LOG("Time: %s.n", sz_Date_Time);
  77. PRO_LOG("tDT_MFCC_Coefficient = %10.4f.nn", u_Pro_Config.d_Feature_DT_MFCC_Coefficient / 3.0F);
  78. double * pd = new double[u_Pro_Config.n_Rotate_Test_Num];
  79. ASSERT(pd != NULL);
  80. // 第二层循环,循环滚动测试的训练集与测试集
  81. for (n_Rotate_Test_Index = 0; n_Rotate_Test_Index < u_Pro_Config.n_Rotate_Test_Num; n_Rotate_Test_Index++)
  82. {
  83. PRO_LOG("Leave-one-out Loop: %4d/%4d.n", n_Rotate_Test_Index, u_Pro_Config.n_Rotate_Test_Num);
  84. u_Pro_Config.n_DHMM_Model_Person_Start_Index = (u_Pro_Config.n_DHMM_Model_Person_Start_Index + (u_Pro_Config.n_Feature_Person_Num / u_Pro_Config.n_Rotate_Test_Num) * n_Rotate_Test_Index) % u_Pro_Config.n_Feature_Person_Num;
  85. u_Pro_Config.n_DHMM_Model_Person_End_Index = (u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num + u_Pro_Config.n_DHMM_Model_Person_Start_Index - 1) % u_Pro_Config.n_Feature_Person_Num;
  86. u_Pro_Config.n_Recog_Person_Start_Index = (u_Pro_Config.n_DHMM_Model_Person_End_Index + 1) % u_Pro_Config.n_Feature_Person_Num;
  87. u_Pro_Config.n_Recog_Person_End_Index = (u_Pro_Config.n_Recog_Person_Start_Index + u_Pro_Config.n_Recog_Set_Person_Num - 1) % u_Pro_Config.n_Feature_Person_Num;
  88. sprintf(u_Pro_Config.sz_Toload_Code_Book_File_Name, u_Pro_Config.sz_Toload_Code_Book_File_fmt, n_Rotate_Test_Index);
  89. sprintf(u_Pro_Config.sz_Toload_DHMM_Model_File_Name, u_Pro_Config.sz_Toload_DHMM_Model_File_fmt, n_Rotate_Test_Index);
  90. Run();
  91. pd[n_Rotate_Test_Index] = u_Pro_Config.d_Recognition_Accurate_Rate;
  92. }
  93. // 统计识别率。
  94. double d = 0.0F;
  95. for (n_Rotate_Test_Index = 0; n_Rotate_Test_Index < u_Pro_Config.n_Rotate_Test_Num; n_Rotate_Test_Index++)
  96. d += pd[n_Rotate_Test_Index];
  97. RESULT_LOG("nAvg. Accuracy Rate = %8.4f.nn", d / u_Pro_Config.n_Rotate_Test_Num);
  98. RESULT_LOG("Code_Book_Size = %4d.t", u_Pro_Config.n_VQ_Code_Book_Size);
  99. RESULT_LOG("tDHMM_Model_Num = %4d.t", u_Pro_Config.n_DHMM_Model_Num);
  100. RESULT_LOG("tDHMM_Model_State_Num = %4d.tn", u_Pro_Config.n_DHMM_Model_State_Num);
  101. delete [] pd;
  102. }
  103. RESULT_LOG("n");
  104. TRACE("Program Ended ...nn");
  105. PRO_LOG("Program Ended ...n");
  106. Done();
  107. return 0;
  108. }
  109. static void Init_Pro_Config(void)
  110. {
  111. memset(&u_Pro_Config, 0, sizeof(u_Pro_Config));
  112. u_Pro_Config.l_Prgram_Start_Time = time(NULL);
  113. u_Pro_Config.n_Loop_Num = 1;
  114. u_Pro_Config.n_Rotate_Test_Num = 1;
  115. strcpy(u_Pro_Config.sz_Program_Log_File_Name, "..\..\DATA\DHMM.LOG");
  116. strcpy(u_Pro_Config.sz_Result_File_Name, "..\..\DATA\Result.TXT");
  117.    char buffer[_MAX_PATH];
  118. /* Get the current working directory: */
  119. if( _getcwd( buffer, _MAX_PATH ) == NULL )
  120. perror( "_getcwd error" );
  121. else
  122. printf( "%sn", buffer );
  123. u_Pro_Config.n_Feature_Person_Num = 100;
  124. CString pv = FindProp("Feature_Person_Num"); 
  125. if (pv != "")
  126. sscanf(pv, "%d", &u_Pro_Config.n_Feature_Person_Num);
  127. u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num = (u_Pro_Config.n_Feature_Person_Num * 4 / 5);
  128. u_Pro_Config.n_DHMM_Model_Person_Start_Index = 25;
  129. u_Pro_Config.n_DHMM_Model_Person_End_Index = (u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num + u_Pro_Config.n_DHMM_Model_Person_Start_Index - 1) % u_Pro_Config.n_Feature_Person_Num;
  130. u_Pro_Config.n_Recog_Person_Start_Index = 0;
  131. u_Pro_Config.n_Recog_Person_End_Index = 0;
  132. u_Pro_Config.n_Recog_Set_Person_Num = u_Pro_Config.n_Feature_Person_Num - u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num;
  133. strcpy(u_Pro_Config.sz_Feature_Origin_File_Name_Format, "..\..\kitty_8k_0db\%03d_LPCCFIX.DAT");
  134. pv = FindProp("Feature_Origin_File_Name_Format"); 
  135. if (pv != "")
  136. sscanf(pv, "%s", &u_Pro_Config.sz_Feature_Origin_File_Name_Format);
  137. strcpy(u_Pro_Config.sz_Recog_Origin_File_Name_Format, u_Pro_Config.sz_Feature_Origin_File_Name_Format);
  138. pv = FindProp("Recog_Origin_File_Name_Format"); 
  139. if (pv != "")
  140. sscanf(pv, "%s", &u_Pro_Config.sz_Recog_Origin_File_Name_Format);
  141. u_Pro_Config.n_Feature_Dim = 16;
  142. u_Pro_Config.n_Feature_DT_MFCC_Start_Index = u_Pro_Config.n_Feature_Dim / 2;
  143. u_Pro_Config.n_Feature_DT_MFCC_End_Index = u_Pro_Config.n_Feature_Dim - 1;
  144. u_Pro_Config.n_Feature_Sentence_Num = 13;
  145. pv = FindProp("Feature_Sentence_Num"); 
  146. if (pv != "")
  147. sscanf(pv, "%d", &u_Pro_Config.n_Feature_Sentence_Num);
  148. u_Pro_Config.n_DHMM_Model_Num = u_Pro_Config.n_Feature_Sentence_Num;
  149. pv = FindProp("DHMM_Model_Num"); 
  150. if (pv != "")
  151. sscanf(pv, "%d", &u_Pro_Config.n_DHMM_Model_Num);
  152. u_Pro_Config.n_Sentence_Start_Index = 0;
  153. pv = FindProp("Sentence_Start_Index"); 
  154. if (pv != "")
  155. sscanf(pv, "%d", &u_Pro_Config.n_Sentence_Start_Index);
  156. u_Pro_Config.n_VQ_Code_Book_Size = 256;
  157. pv = FindProp("VQ_Code_Book_Size"); 
  158. if (pv != "")
  159. sscanf(pv, "%d", &u_Pro_Config.n_VQ_Code_Book_Size);
  160. u_Pro_Config.n_DHMM_Model_State_Num = 6;
  161. pv = FindProp("DHMM_Model_State_Num"); 
  162. if (pv != "")
  163. sscanf(pv, "%d", &u_Pro_Config.n_DHMM_Model_State_Num);
  164. u_Pro_Config.d_Feature_DT_MFCC_Coefficient_Start = 0.333;
  165. u_Pro_Config.USE_SILENCE_FEA = 1;
  166. u_Pro_Config.WATCH_RECOG_RESULT = 0;
  167. pv = FindProp("WATCH_RECOG_RESULT"); 
  168. if (pv != "")
  169. sscanf(pv, "%d", &u_Pro_Config.WATCH_RECOG_RESULT);
  170. CString pp = "VQ_Config";
  171. pv = FindProp(pp); 
  172. if (pv.Find("VQ_CONFIG_LOAD_ONLY_CODE_BOOK",0) != -1)
  173. u_Pro_Config.l_VQ_Config = VQ_CONFIG_LOAD_ONLY_CODE_BOOK;
  174. else if (pv.Find("VQ_CONFIG_TRAIN_ONLY_CODE_BOOK",0) != -1)
  175. u_Pro_Config.l_VQ_Config = VQ_CONFIG_TRAIN_ONLY_CODE_BOOK | VQ_CONFIG_LOAD_TRAIN_SET_ONLY | VQ_CONFIG_TRAIN_PROCEDURE_GL;
  176. else
  177.      u_Pro_Config.l_VQ_Config = VQ_CONFIG_LOAD_ONLY_CODE_BOOK;
  178. pp = "DHMM_Model_Config";
  179. pv = FindProp(pp); 
  180. if (pv.Find("MODEL_CONFIG_TRAIN_WITH_SILENCE_MODEL",0) != -1)
  181. u_Pro_Config.l_DHMM_Model_Config = MODEL_CONFIG_TRAIN_WITH_SILENCE_MODEL | MODEL_CONFIG_TRAIN_PROCEDURE_GL;
  182. else
  183. u_Pro_Config.l_DHMM_Model_Config = MODEL_CONFIG_LOAD_WITH_SILENCE_MODEL ;
  184. u_Pro_Config.l_DHMM_Recog_Config = RECOG_CONFIG_METHOD_VITERBI_ONLY | RECOG_CONFIG_RECOG_PROCEDURE_HQ;
  185. u_Pro_Config.n_Recog_Sentence_Num = u_Pro_Config.n_Feature_Sentence_Num;
  186. u_Pro_Config.n_Recog_Person_Num = u_Pro_Config.n_Feature_Person_Num;
  187. strcpy(u_Pro_Config.sz_Toload_Code_Book_File_fmt, "..\..\DATA\DWDN_CB_%02d.DAT");
  188. strcpy(u_Pro_Config.sz_Toload_DHMM_Model_File_fmt, "..\..\DATA\DWDN_Model_%02d.DAT");
  189. strcpy(u_Pro_Config.sz_Recog_LLS_File, "..\..\data\Recog_Ret\Kitty_Recog_LLS_Ret.txt");
  190. pv = FindProp("Recog_LLS_File"); 
  191. if (pv != "")
  192. sscanf(pv, "%s", &u_Pro_Config.sz_Recog_LLS_File);
  193. if(u_Pro_Config.WATCH_RECOG_RESULT == 1)
  194. {
  195. char RetFile[100];
  196. strcpy(RetFile,u_Pro_Config.sz_Recog_LLS_File);
  197. FILE* fp = fopen(RetFile, "wt");
  198. ASSERT(fp!=NULL);
  199. fprintf(fp, "t");
  200. fclose(fp);
  201. }
  202. srand(time(NULL));
  203. }
  204. // 一次Run(),可以进行一次码本训练,模型训练及识别的全过程,
  205. // 但是根据程序配置不同,可以略去某些环节
  206. static void Run(void)
  207. {
  208. int nRetCode;
  209. char sz_Date_Time[64];
  210. u_Pro_Config.l_Prgram_Start_Time = time(NULL);
  211. // 写DHMM.log,程序运行信息纪录
  212. _strdate(sz_Date_Time);
  213. PRO_LOG("tCur Date: %s, ", sz_Date_Time);
  214. _strtime(sz_Date_Time);
  215. PRO_LOG("Time: %s.n", sz_Date_Time);
  216. PRO_LOG("tDT_MFCC_Coefficient = %10.4f.n", u_Pro_Config.d_Feature_DT_MFCC_Coefficient / 3.0F);
  217. PRO_LOG("tTraining set person num = %3d.n", u_Pro_Config.n_DHMM_Model_Train_Set_Person_Num);
  218. PRO_LOG("tTraining set start person No. = %3d.n", u_Pro_Config.n_DHMM_Model_Person_Start_Index);
  219. // 进行码本训练,使用文件作为输入输出
  220. //  这里是实际运行的部分。王栋
  221. PRO_LOG("DHMM_VQ...n");
  222. PRO_LOG("tVQ Method Config = 0x%X.n", u_Pro_Config.l_VQ_Config);
  223. nRetCode = DHMM_VQ();
  224. ASSERT(nRetCode == 0);
  225. DHMM_VQ_To_519_CodeBook();
  226. ELAPSED_TIME_OUTPUT();
  227. PRO_LOG("n");
  228. // 进行模型训练,使用文件作为输入输出
  229. PRO_LOG("DHMM_Model...n");
  230. PRO_LOG("tModel Method Config = 0x%X.n", u_Pro_Config.l_DHMM_Model_Config);
  231. nRetCode = DHMM_Model();
  232. ASSERT(nRetCode == 0);
  233. DHMM_Model_To_519_Model();
  234. ELAPSED_TIME_OUTPUT();
  235. PRO_LOG("n");
  236. // 进行识别测试,使用文件作为输入,结果回存向程序配置u_Pro_Config中
  237. PRO_LOG("DHMM_Recog...n");
  238. PRO_LOG("tRecognition Method Config = 0x%X.n", u_Pro_Config.l_DHMM_Recog_Config);
  239. PRO_LOG("tRecognition set person num = %3d.n", u_Pro_Config.n_Recog_Set_Person_Num);
  240. PRO_LOG("tRecognition set start person No. = %3d.n", u_Pro_Config.n_Recog_Person_Start_Index);
  241. nRetCode = DHMM_Recog_Kitty();
  242. PRO_LOG("Origin File Format = %sn", u_Pro_Config.sz_Feature_Origin_File_Name_Format);
  243. PRO_LOG("Recog File Format = %sn", u_Pro_Config.sz_Recog_Origin_File_Name_Format);
  244. ASSERT(nRetCode == 0);
  245. ELAPSED_TIME_OUTPUT();
  246. PRO_LOG("n");
  247. }
  248. static void Done(void)
  249. {
  250. }
  251. CString FindProp(CString Prop)
  252. {
  253. CStdioFile fin;
  254. CFileException e;
  255. CString Pro_Config_File_Name =  "..\..\DATA\config_kitty.txt";
  256. if( !fin.Open( Pro_Config_File_Name, CFile::modeRead, &e ) )
  257. {
  258. #ifdef _DEBUG
  259. afxDump << "File could not be opened " << e.m_cause << "n";
  260. #endif
  261. }
  262. CString line;
  263. char t = 't';
  264. CString pv = "";
  265. while(!feof(fin.m_pStream))
  266. {
  267. while(true)
  268. {
  269. fin.Read(&t,1); 
  270. if((t != 'n') & !feof(fin.m_pStream))
  271. {
  272. line += t;
  273. }
  274. else
  275. break;
  276. }
  277. int i = line.Find(Prop,0); 
  278. if(i != -1)
  279. {
  280. line.Delete(0, Prop.GetAllocLength());
  281. fin.Close();
  282. line.TrimLeft();
  283. line.TrimRight();
  284. return line;
  285. }
  286. line = "";
  287. }
  288. fin.Close();
  289. return line;
  290. }