%This program imports data collected with crossbow sensors and with
%wireless sensors and tries to match them.

%IMPORTANT: the data from wireless sensors have to be stored as:
% Sensor 8 - Bearing in Files 1
% Sensor 7 - Gearmesh in Files 2

function processing_ojo

%CROSSBOW
%For both failures: Load the data, normalise them and calculate max and min.

%%%GEARMESH
load('C:\Documents and Settings\uos\Desktop\programs\Matlab\Crossbow-Wireless\g1_6.mat');

mg1=mean(g1);
vg1=var(g1);
g1_N=zeros(5000,1);
g1_N=g1-mg1;
g1_N=g1_N/vg1;

g1_N_max=max(g1_N)
g1_N_min=min(g1_N)
r1=g1_N_max-g1_N_min


%%%BEARING
load('C:\Documents and Settings\uos\Desktop\programs\Matlab\Crossbow-Wireless\y1_6.mat');
mb1=mean(y1);
vb1=var(y1);
b1_N=zeros(5000,1);
b1_N=y1-mb1;
b1_N=b1_N/vb1;

b1_N_max=max(b1_N)
b1_N_min=min(b1_N)
r2=b1_N_max-b1_N_min

%WIRELESS
%For both failures: Load the data, look at the length of the files and ask
%the user from which value he wants to extract 5000 points to compare,
%normalise the data and calculate max and min

nsensors = 2;

for i=1:nsensors

fileXYZ=sprintf('XYZ_%d.txt',i);
eval(['load ',fileXYZ]);
tam=size(fileXYZ); tf=tam(2);
fileXYZ=fileXYZ(1:tf-4);
f1=eval(fileXYZ); 
tam=size(f1); maxv=tam(2);

%vertical
data=f1(:,2);
length(data)
first=input('First value:');
dataW=zeros(5000,1);
dataW=data(first:(first+5000),1);

mW=mean(dataW);
vW=var(dataW);
dataW=dataW-mW;
dataW=dataW/vW;

dataW_max=max(dataW)
dataW_min=min(dataW)
r=dataW_max-dataW_min

%For both failures: The collected data from crossbow sensors are scaled and
%then the data of each kind of sensor are plotted.

figure
plot(dataW);grid;
xlabel('Time msec');

%i=1 -> bearing
if i==1
    b1_N=b1_N*(r/r2);
    b1_N_max=max(b1_N)
    b1_N_min=min(b1_N)
    hold on;
    plot(b1_N,'g');
    title('Bearing')
    Legend('wireless','crossbow')
    
    %The function "calculo_mPSD" is called to carry out the feature
    %extraction
    [Fault_b_crossbow,f_b_crossbow,y_b_crossbow]=calculo_mPSD(b1_N);
    [Fault_W1,f_W1,y_W1]=calculo_mPSD(dataW);
else
    %i=2 -> gearmesh
    if i==2
        g1_N=g1_N*(r/r1);
        g1_N_max=max(g1_N)
        g1_N_min=min(g1_N)
        hold on;
        plot(g1_N,'g')
        title('Gearmesh')
        Legend('wireless','crossbow')
        
        %The function "calculo_mPSD" is called to carry out the feature
        %extraction
        [Fault_g_crossbow,f_g_crossbow,y_g_crossbow]=calculo_mPSD(g1_N);
        [Fault_W2,f_W2,y_W2]=calculo_mPSD(dataW);
    end
end

end

%PRINCIPAL COMPONENTS ANALYSIS

%CROSSBOW-BEARING
%To calculate the first 2 principal components of Fault and use them in the visualisation 
c=corrcoef(Fault_b_crossbow); % Calculates a correlation coefficient matrix c of Fault
[v,d]=eig(c); % Find the eigvectors v and the eigenvalues d of Fault

T=[v(:,end)';v(:,end-1)']; % Create the transformation matrix T from the first two principal components
z=T*Fault_b_crossbow'; % Create a 2-dimensional feature vector z
figure
grid;hold on;
plot(z(1,1:5),z(2,1:5),'bo'); % Scatter plot of the 2-dimensional features

%CROSSBOW-GEARMESH
c=corrcoef(Fault_g_crossbow); % Calculates a correlation coefficient matrix c of Fault
[v,d]=eig(c); % Find the eigvectors v and the eigenvalues d of Fault

T=[v(:,end)';v(:,end-1)']; % Create the transformation matrix T from the first two principal components
z=T*Fault_g_crossbow'; % Create a 2-dimensional feature vector z
hold on;
plot(z(1,1:5),z(2,1:5),'g*'); % Scatter plot of the 2-dimensional features

%WIRELESS-BEARING
c=corrcoef(Fault_W1); % Calculates a correlation coefficient matrix c of Fault
[v,d]=eig(c); % Find the eigvectors v and the eigenvalues d of Fault

T=[v(:,end)';v(:,end-1)']; % Create the transformation matrix T from the first two principal components
z=T*Fault_W1'; % Create a 2-dimensional feature vector z
grid;hold on;
plot(z(1,1:5),z(2,1:5),'kd'); % Scatter plot of the 2-dimensional features

%WIRELESS-GEARMESH
c=corrcoef(Fault_W2); % Calculates a correlation coefficient matrix c of Fault
[v,d]=eig(c); % Find the eigvectors v and the eigenvalues d of Fault

T=[v(:,end)';v(:,end-1)']; % Create the transformation matrix T from the first two principal components
z=T*Fault_W2'; % Create a 2-dimensional feature vector z
grid;hold on;
plot(z(1,1:5),z(2,1:5),'r+'); % Scatter plot of the 2-dimensional features

legend('bearing crossbow','gearmesh crossbow', 'bearing wireless','gearmesh wireless' )

%PSDs
figure;
grid;hold on;
plot(f_b_crossbow,y_b_crossbow,'b');
hold on;
plot(f_g_crossbow,y_g_crossbow,'g');
hold on;
plot(f_W1,y_W1,'k');
hold on;
plot(f_W2,y_W2,'r');

figure;grid;hold on;
plot(f_b_crossbow,y_b_crossbow,'b',f_W1,y_W1,'k');title('BEARING');

figure;grid;hold on;
plot(f_g_crossbow,y_g_crossbow,'g',f_W2,y_W2,'r');title('GEARMESH');

