function N0=amdfnorm(x,M_min,M_max,Fe,fig);
%function N0=amdfnorm(x,M_min,M_max,Fe,fig);
%x est la portion de signal analyser (une trame)
%M_min est le nb de points correspondant la priode pitch minimale (Fe/F0max)
%M_max est le nb de points correspondant la priode pitch maximale (Fe/F0min)
%fig prcise le numro de la figure sur laquelle afficher les rsultats
%si fig=0, il n'y a pas d'affichage
%la fonction retourne N0=0 si le signal est jug non vois
%elle retourne N0=nombre de points d'une priode pitch dans le cas vois
min_seuil=.5;%(<1 ,suggestion .4) est tel que si min(amdf)< min_seuil
% la portion est dclare voise et N0=argmin(amdf) dfinit le pitch: F0=Fe/N0
oct_seuil=.2;%(>0 ,suggestion .2) est tel que si amdf(N0/2)-amdf(N0)<oct_seuil
% on prend N0/2 pour dfinir le pitch: F0=Fe/(N0/2)
max_seuil=.3;%(>0 ,suggestion .3) est tel que si amdf(N0/2)-amdf(N0)<max_seuil
% ou amdf(3N0/2)-amdf(N0)<max_seuil
% alors la portion est dclare non voise (F0=0)
[FRAME,L]=size(x);
for dec=M_min:M_max
amdf(dec+1-M_min)=mean(abs(x(1:FRAME-dec+1)-x(dec:FRAME)));
end
amdf=amdf'/mean(abs(x(1:FRAME-dec+1))+abs(x(dec:FRAME)));
[amdfmin,N0]=min(amdf);
if (amdfmin >min_seuil)
N0=inf;
else N0=N0+M_min-1;
end
%test erreur d'octave
N02=round(N0/2);
if(M_min<=N02 & N02<inf)%check if the min doesn't
%corresponds to twice the pitch period
if((amdf(N02+1-M_min)-amdfmin)<oct_seuil)
N0=N02;
disp('rectification octave')
end
end
%test profondeur du min par rapport au max environnant
N1= round(3*N0/2);
N2=round(N0/2);
if(N1 < M_max) amN1=amdf(N1+1-M_min);else amN1=amdfmin;end
if(N2 > M_min & N2 <inf) amN2=amdf(N2+1-M_min);else amN2=amdfmin;end
maxi_aroundN0=max(amN1,amN2);
if (maxi_aroundN0-amdfmin < max_seuil) N0=inf;end
f0= Fe/N0;
if fig
figure(fig)
subplot(211);plot(x);legend ('signal temporel original');subplot(212);plot(M_min:M_max,amdf);legend(['trac de l''AMDF : F0 = ',num2str(f0),' Hz']);
end