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