%celp.m
function [synsp,codebook,ICN,LPC,CN,G1,G2,DELAY]=celp(S,FS);
%ORIS-Orignial speech signal
%synsp-Synthesized speech signal
%ICN-Initial code number
framelength= round(FS*30*10^(-3));
exitationlength=round(FS*7.5*10^(-3));
%Generating the code book
r=rand(1,1082);
codebook=[];
for i=1:1:1082
if(r(i)<=0.1)
codebook=[codebook -1];
elseif ((0.1<r(i))&(r(i)<0.9))
codebook=[codebook 0];
else
codebook=[codebook 1];
end
end
CN=[];
ICN=[];
G1=[];
G2=[];
DELAY=[];
synsp=[];
ORIS=[];
for frame=1:1:fix(length(S)/framelength)
S1=S((frame-1)*framelength+1:1:(frame-1)*framelength+framelength+1) ;
l=lpc(S1,10);
LPC{frame}=l;
l1=l(2:1:length(l));
l1=l1.*(0.994.^[1:1:(length(l)-1)]);
lmod1=[1 l1];
l2=l(2:1:length(l));
l2=l2.*(0.8.^[1:1:(length(l)-1)]);
lmod2=[1 l2];
%Usually frequency of the exitation is in the range of 54 Hz to 400 Hz.
%Calculated in terms of number of samples as the range from 32 to 232
ee=conv(S1,l);
col1=[];
for n=32:1:232
col2=[];
for k=1:1:n
temp=ee(k:1:length(ee));
m=fix(length(temp)/n);
if(m==1)
break
end
f=temp(1:1:m*n);
r=reshape(f,n,m);
error=0;
for p=1:1:size(r,2)-1
error=error+sum((r(:,p)-r(:,p+1)).^2)/((m-1)*n);
end
col2=[col2 error];

end
[s,t]=min(col2);
col1=[col1 s];
end
[u1,v1]=min(col1);
m=v1+31;
DELAY=[DELAY m];
%The exitation period is m/FS or exitation frequency is FS/m
r=rand;
ICN=[ICN round(r*(1082-exitationlength))+1];
e=codebook(round(r*(1082-exitationlength))+...
1:1:round(r*(1082-exitationlength))+exitationlength);
for exitation=1:1:4
%gain1=Rsm/Rs0
Rsm=sum([e zeros(1,m)].*[zeros(1,m) e]);
Rsm=Rsm/length([e zeros(1,m)]);
Rs0=sum(e.*e);
Rs0=Rs0/length(e);
gain1=(Rsm+eps)/Rs0;
G1=[G1 gain1];
%gain2 is computed
t=filter(l,lmod2,S1((exitation-1)*exitationlength+1:1:(exitation-1)*...
exitationlength+exitationlength));
q=filter(1,lmod2,gain1*[zeros(1,m) e]);
L1= length(q);
L2=length(t);
if(L1>=L2)
u=t-q(1:1:length(t));
else
u= t(1:1:length(q))-q;
end
error1=[];
for iteration=1:1:length(codebook)-exitationlength-1;
codeselection=codebook(iteration:1:iteration+exitationlength-1);
y=filter(1,lmod2,codeselection);
u=u(1:1:length(u));
gain2=sum(y.*u)/sum(y.^2);
G2=[G2 gain2];
temp1=[zeros(1,m) e];
temp1=temp1(1:1:exitationlength);
temp2=gain1*temp1+gain2*codeselection;
sp=filter(1,l,temp2);
z1=S1((exitation-1)*exitationlength+1:1:(exitation-1)*...
exitationlength+exitationlength)-sp(1:1:exitationlength);
e2=filter(l,lmod2,z1);
error1=[error1 sum(e2.^2)];
end
[a,b]=min(error1);
selectedcode=codebook(b:1:b+exitationlength-1);
y=filter(1,lmod2,selectedcode);
u=u(1:1:length(u));
gain2=sum(y.*u)/sum(y.^2);
temp1=[ zeros(1,m) e];
temp1=temp1(1:1:exitationlength);
temp2=gain1*temp1+gain2*codeselection;
sp=filter(1,l,temp2);
sp=sp(1:1:exitationlength);
synsp=[synsp sp];
ORIS=[ORIS S1((exitation-1)*exitationlength+1:1:(exitation-1)*...
exitationlength+exitationlength)];
e= temp2;
end
end
figure
subplot(2,1,1)
plot(ORIS)
hold on
subplot(2,1,2)
plot(synsp,'r')
j=1;
for i=1:1:9
r=round(rand*(length(synsp)-4*exitationlength-1));
subplot(3,3,j)
plot(ORIS(r:1:r+4*exitationlength))
hold on
plot(synsp(r:1:r+4*exitationlength),'r-')
j=j+1;
end