%Feature extraction for files with 5000 points

function [Fault,f,y]=calculo_m(Featurefile)
%file could be bearing, gearmesh, imbalance, misalignment or resonance
M=zeros(1000,5);
for i=1:5
    M(:,i)=Featurefile((1+1000*(i-1)):(i*1000),1);
end
%Normalised M, each column of N is xi
N=zeros(1000,5);
for j=1:5
    N(:,j)=M(:,j)-mean(M(:,j));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Feature 1: the Root Mean Square(RMS) of the signal
%PSD of each column of N
y=zeros(129,5);
f=zeros(129,5);
for k=1:5
    [y(:,k),f(:,k)]=pwelch(N(:,k),[],[],[],1000);
end
ONE=zeros(5,1);
for i=1:5
    ONE(i,1)=norm(y(:,i))/sqrt(1000);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Feature 2: RMS of low frequencies 0-50 Hz
%each column of N is filtered
J=zeros(1000,5);
[B,A]=butter(11,0.1);
for j=1:5
    J(:,j)=filter(B,A,N(:,j));
end
%PSD of each column of J 
y2=zeros(129,5);
f2=zeros(129,5);
for k=1:5
    [y2(:,k),f2(:,k)]=pwelch(J(:,k),[],[],[],1000);
end    
TWO=zeros(5,1);
for i=1:5
    TWO(i,1)=norm(y2(:,i))/sqrt(1000);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Feature 3: RMS of middle frequencies 50-200 Hz
%each column of N is filtered
J=zeros(1000,5);
[B,A]=butter(13,[0.1 0.4]);
for j=1:5
    J(:,j)=filter(B,A,N(:,j));
end
%PSD of each column of J    
y3=zeros(129,5);
f3=zeros(129,5);
for k=1:5
    [y3(:,k),f3(:,k)]=pwelch(J(:,k),[],[],[],1000);
end    
THREE=zeros(5,1);
for i=1:5
    THREE(i,1)=norm(y3(:,i))/sqrt(1000);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Feature 4: RMS of high frequencies 200-500 Hz
%each column of N is filtered
J=zeros(1000,5);
[B,A]=butter(18,0.4,'high');
for j=1:5
    J(:,j)=filter(B,A,N(:,j));
end
%PSD of each column of J
y4=zeros(129,5);
f4=zeros(129,5);
for k=1:5
    [y4(:,k),f4(:,k)]=pwelch(J(:,k),[],[],[],1000);
end    
FOUR=zeros(5,1);
for i=1:5
    FOUR(i,1)=norm(y4(:,i))/sqrt(1000);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fault=zeros(5,4);
for m=1:5
    Fault(m,1)=ONE(m,1);
    Fault(m,2)=TWO(m,1);
    Fault(m,3)=THREE(m,1);
    Fault(m,4)=FOUR(m,1);
end
