%% Find Endmembers Using Pixel Purity Index
% Read a hyperspectral data into the workspace.

hcube = hypercube('paviaU.hdr');

% Find the number of spectrally distinct endmembers present in the hyperspectral data cube by using countEndmembersHFC function.

numEndmembers = countEndmembersHFC(hcube,'PFA',10^-7);

% Compute the endmembers using the pixel purity index (PPI) method. By default, the ppi function uses maximum noise fraction (MNF) transform for preprocessing. The default number of skewers used for projection is $10^4$.

endmembers = ppi(hcube.DataCube,numEndmembers);

% Plot the endmembers of the hyperspectral data.

figure
plot(endmembers)
xlabel('Band Number')
ylabel('Pixel Values')
title({'Endmembers Spectra',['Number of Endmembers = ' num2str(numEndmembers)]});
ylim([0 9000])

% Copyright 2020 The MathWorks, Inc.