%% Extract Gear Condition Monitoring Metrics from Vibration Signals
% Consider a drivetrain with six gears driven by a motor that is fitted with a vibration sensor, as depicted in the figure below. Gear 1 on the motor shaft meshes with gear 2 with a gear ratio of 17:1. The final gear ratio, that is, the ratio between gears 1 and 2 and gears 3 and 4, is 51:1. Gear 5, also on the motor shaft, meshes with gear 6 with a gear ratio of 10:1. The motor is spinning at 180 RPM, and the sampling rate of the vibration sensor is 50 kHz.
% Create the dataset.

rpm = 180;                                          
fs = 50e3;                                          
t = (0:1/fs:(1/3)-1/fs)'; % sample times
orderList = [17 51];                                
f = rpm/60*[1 orderList 10];

% In practice, you would use measured data such as vibration signals obtained from an accelerometer. For this example, generate TSA signal X, which is the simulated data from the vibration sensor mounted on the motor, and then compute the difference, regular, and residual signals. Store the signals in a preallocated table.

T = table('Size',[10 4],'VariableTypes',{'cell','cell','cell','cell'},'VariableNames',{'TSA','Diff','Reg','Res'});
for k = 1:10
    X = sin(2*pi*f(1)*t) + sin(2*pi*2*f(1)*t) + ... % motor shaft rotation and harmonic
    3*sin(2*pi*f(2)*t) + 3*sin(2*pi*2*f(2)*t) + ... % gear mesh vibration and harmonic for gears 1 and 2
    4*sin(2*pi*f(3)*t) + 4*sin(2*pi*2*f(3)*t) + ... % gear mesh vibration and harmonic for gears 3 and 4
    2*(k/6)*sin(2*pi*10*f(1)*t) + randn(size(t))/5;    % gear mesh vibration for gears 5 and 6 and noise
  res = tsaresidual(X, fs, rpm, orderList);
  dif = tsadifference(X, fs, rpm, orderList);
  reg = tsaregular(X, fs, rpm, orderList);
  
  T(k,'TSA') = {X};
  T(k,'Diff') = {dif};
  T(k,'Reg') = {reg};
  T(k,'Res') = {res};
end
T

% T is a 10x4 table, where each element is a cell array.
%
% Compute the gear condition monitoring metrics using the dataset in table T.

[gearMetrics1,info1] = gearConditionMetrics(T,'SignalVariable','TSA','DifferenceVariable','Diff','RegularVariable','Reg','ResidualVariable','Res')

% Observe that the gear metrics are changing due to fault in gear mesh between gears 5 and 6. The NA4 value is highly sensitive to the fault and its propagation as it significantly increases in value over the different data sets.
% info1 contains information about variables that were used to compute the metrics.
% Alternatively, you can also compute the metrics using following syntax.

[gearMetrics2,info2] = gearConditionMetrics(T,'TSA','Diff','Reg','Res')

% Copyright 2018 The MathWorks, Inc.
%
%
%
%