0001 function [m,v]=psycdigit(proc,r,mode,p,q,xp,noise,fn,dfile,ofile)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 persistent tok mtok tigflgp digitsp
0103 if nargin<10
0104 ofile='';
0105 if nargin<9
0106 dfile='F:\home\dmb\data\old_speech\tidigits';
0107
0108 if nargin<8
0109 fn=16000;
0110 if nargin<7
0111 noise=[];
0112 if nargin<6
0113 xp=[];
0114 if nargin<5
0115 q=[];
0116 if nargin<4
0117 p=[];
0118 if nargin<3
0119 mode='';
0120 if nargin<2
0121 r=[];
0122 end
0123 end
0124 end
0125 end
0126 end
0127 end
0128 end
0129 end
0130 end
0131
0132
0133
0134 i=1;
0135 pv=[0 3 3 1 25 0 0 1 1 1 1 100];
0136 px=[0 3 3 1 25 20 18 1 1 1 1 100];
0137 pvs='jlLmnxXvVfFb';
0138 pf=zeros(1,52);
0139 mval=0;
0140 lmode=length(mode);
0141 while i<=lmode
0142 if i<lmode
0143 [v,nv,e,ni]=sscanf(mode(i+1:end),'%d',1);
0144 else
0145 nv=0;
0146 ni=1;
0147 end
0148 j=find(mode(i)==pvs,1);
0149 k=1+2*(double(lower(mode(i)))-'a')+(mode(i)<'a');
0150 if k>=1 && k<=52
0151 pf(k)=1-pf(k);
0152 if ~isempty(j)
0153 if nv==0
0154 pv(j)=px(j);
0155 else
0156 pv(j)=v;
0157 mval=mval || j==4;
0158 end
0159 end
0160 end
0161 i=i+ni;
0162 end
0163
0164
0165
0166 varthr=20;
0167 nxevo=30;
0168
0169 if pf(23)>pf(24) || pv(2)>pv(3)
0170 pv(3)=pv(2);
0171 end
0172 if pf(24)>pf(23)
0173 pv(2)=1;
0174 end
0175 zmodel=pv(4)+(pf(26)>0);
0176 ntrial=zmodel*pv(5);
0177 if pf(12)>pf(11)
0178 pv(10)=pv(11);
0179 end
0180 if pf(11)+pf(12)>0 && (pv(10)<1 || pv(10)>3)
0181 error('Invalid average type for option f/F');
0182 end
0183
0184
0185 unimode=pf(41);
0186 if isempty(p)
0187 p=0.75;
0188 end
0189 if size(p,1)<3-unimode
0190 p(2-unimode,1)=0.04;
0191 p(3-unimode,1)=0;
0192 end
0193 if size(p,2)<(zmodel)
0194 p=p(:,1+mod(0:zmodel-1,size(p,2)));
0195 end
0196
0197
0198
0199 tigflg=[pv(2:3) pf(51:52)];
0200 if isempty(tok) || any(tigflg~=tigflgp) || ~strcmp(dfile,digitsp)
0201 disp('Scanning TIDIGITS files ...');
0202 digitsp=dfile;
0203 tigflgp=tigflg;
0204 if any(dfile(end)=='/\')
0205 dfile(end)=[];
0206 end
0207 dirlist{1}='';
0208 ntok=0;
0209 mtok=zeros(1,pv(3));
0210 tok=cell(1,2);
0211 while ~isempty(dirlist)
0212 dd=dir([dfile dirlist{1}]);
0213 for i=1:length(dd)
0214 name=dd(i).name;
0215 if name(1)~='.'
0216 if dd(i).isdir
0217 dirlist{end+1}=[dirlist{1} '\' name];
0218 elseif length(name)>4 && strcmpi(name(end-3:end),'.wav')
0219 digs=name(1:end-4);
0220 digz=upper(digs)=='Z';
0221 digo=upper(digs)=='O';
0222 digs(digo | digz)='0';
0223 digs=digs(digs>='0' & digs<='9');
0224 ndigs=length(digs);
0225 if ndigs>=tigflg(1) && ndigs<=tigflg(2) && ~(tigflg(3) && any(digo)) && ~(tigflg(4) && any(digz))
0226 ntok=ntok+1;
0227 mtok(ndigs)=mtok(ndigs)+1;
0228 tok{ntok,1}=[dirlist{1} '\' name];
0229 tok{ntok,2}=digs;
0230 end
0231 end
0232 end
0233 end
0234 dirlist(1)=[];
0235 end
0236 end
0237 ntok=size(tok,1);
0238 [s,fs]=readsph([dfile tok{1,1}]);
0239
0240
0241
0242 if any(mode=='d')
0243 p(3-unimode,:)=0.1;
0244 else
0245 p(3-unimode,:)=0.1.^(1:pv(3))*mtok'/ntok;
0246 end
0247
0248
0249
0250 if unimode
0251 [xx,ii,m,v]=psycestu(-zmodel,p,q,xp);
0252 [pact,qact]=psycestu(0);
0253 else
0254 [xx,ii,m,v]=psycest(-zmodel,p,q,xp);
0255 [pact,qact]=psycest(0);
0256 end
0257 if pv(4) && pf(5)
0258 x=[];
0259 ii=1;
0260 if pf(1)
0261 if isempty(r)
0262 if mval
0263 procdesc=proc(x,fs,xx,ii);
0264 else
0265 procdesc=proc(x,fs,xx);
0266 end
0267 else
0268 if mval
0269 procdesc=proc(x,fs,xx,ii,r);
0270 else
0271 procdesc=proc(x,fs,xx,r);
0272 end
0273 end
0274 else
0275 if isempty(r)
0276 if mval
0277 procdesc=proc(x,fs,ii);
0278 else
0279 procdesc=proc(x,fs);
0280 end
0281 else
0282 if mval
0283 y=proc(x,fs,ii,r);
0284 else
0285 y=proc(x,fs,r);
0286 end
0287 end
0288 end
0289 else
0290 procdesc='';
0291 end
0292
0293
0294
0295 if pf(29) || pf(30)
0296 nw=fix(datevec(now));
0297 of=sprintf('psy%4d%02d%02d%02d%02d.txt',nw(1:5));
0298 ofid=fopen(of,'wt');
0299 if ~ofid
0300 error('Cannot write to %s',of);
0301 end
0302 fprintf(ofid,'%% %s evaluation on %s\n',mfilename,datestr(now));
0303 fmfnm=[mfilename('fullpath') '.m'];
0304 dd=dir(fmfnm);
0305 fprintf(ofid,'%% %s = %d bytes = %s\n',fmfnm,dd.bytes,dd.date);
0306 fprintf(ofid,'V %d\n',2);
0307 fmfnm=which(func2str(proc));
0308 dd=dir(fmfnm);
0309 fprintf(ofid,'P %s = %d bytes = %s\n',fmfnm,dd.bytes,dd.date);
0310 if numel(procdesc)
0311 fprintf(ofid,'C %s\n',procdesc);
0312 end
0313 fprintf(ofid,'O %s\n',mode);
0314 end
0315
0316
0317
0318 disp(['Testing ' procdesc]);
0319
0320 mnt=zeros(2+2*unimode,zmodel,3,ntrial+1);
0321 vnt=zeros(3+unimode,zmodel,ntrial+1);
0322 mnt(:,:,:,1)=m;
0323 vnt(:,:,1)=v;
0324 i=0;
0325 imax=0;
0326 quitit=0;
0327 while ~quitit
0328 i=i+1;
0329 isp=min(1+floor(rand(1)*ntok),ntok);
0330 [s,fs]=readsph([dfile tok{isp,1}]);
0331 s=[zeros(pv(6)*round(fs/10),1); activlev(s(:),fs,'n')];
0332 if pf(1) && ii<=pv(4)
0333 if isempty(r)
0334 if mval
0335 y=proc(s,fs,xx,ii);
0336 else
0337 y=proc(s,fs,xx);
0338 end
0339 else
0340 if mval
0341 y=proc(s,fs,xx,ii,r);
0342 else
0343 y=proc(s,fs,xx,r);
0344 end
0345 end
0346 else
0347 nn=randn(length(s),1);
0348 x=nn+s*10^(xx/20);
0349 if ii<=pv(4)
0350 if isempty(r)
0351 if mval
0352 y=proc(x,fs,ii);
0353 else
0354 y=proc(x,fs);
0355 end
0356 else
0357 if mval
0358 y=proc(x,fs,ii,r);
0359 else
0360 y=proc(x,fs,r);
0361 end
0362 end
0363 else
0364 y=x;
0365 end
0366 end
0367 y=y(1+pv(7)*round(fs/10):end);
0368 if pf(13)
0369 prg=sprintf(' %d',length(tok{isp,2}));
0370 else
0371 prg='';
0372 end
0373 if pf(14)
0374 prG=sprintf('SNR=%d dB, ',round(xx));
0375 else
0376 prG='';
0377 end
0378 if pf(35)
0379 prr=sprintf(', ''r'' to repeat');
0380 else
0381 prr='';
0382 end
0383 prompt=[prG 'enter' prg ' digits (''q'' to quit' prr '): '];
0384 ansr=-(var(y)>varthr);
0385 say=1;
0386 while ansr<0
0387 if say
0388 tdel=0;
0389 tic;
0390 soundsc(y,fs);
0391 say=0;
0392 end
0393 rv=input(prompt,'s');
0394 tdel=toc;
0395 if ~isempty(rv)
0396 if lower(rv(1))=='q'
0397 quitit=1;
0398 ansr=2;
0399 elseif lower(rv(1))=='r' && pf(35)
0400 say=1;
0401 elseif lower(rv(1))=='s' && pf(37)
0402 ofn=input('File name: ','s');
0403 if numel(ofn)
0404 writewav(y,fs,ofn);
0405 end
0406 elseif all(rv>='0') && all(rv<='9') && ( ~pf(13) || length(rv)==length(tok{isp,2}))
0407 ansr=strcmp(rv,tok{isp,2});
0408 end
0409 end
0410 end
0411 quitit=quitit || i==ntrial;
0412 jj=ii;
0413 xxold=xx;
0414 if ansr<2
0415 if unimode
0416 [xx,ii,m,v]=psycestu(ii,xx,ansr);
0417 else
0418 [xx,ii,m,v]=psycest(ii,xx,ansr);
0419 end
0420 mnt(:,:,:,i+1)=m;
0421 vnt(:,:,i+1)=v;
0422 imax=i;
0423 end
0424 if pf(30) || quitit && pf(29)
0425 if ansr>1
0426 rv=num2str(double(rv(1)));
0427 end
0428
0429 fprintf(ofid,'M %d %d %.3g %s %s %d %.1f',i,ii,xxold,tok{isp,2},rv,ansr,tdel);
0430 fprintf(ofid,' %.3g',m(:,jj,:),v(:,jj));
0431 fprintf(ofid,'\n');
0432 end
0433 if pf(32)
0434 figure(jj);
0435 if unimode
0436 psycestu(jj);
0437 else
0438 psycest(jj);
0439 end
0440 elseif quitit && pf(31)
0441 for jj=1:zmodel
0442 figure(jj);
0443 if unimode
0444 psycestu(jj);
0445 else
0446 psycest(jj);
0447 end
0448 end
0449 end
0450 if pf(12) || quitit && pf(11)
0451 figure(pv(12)+1);
0452 if unimode
0453 qqu.pk=m(1,jj,pv(10));
0454 qqu.w=m(2,1,pv(10));
0455 qqu.ll=m(3,1,pv(10));
0456 qqu.lh=m(4,1,pv(10));
0457 qqu.gu=pact(2);
0458 qqu.la=pact(1);
0459 sw=qqu.w*qqu.ll*qqu.lh/(qqu.ll+qqu.lh);
0460 xax=linspace(qqu.pk-(4+sw)/qqu.ll,qqu.pk+(4+sw)/qqu.lh,200);
0461 bs=(qqu.lh-qqu.ll)/2;
0462 bu=(qqu.lh+qqu.ll)/2;
0463 plot(xax,qqu.gu+(1-qqu.gu-qqu.la)*(1+2*exp(-sw)*cosh(bs*(xax-qqu.pk)+bu*abs(xax-qqu.pk))+exp(-2*sw)).^(-1));
0464 else
0465 sd=(pact(1,:)-pact(3,:)).*(1-pact(2,:)-pact(1,:))./(m(2,:,pv(10)).*(1-pact(3,:)-pact(2,:)));
0466 md=m(1,:,pv(10))-sd.*log((pact(1,:)-pact(3,:))./(1-pact(2,:)-pact(1,:)));
0467 xax=linspace(min(md-3*sd),max(md+3*sd),100);
0468 for jj=1:zmodel
0469 plot(xax,psychofunc('',[pact(1,jj);m(:,jj,pv(10)) ; pact(2:3,jj); qact(11)],xax));
0470 hold on
0471 end
0472 hold off
0473 end
0474 axis([xax(1) xax(end) 0 1]);
0475 xlabel('SNR (dB)');
0476 ylabel('Recognition Probability');
0477 title(sprintf('Intelligibility: %s',procdesc));
0478 end
0479 if pf(10) || quitit && pf(9)
0480 figure(pv(12)+3);
0481 psyevo=zeros(ntrial+1,nxevo);
0482 if unimode
0483 qqu.pk=m(1,jj,pv(10));
0484 qqu.w=m(2,1,pv(10));
0485 qqu.ll=m(3,1,pv(10));
0486 qqu.lh=m(4,1,pv(10));
0487 qqu.gu=pact(2);
0488 qqu.la=pact(1);
0489 sw=qqu.w*qqu.ll*qqu.lh/(qqu.ll+qqu.lh);
0490 xax=linspace(qqu.pk-(4+sw)/qqu.ll,qqu.pk+(4+sw)/qqu.lh,nxevo);
0491 for iet=1:imax+1
0492 qqu.pk=mnt(1,1,pv(10),iet);
0493 qqu.w=mnt(2,1,pv(10),iet);
0494 qqu.ll=mnt(3,1,pv(10),iet);
0495 qqu.lh=mnt(4,1,pv(10),iet);
0496 sw=qqu.w*qqu.ll*qqu.lh/(qqu.ll+qqu.lh);
0497 bs=(qqu.lh-qqu.ll)/2;
0498 bu=(qqu.lh+qqu.ll)/2;
0499 psyevo(iet,:)=qqu.gu+(1-qqu.gu-qqu.la)*(1+2*exp(-sw)*cosh(bs*(xax-qqu.pk)+bu*abs(xax-qqu.pk))+exp(-2*sw)).^(-1);
0500 end
0501 imagesc(xax,0:ntrial,psyevo);
0502 axis 'ij'
0503 else
0504 end
0505 xlabel('SNR (dB)');
0506 ylabel('After trial');
0507 title(sprintf('Intelligibility: %s',procdesc));
0508 end
0509 if pf(44) || quitit && pf(43)
0510 figure(pv(12)+2);
0511 if uniplot
0512 sw=mnt(2,1,pv(10),1:imax+1).*mnt(3,1,pv(10),1:imax+1).*mnt(4,1,pv(10),1:imax+1)./(mnt(3,1,pv(10),1:imax+1).*mnt(4,1,pv(10),1:imax+1));
0513 subplot(221)
0514 plot(0:imax,squeeze(mnt(1,1,pv(10),1:imax+1)));
0515 set(gca,'xlim',[0 ntrial]);
0516 xlabel('After trial');
0517 ylabel('Peak position (dB SNR)');
0518 subplot(222)
0519 plot(0:imax,squeeze(mnt(1,1,pv(10),1:imax+1)-sw.*mnt(3,1,pv(10),1:imax+1)),'-b',0:imax,squeeze(mnt(1,1,pv(10),1:imax+1)+sw.*mnt(4,1,pv(10),1:imax+1)),'-b');
0520 set(gca,'xlim',[0 ntrial]);
0521 xlabel('After trial');
0522 ylabel('Inflections (dB)');
0523 subplot(223)
0524 plot(0:imax,squeeze(mnt(3,1,pv(10),1:imax+1)));
0525 set(gca,'xlim',[0 ntrial]);
0526 xlabel('After trial');
0527 ylabel('Upwards slope (prob/dB)');
0528 subplot(224)
0529 plot(0:imax,squeeze(mnt(4,1,pv(10),1:imax+1)));
0530 set(gca,'xlim',[0 ntrial]);
0531 xlabel('After trial');
0532 ylabel('Downwards slope (prob/dB)');
0533 else
0534 subplot(211);
0535 for jj=1:zmodel
0536 plot(0:imax,squeeze(mnt(1,jj,pv(10),1:imax+1)));
0537 hold on
0538 end
0539 hold off
0540 set(gca,'xlim',[0 ntrial]);
0541 xlabel('After trial');
0542 ylabel('SRT (dB SNR)');
0543 subplot(212);
0544 for jj=1:zmodel
0545 plot(0:i,squeeze(mnt(2,jj,pv(10),1:imax+1)));
0546 hold on
0547 end
0548 hold off
0549 set(gca,'xlim',[0 ntrial]);
0550 xlabel('After trial');
0551 ylabel('Slope (Prob/dB)');
0552 end
0553 end
0554 end
0555 if pf(29) || pf(30)
0556 fclose(ofid);
0557 end