I have a spectrum (wavelength(x) versus absorption(y)) which is a mix of two signals (xa,ya) and (xb,yb). I am trying to use PCA (code I found online) to unmix the signals in (x,y):
%step 1, input data
numdata=length(data(:,1));
x=data(:,1);
y=data(:,1);
%step 2, finding a mean and subtracting
xmean=mean(x);
ymean=mean(y);
xnew=x-xmean*ones(numdata,1);
ynew=y-ymean*ones(numdata,1);
subplot(3,1,1);
plot(x,y, 'o');
title('Original Data');
%step 3, covariance matrix
covariancematrix=cov(xnew,ynew);
%step 4, Finding Eigenvectors
[V,D] = eig(covariancematrix);
D=diag(D);
maxeigval=V(:,find(D==max(D)));
%step 5, Deriving the new data set
%finding the projection onto the eigenvectors
finaldata=maxeigval'*[xnew,ynew]';
subplot(3,1,2);
stem(finaldata, 'DisplayName', 'finaldata', 'YDataSource', 'finaldata');
title('PCA 1D output ')
%we do a classification now
subplot(3,1,3);
title('Final Classification')
hold on
for i=1:size(finaldata,2)
if finaldata(i)>=0
plot(x(i),y(i),'o')
plot(x(i),y(i),'r*')
else
plot(x(i),y(i),'o')
plot(x(i),y(i),'g*')
end
end
How best to apply the PCA output to unmix (y) into components ya and yb? I don't have experience with PCA and cannot find any good tutorials online for this application. Is it best to generate the eigenvectors for training spectrum and then compare with test spectrum? Thanks