function [CA, CAl, CAr]=centroid(A)

left=min(A(1:8)); % the range
right=max(A(1:8));
dx=(right-left)/500;  % discretization

for j=1:501
    x=left+(j-1)*dx;
    
    if x<=A(1)| x>=A(4);
        upperA(j)=0;
    elseif x>A(1) & x<A(2);
        upperA(j)=(x-A(1))/(A(2)-A(1));
    elseif x>=A(2) & x<=A(3);
        upperA(j)=1;
    else 
        upperA(j)=(A(4)-x)/(A(4)-A(3));
    end
    
    if x<=A(5) | x>=A(8);
        lowerA(j)=0;
    elseif x<A(6)
        lowerA(j)=A(9)*(x-A(5))/(A(6)-A(5));
    elseif x<A(7)
        lowerA(j)=A(9);
    else
        lowerA(j)=A(9)*(A(8)-x)/(A(8)-A(7));
    end
end
%% centroid of A
[CAl,CAr]=cos_tr(left+[0:500]*dx,zeros(1,501),(upperA+lowerA)/2,(upperA-lowerA)/2);
Codebook_Centroid=[CAl, CAr];
CA=(CAl+CAr)/2;
Codebook_MeanOfCentroild=[CA];
