function b=wordrec(x,mod)

%b=wordrec(x,mod), x is the signal, mod is the model, and b is the output
clear formant
p=8;
f=x;
 f=filter([1 -15/16],1,f);
 f=f(1:4500);
 a=find(f>.045);
 f=[f(a(1):4500); zeros(a(1)-1,1)];
 l=300;
 for d=1:42
  F=f(1+(d-1)*l/3:(d+2)*l/3);
  F=F.*hamming(l);
  y=ar(F,p);
  [a,b]=th2tf(y);
  Form(:,d)=roots(b);
 end


%---discards poles at 0, pi, and mag<.8
for i=1:p
 for j=1:42
  if abs(Form(i,j))<.7
   Form(i,j)=NaN;
  end
  if angle(Form(i,j))<.001
   Form(i,j)=NaN;
  end
  if angle(Form(i,j))>3.14
   Form(i,j)=NaN;
  end
 end
end

%---forms 3X42 matrix with poles ordered
for j=1:42
 for i=1:3
  [a,b]=min(angle(Form(:,j)));
  formant(i,j)=Form(b,j);
  Form(b,j)=NaN;
 end
end 
formant=angle(formant);
%subplot(2,1,1)
%j=1:42;
%plot(j,formant(:,j),'*')


ftest=formant;
%---formreorg----

reorg=formant;



for i=1:42
 if reorg(1,i)>1
    reorg(3,i)=reorg(2,i);
    reorg(2,i)=reorg(1,i);
    reorg(1,i)=NaN;
 end
end
 a=find(reorg(1,:)>.0000001);
 A=reorg(1,a);
 m=mean(A');
 clear a A

for i=1:42
 if reorg(1,i)>2*m
    reorg(3,i)=reorg(2,i);
    reorg(2,i)=reorg(1,i);
    reorg(1,i)=NaN;
 end

 if reorg(2,i)>2.5
    reorg(3,i)=reorg(2,i);
    reorg(2,i)=NaN;
 end
 if reorg(3,i)<2.5
    reorg(3,i)=NaN;
 end
end
formant=reorg;

MOD=mod;
%--checks versus the models---%
for w=1:5
 mod=squeeze(MOD(w,:,:));
 dif=formant-mod;
 for k=1:2
  a=find(dif(k,:)>.0000001);
  A=dif(k,a);
  e(k)=sum(A.^2)/length(A);
  clear a A
 end

 E(w)=sum(e);
 clear e
end

[a,b]=min(E);