You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. %
  2. % Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. %
  4. % Use of this source code is governed by a BSD-style license
  5. % that can be found in the LICENSE file in the root of the source
  6. % tree. An additional intellectual property rights grant can be found
  7. % in the file PATENTS. All contributing project authors may
  8. % be found in the AUTHORS file in the root of the source tree.
  9. %
  10. function apmtest(task, testname, filepath, casenumber, legacy)
  11. %APMTEST is a tool to process APM file sets and easily display the output.
  12. % APMTEST(TASK, TESTNAME, CASENUMBER) performs one of several TASKs:
  13. % 'test' Processes the files to produce test output.
  14. % 'list' Prints a list of cases in the test set, preceded by their
  15. % CASENUMBERs.
  16. % 'show' Uses spclab to show the test case specified by the
  17. % CASENUMBER parameter.
  18. %
  19. % using a set of test files determined by TESTNAME:
  20. % 'all' All tests.
  21. % 'apm' The standard APM test set (default).
  22. % 'apmm' The mobile APM test set.
  23. % 'aec' The AEC test set.
  24. % 'aecm' The AECM test set.
  25. % 'agc' The AGC test set.
  26. % 'ns' The NS test set.
  27. % 'vad' The VAD test set.
  28. %
  29. % FILEPATH specifies the path to the test data files.
  30. %
  31. % CASENUMBER can be used to select a single test case. Omit CASENUMBER,
  32. % or set to zero, to use all test cases.
  33. %
  34. if nargin < 5 || isempty(legacy)
  35. % Set to true to run old VQE recordings.
  36. legacy = false;
  37. end
  38. if nargin < 4 || isempty(casenumber)
  39. casenumber = 0;
  40. end
  41. if nargin < 3 || isempty(filepath)
  42. filepath = 'data/';
  43. end
  44. if nargin < 2 || isempty(testname)
  45. testname = 'all';
  46. end
  47. if nargin < 1 || isempty(task)
  48. task = 'test';
  49. end
  50. if ~strcmp(task, 'test') && ~strcmp(task, 'list') && ~strcmp(task, 'show')
  51. error(['TASK ' task ' is not recognized']);
  52. end
  53. if casenumber == 0 && strcmp(task, 'show')
  54. error(['CASENUMBER must be specified for TASK ' task]);
  55. end
  56. inpath = [filepath 'input/'];
  57. outpath = [filepath 'output/'];
  58. refpath = [filepath 'reference/'];
  59. if strcmp(testname, 'all')
  60. tests = {'apm','apmm','aec','aecm','agc','ns','vad'};
  61. else
  62. tests = {testname};
  63. end
  64. if legacy
  65. progname = './test';
  66. else
  67. progname = './process_test';
  68. end
  69. global farFile;
  70. global nearFile;
  71. global eventFile;
  72. global delayFile;
  73. global driftFile;
  74. if legacy
  75. farFile = 'vqeFar.pcm';
  76. nearFile = 'vqeNear.pcm';
  77. eventFile = 'vqeEvent.dat';
  78. delayFile = 'vqeBuf.dat';
  79. driftFile = 'vqeDrift.dat';
  80. else
  81. farFile = 'apm_far.pcm';
  82. nearFile = 'apm_near.pcm';
  83. eventFile = 'apm_event.dat';
  84. delayFile = 'apm_delay.dat';
  85. driftFile = 'apm_drift.dat';
  86. end
  87. simulateMode = false;
  88. nErr = 0;
  89. nCases = 0;
  90. for i=1:length(tests)
  91. simulateMode = false;
  92. if strcmp(tests{i}, 'apm')
  93. testdir = ['apm/'];
  94. outfile = ['out'];
  95. if legacy
  96. opt = ['-ec 1 -agc 2 -nc 2 -vad 3'];
  97. else
  98. opt = ['--no_progress -hpf' ...
  99. ' -aec --drift_compensation -agc --fixed_digital' ...
  100. ' -ns --ns_moderate -vad'];
  101. end
  102. elseif strcmp(tests{i}, 'apm-swb')
  103. simulateMode = true;
  104. testdir = ['apm-swb/'];
  105. outfile = ['out'];
  106. if legacy
  107. opt = ['-fs 32000 -ec 1 -agc 2 -nc 2'];
  108. else
  109. opt = ['--no_progress -fs 32000 -hpf' ...
  110. ' -aec --drift_compensation -agc --adaptive_digital' ...
  111. ' -ns --ns_moderate -vad'];
  112. end
  113. elseif strcmp(tests{i}, 'apmm')
  114. testdir = ['apmm/'];
  115. outfile = ['out'];
  116. opt = ['-aec --drift_compensation -agc --fixed_digital -hpf -ns ' ...
  117. '--ns_moderate'];
  118. else
  119. error(['TESTNAME ' tests{i} ' is not recognized']);
  120. end
  121. inpathtest = [inpath testdir];
  122. outpathtest = [outpath testdir];
  123. refpathtest = [refpath testdir];
  124. if ~exist(inpathtest,'dir')
  125. error(['Input directory ' inpathtest ' does not exist']);
  126. end
  127. if ~exist(refpathtest,'dir')
  128. warning(['Reference directory ' refpathtest ' does not exist']);
  129. end
  130. [status, errMsg] = mkdir(outpathtest);
  131. if (status == 0)
  132. error(errMsg);
  133. end
  134. [nErr, nCases] = recurseDir(inpathtest, outpathtest, refpathtest, outfile, ...
  135. progname, opt, simulateMode, nErr, nCases, task, casenumber, legacy);
  136. if strcmp(task, 'test') || strcmp(task, 'show')
  137. system(['rm ' farFile]);
  138. system(['rm ' nearFile]);
  139. if simulateMode == false
  140. system(['rm ' eventFile]);
  141. system(['rm ' delayFile]);
  142. system(['rm ' driftFile]);
  143. end
  144. end
  145. end
  146. if ~strcmp(task, 'list')
  147. if nErr == 0
  148. fprintf(1, '\nAll files are bit-exact to reference\n', nErr);
  149. else
  150. fprintf(1, '\n%d files are NOT bit-exact to reference\n', nErr);
  151. end
  152. end
  153. function [nErrOut, nCases] = recurseDir(inpath, outpath, refpath, ...
  154. outfile, progname, opt, simulateMode, nErr, nCases, task, casenumber, ...
  155. legacy)
  156. global farFile;
  157. global nearFile;
  158. global eventFile;
  159. global delayFile;
  160. global driftFile;
  161. dirs = dir(inpath);
  162. nDirs = 0;
  163. nErrOut = nErr;
  164. for i=3:length(dirs) % skip . and ..
  165. nDirs = nDirs + dirs(i).isdir;
  166. end
  167. if nDirs == 0
  168. nCases = nCases + 1;
  169. if casenumber == nCases || casenumber == 0
  170. if strcmp(task, 'list')
  171. fprintf([num2str(nCases) '. ' outfile '\n'])
  172. else
  173. vadoutfile = ['vad_' outfile '.dat'];
  174. outfile = [outfile '.pcm'];
  175. % Check for VAD test
  176. vadTest = 0;
  177. if ~isempty(findstr(opt, '-vad'))
  178. vadTest = 1;
  179. if legacy
  180. opt = [opt ' ' outpath vadoutfile];
  181. else
  182. opt = [opt ' --vad_out_file ' outpath vadoutfile];
  183. end
  184. end
  185. if exist([inpath 'vqeFar.pcm'])
  186. system(['ln -s -f ' inpath 'vqeFar.pcm ' farFile]);
  187. elseif exist([inpath 'apm_far.pcm'])
  188. system(['ln -s -f ' inpath 'apm_far.pcm ' farFile]);
  189. end
  190. if exist([inpath 'vqeNear.pcm'])
  191. system(['ln -s -f ' inpath 'vqeNear.pcm ' nearFile]);
  192. elseif exist([inpath 'apm_near.pcm'])
  193. system(['ln -s -f ' inpath 'apm_near.pcm ' nearFile]);
  194. end
  195. if exist([inpath 'vqeEvent.dat'])
  196. system(['ln -s -f ' inpath 'vqeEvent.dat ' eventFile]);
  197. elseif exist([inpath 'apm_event.dat'])
  198. system(['ln -s -f ' inpath 'apm_event.dat ' eventFile]);
  199. end
  200. if exist([inpath 'vqeBuf.dat'])
  201. system(['ln -s -f ' inpath 'vqeBuf.dat ' delayFile]);
  202. elseif exist([inpath 'apm_delay.dat'])
  203. system(['ln -s -f ' inpath 'apm_delay.dat ' delayFile]);
  204. end
  205. if exist([inpath 'vqeSkew.dat'])
  206. system(['ln -s -f ' inpath 'vqeSkew.dat ' driftFile]);
  207. elseif exist([inpath 'vqeDrift.dat'])
  208. system(['ln -s -f ' inpath 'vqeDrift.dat ' driftFile]);
  209. elseif exist([inpath 'apm_drift.dat'])
  210. system(['ln -s -f ' inpath 'apm_drift.dat ' driftFile]);
  211. end
  212. if simulateMode == false
  213. command = [progname ' -o ' outpath outfile ' ' opt];
  214. else
  215. if legacy
  216. inputCmd = [' -in ' nearFile];
  217. else
  218. inputCmd = [' -i ' nearFile];
  219. end
  220. if exist([farFile])
  221. if legacy
  222. inputCmd = [' -if ' farFile inputCmd];
  223. else
  224. inputCmd = [' -ir ' farFile inputCmd];
  225. end
  226. end
  227. command = [progname inputCmd ' -o ' outpath outfile ' ' opt];
  228. end
  229. % This prevents MATLAB from using its own C libraries.
  230. shellcmd = ['bash -c "unset LD_LIBRARY_PATH;'];
  231. fprintf([command '\n']);
  232. [status, result] = system([shellcmd command '"']);
  233. fprintf(result);
  234. fprintf(['Reference file: ' refpath outfile '\n']);
  235. if vadTest == 1
  236. equal_to_ref = are_files_equal([outpath vadoutfile], ...
  237. [refpath vadoutfile], ...
  238. 'int8');
  239. if ~equal_to_ref
  240. nErr = nErr + 1;
  241. end
  242. end
  243. [equal_to_ref, diffvector] = are_files_equal([outpath outfile], ...
  244. [refpath outfile], ...
  245. 'int16');
  246. if ~equal_to_ref
  247. nErr = nErr + 1;
  248. end
  249. if strcmp(task, 'show')
  250. % Assume the last init gives the sample rate of interest.
  251. str_idx = strfind(result, 'Sample rate:');
  252. fs = str2num(result(str_idx(end) + 13:str_idx(end) + 17));
  253. fprintf('Using %d Hz\n', fs);
  254. if exist([farFile])
  255. spclab(fs, farFile, nearFile, [refpath outfile], ...
  256. [outpath outfile], diffvector);
  257. %spclab(fs, diffvector);
  258. else
  259. spclab(fs, nearFile, [refpath outfile], [outpath outfile], ...
  260. diffvector);
  261. %spclab(fs, diffvector);
  262. end
  263. end
  264. end
  265. end
  266. else
  267. for i=3:length(dirs)
  268. if dirs(i).isdir
  269. [nErr, nCases] = recurseDir([inpath dirs(i).name '/'], outpath, ...
  270. refpath,[outfile '_' dirs(i).name], progname, opt, ...
  271. simulateMode, nErr, nCases, task, casenumber, legacy);
  272. end
  273. end
  274. end
  275. nErrOut = nErr;
  276. function [are_equal, diffvector] = ...
  277. are_files_equal(newfile, reffile, precision, diffvector)
  278. are_equal = false;
  279. diffvector = 0;
  280. if ~exist(newfile,'file')
  281. warning(['Output file ' newfile ' does not exist']);
  282. return
  283. end
  284. if ~exist(reffile,'file')
  285. warning(['Reference file ' reffile ' does not exist']);
  286. return
  287. end
  288. fid = fopen(newfile,'rb');
  289. new = fread(fid,inf,precision);
  290. fclose(fid);
  291. fid = fopen(reffile,'rb');
  292. ref = fread(fid,inf,precision);
  293. fclose(fid);
  294. if length(new) ~= length(ref)
  295. warning('Reference is not the same length as output');
  296. minlength = min(length(new), length(ref));
  297. new = new(1:minlength);
  298. ref = ref(1:minlength);
  299. end
  300. diffvector = new - ref;
  301. if isequal(new, ref)
  302. fprintf([newfile ' is bit-exact to reference\n']);
  303. are_equal = true;
  304. else
  305. if isempty(new)
  306. warning([newfile ' is empty']);
  307. return
  308. end
  309. snr = snrseg(new,ref,80);
  310. fprintf('\n');
  311. are_equal = false;
  312. end