function DV = alphaPlanesKMIP(T2FS, numPlanes);

% DV = alphaPlanesKMIP(T2FS, numPlanes);
% defuzzifies a type-2 fuzzy set to give an x-value
% firstly slices the type-2 fuzzy set into numPlanes alpha-planes,
% secondly applies the KMIP to each alpha-plane to give an interval,
% thirdly assembles the intervals into a type-1 set (TRS)'
% and fourthly defuzzifies the type-1 set to give the defuzzified value of
% the original type-2 set.
%
% SG 041209

degreeDisc = 1/(numPlanes-1); % degree of discretisation of z-axis
sizeT2FS = size(T2FS);
domainSize = sizeT2FS(2); % number of x slices (columns)
coDomainSize = sizeT2FS(1); % number of y slices (rows)

resultsPairList = []; % initialisation of TRS

xSlices = linspace(1,domainSize,domainSize); % initialise x-axis
xVector = linspace(0,1,domainSize); % initialise x-axis

nullES = zeros(1,domainSize)+0.0001; % initialise x-axis
totalPlanes = 0; % initialise total of planes defuzzified
totalDV = 0; % initialise total of planes' defuzzified values

for planesCounter = 1:numPlanes
    planeHeight = (planesCounter-1)*degreeDisc; % height of alpha-plane
      
    lowerES = zeros(1, domainSize); % initialisation of a list of y co-ords defining the ES of the lower MF
    upperES = zeros(1, domainSize); % initialisation of a list of y co-ords defining the ES of the upper MF
    
    for columnCounter = 1:domainSize % work through all the columns of the type-2 fuzzy set
        for rowCounter = 1:coDomainSize % work through all the rows of the type-2 fuzzy set
            if T2FS(rowCounter,columnCounter) >= 0.0001
                if T2FS(rowCounter,columnCounter) >= planeHeight
                    lowerES(columnCounter) = rowCounter; % y co-ord of lower MF
                    break
                end
            end
        end
    end
    % lowerES
    for columnCounter = 1:domainSize % work through all the columns of the type-2 fuzzy set
        for rowCounter = 1:coDomainSize % work through all the rows of the type-2 fuzzy set
            if T2FS(rowCounter,columnCounter) >= 0.0001
                if T2FS(rowCounter,columnCounter) >= planeHeight
                    upperES(columnCounter) = rowCounter; % y co-ord of upper MF
                end
            end
        end
    end
    % upperES    

    totalPlanes = totalPlanes+1;
    midGrades = (upperES + lowerES)/2;
    if midGrades < nullES
        break
    end

    initCentroid = defuzz(xVector,midGrades,'centroid');
    e = round(round(initCentroid*10)*domainSize/10); % should be round(initCentroid*domainSize)
                                                     % but this can lead to numerical errors    
    
    eOld = -e;
    eInit = e;

    while eOld ~= e
        eOld = e;
        for counter = 1:e
            workingGrades(counter) = upperES(counter);
        end
        for counter = e+1:domainSize
            workingGrades(counter) = lowerES(counter);
        end    
        workingCentroid = defuzz(xVector,workingGrades,'centroid');
        eLeft = floor(workingCentroid * domainSize);
    end
    left = workingCentroid;
    
    resultsPair = [left; planeHeight]; % pair of (x,z) co-ordinates
    resultsPairList = [resultsPairList resultsPair]; % matrix comprising list of (x,z) co-ordinates

    e = eInit;
    eOld = -e;
    while eOld ~= e
        eOld = e;
        for counter = 1:e-1
            workingGrades(counter) = lowerES(counter);
        end
        for counter = e:domainSize
            workingGrades(counter) = upperES(counter);
        end    
        workingCentroid = defuzz(xVector,workingGrades,'centroid');
        eRight = ceil(workingCentroid * domainSize);
    end
    right = workingCentroid;
    
    resultsPair = [right; planeHeight]; % pair of (x,z) co-ordinates
    resultsPairList = [resultsPairList resultsPair]; % matrix comprising list of (x,z) co-ordinates
end

DV = defuzz(resultsPairList(1,:), resultsPairList(2,:), 'centroid'); % defuzzify the type-1 fuzzy set 

fprintf('\n')
fprintf('Number of alpha-planes: %0.0f \n', numPlanes);
fprintf('Number of alpha-planes used: %0.0f \n', totalPlanes);
fprintf('Defuzzified value: %0.10f \n', DV);
