
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% General description:
%%
%% This function process JCSD spectrum library. 
%%
%% There are 3 major tasks
%% 1. Choose spectrum range of interest
%% 2. Baseline correction
%% 3. Library partitioning 
%%
%% Written by: Wei Wang
%% 8/27/2007
%%
%% Copyright(c) 2007 by
%% Machine Learning for Signal Processing Laboratory
%% University of Maryland, Baltimore County
%%
%% Permission to use, copy, modify, and distribute this software for any
%% purpose without fee is hereby granted, provided that this entire notice
%% is included in all copies of any software which is or includes a copy
%% or modification of this software and in all copies of the supporting
%% documentation for such software.
%% THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
%% WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR DOES NOT MAKE ANY
%% REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
%% OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
close all;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   1. Load spectrum library                                  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('./JCSDLib.mat');   %load in 'lbryMtrx'

wavel=data(1).x;
threshold=0.9; % thereshold for library partitioning

rangeleft=500;
rangeright=3200;

irange=find(wavel>rangeleft & wavel< rangeright);

x=wavel(irange);

for i = 1:length(data)
    if length(data(i).y)~=1024    % interpolate for diff resolutions   
       xt=data(i).x;
       yt=data(i).y;
       if xt(1) < x(1) % 'x' is decreasing 
           xt=[x(1);xt];
           yt=[0;yt];
       end       
       if xt(end) > x(end) % 'x' is decreasing 
           xt=[xt;x(end)];
           yt=[yt;0];
       end
       libsall(:,i)=interp1(xt,yt,x);
    else        
       libsall(:,i)=data(i).y(irange);
    end
end


JCSDAgentMaxID=30; % - the largest index of agent in JCSD spectrum library.
%                      Above this number are spectra of surface or
%                      intefernent materials, which should not be included
%                      in clusters.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   2. Processing library in broad spectrum [300 - 3500]      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[clusters, list_corr_pairs]=...
    JCSD_LibraryPartitioning(libsall, threshold, JCSDAgentMaxID);
libs=libsall;
save JCSDLibs_BroadSpectrum.mat libs x clusters list_corr_pairs;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   3. Processing library in broad spectrum [300 - 3500] after %%
%%      baselie correction                                      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n,m]=size(libsall);
for i=1:m    
    [locs, amps, yy, y_smoothed]=extracting_peaks(libsall(:,i)',x);         
    yy=yy/norm(yy);
    libs(:,i)=yy';
        
    PeakLocs(i,1:length(locs))=locs;
end
[clusters, list_corr_pairs]=...
    JCSD_LibraryPartitioning(libs, threshold, JCSDAgentMaxID);
save JCSDLibs_BaselineCorrected_BroadSpectrum.mat libs PeakLocs x clusters list_corr_pairs;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   4. Processing library in broad spectrum [500 - 1700]      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rangeleft=500;
rangeright=1700;

irange=find(x > rangeleft & x < rangeright);

x=x(irange);
libsall=libsall(irange,:);

[clusters, list_corr_pairs]=...
    JCSD_LibraryPartitioning(libsall, threshold, JCSDAgentMaxID);
libs=libsall;
save JCSDLibs_NarrowSpectrum.mat libs x clusters list_corr_pairs;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%   5. Processing library in broad spectrum [500 - 1700] after %%
%%      baselie correction                                      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n,m]=size(libsall);
for i=1:m    
    [locs, amps, yy, y_smoothed]=extracting_peaks(libsall(:,i)',x);         
    yy=yy/norm(yy);
    libs(:,i)=yy';
end
[clusters, list_corr_pairs]=...
    JCSD_LibraryPartitioning(libs, threshold, JCSDAgentMaxID);
save JCSDLibs_BaselineCorrected_NarrowSpectrum.mat libs x clusters list_corr_pairs;

