www.pudn.com > CELP.ZIP > pdetect.m


function [pcode, pgain, pk]=pdetect(pdelay,sube1,bkl,bkh,fracflg) 
 
global h len cbsize szsubf pcbsize fstfrm intp pdmin pdmax pdlyindx 
 
if fracflg==1 
    % Fractional delay search 
    start=intp+pdmin;                                        % Start point for computing fractional delay vector 
    c=bkh-bkl; i=0; 
    while (i<=c) 
        m=fix(pdlyindx(bkl+i)); 
        frac=pdlyindx(bkl+i)-m; 
        if frac>10e-6 
            [pdelay,fracdly(i+1,:)]=interpolate(pdelay,start,szsubf,frac,m);      % All interpolate.m all equal either 1 or 0 (avoid FirstCom flag getting error) 
             
            for j=0:szsubf-1                                                        %Convolution of the fractional delays with weighting filter 
                endpt=min(j,len-1); 
                y(i+1,j+1)=sum(h(1:endpt+1).*fracdly(i+1,1+j:-1:1+j-endpt)); 
            end 
            yy(i+1,:)=y(i+1,:); 
         
            cor(i+1)=sum(yy(i+1,:).*sube1); 
            eng(i+1)=sum(yy(i+1,:).*yy(i+1,:)); 
            gain(i+1)=cor(i+1)/eng(i+1); 
            peak(i+1)=gain(i+1)*cor(i+1); 
        else 
            peak(i+1)=-10e5; 
            gain(i+1)=0.0; 
        end 
        i=i+1; 
    end 
 
    [pk,pcode]=max(peak);           % Find the best estimate of fractional delay and gain 
    pgain=gain(pcode); 
    pcode=bkl+pcode-1; 
     
else 
     
    % Integer delay search 
    strptr=pdmax+1-fix(pdlyindx(bkh));             % Find computation start point in pitch delay buffer 
    endptr=pdmax+1-ceil(pdlyindx(bkl));            % Find computation end point in pitch delay buffer 
     
    for i=0:szsubf-1 
        endpt=min(i,len-1);                                                         %compute conv before interpolation 
        y(endptr,i+1)=sum(h(1:endpt+1).*pdelay(endptr+i:-1:i-endpt+endptr)); 
    end 
     
    for i=endptr-1:-1:strptr 
        y(i,1)=h(1)*pdelay(i);                                       % End point correction 
        y(i,2:len)=y(i+1,1:len-1)+h(2:len)*pdelay(i); 
        y(i,len+1:szsubf)=y(i+1,len:szsubf-1); 
    end 
 
    for i=strptr:endptr 
        yy(i,:)=y(i,:); 
        m=intp+pdmin-i;                                          % M=pitch delay 
        if m