Начало | Новости

Вернуться в раздел: Science


pcademo(A)


function pcademo(A)
% pcademo(A)
% Interactive PCA plot. A should be a set of signals to analyze
% (time dimention should run down, sample number – to the right).
% This code is good for demonstration purposes, as it helps to explain
% the principles of PCA, and the key to components interpretation.
% I find it useful for daily analysis of my signals though.

% Rev 03 Sep 2010

[S.cPca,S.signalsPca,S.eigenvalues] = princomp(A);

hF = figure;
set(hF,'WindowButtonMotionFcn',@hoverfunction,'ResizeFcn',@resizecall);
S.hA1 = subplot(1,2,1); % Axes for the signal
S.hPlot = plot(mean(A,2),'-'); % Average across samples
ylim([min(A(:)) max(A(:))]);
xlim([1 size(A,1)]);
title('Signal');

S.hA2 = subplot(1,2,2); % Axes for the plot of component coefficients
title('PCA coefficients');

S.text1 = uicontrol('Style','text','String','X data:');
S.text2 = uicontrol('Style','text','String','Y data:');
S.pop1 = uicontrol('Style','popup','String','1|2|3|4|5','Value',1,'Callback',@uicall);
S.pop2 = uicontrol('Style','popup','String','1|2|3|4|5','Value',2,'Callback',@uicall);

set(hF,'UserData',S); % Send data

figPos = get(hF,'Position'); % Figure size in pixels
set(hF,'Position',[figPos(1)-figPos(3)*0.25 figPos(2) figPos(3)*1.5 figPos(4)]);

resizecall; % Arrange things neatly
uicall;

end

function hoverfunction(h,event)
hF = gcf; S = get(hF,'UserData'); % Get data

xy = get(hF,'CurrentPoint'); % Coordinates within a figure window, in pixels
axesPos = get(S.hA2,'Position'); % Position in relative units (for axes it seems to be like that by default)
figPos = get(hF,'Position'); % Figure size in pixels
x = (xy(1)/figPos(3)-axesPos(1))/axesPos(3); % Relative [0 1] coordinates within axes2
y = (xy(2)/figPos(4)-axesPos(2))/axesPos(4);
x = min([1 max([0 x])]); y = min([1 max([0 y])]); % Limit
xLim = get(S.hA2,'XLim'); yLim = get(S.hA2,'YLim');
x = xLim(1)+x*(xLim(2)-xLim(1)); % Now let's move to "real" coordinates within axes2
y = yLim(1)+y*(yLim(2)-yLim(1));
%fprintf('%f\t%f\n',x,y);

c1 = get(S.pop1,'Value'); c2 = get(S.pop2,'Value'); % Components to show
if((c1~=1)&(c1~=1)) % If working with higher components, it worth adding average lower components to make the signal more meaningful
yV = S.signalsPca(:,1)*mean(S.cPca(:,1));
else
yV = zeros(length(S.signalsPca(:,1)),1);
end

yV = yV + S.signalsPca(:,[c1 c2])*[x y]';
set(S.hPlot,'YData',yV);
end

function uicall(h,event)
hF = gcf; S = get(hF,'UserData'); % Get data

axes(S.hA2);
plot(S.cPca(:,get(S.pop1,'Value')),S.cPca(:,get(S.pop2,'Value')),'.');
xlabel(['Component ' num2str(get(S.pop1,'Value'))]);
ylabel(['Component ' num2str(get(S.pop2,'Value'))]);

set(hF,'UserData',S); % Send data
end

function resizecall(h,event)
hF = gcf; S = get(hF,'UserData'); % Get data

figPos = get(hF,'Position');
set(S.hA1,'Position',[0.1 0.1+20/figPos(4) 0.35 0.8-20/figPos(4)]);
set(S.hA2,'Position',[0.6 0.1+20/figPos(4) 0.35 0.8-20/figPos(4)]);
w = 20;
set(S.text1,'Position',[w 5 50 20]); w = w+50+5;
set(S.pop1,'Position',[w 5 50 20]); w = w+50+5;
set(S.text2,'Position',[w 5 50 20]); w = w+50+5;
set(S.pop2,'Position',[w 5 50 20]); w = w+50+5;

set(hF,'UserData',S); % Send data
end

Created: 2010.09.07, 20:20
Visits: 631 , LastTime: 2017-07-21 14:33:21


Некоторые права защищены (о) by Арсений Хахалин
Some rights reserved by Arseny Khakhalin
(or Arseni Khakhaline in another transcription)

Пишите...