%% Find and Visualize the Difference Signal of a Compound TSA Signal
% 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. To obtain the signal containing just the meshing components for gears 5 and 6, filter out the components of the shaft rotation, gears 1 and 2 and, 3 and 4 by specifying their gear ratios of 17 and 51 in orderList. The signal components corresponding to the shaft rotation (order = 1) is always implicitly included in the computation.
%

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.

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*sin(2*pi*10*f(1)*t);                          % gear mesh vibration for gears 5 and 6

%
% Compute the difference signal of the TSA signal using the sample time, rpm, and the mesh orders to be filtered out.

Y = tsadifference(X,t,rpm,orderList);

% The output Y is a vector containing the gear mesh signal and harmonics for gears 5 and 6.
%
% Visualize the difference signal, the raw TSA signal, and their amplitude spectrum on a plot.

tsadifference(X,fs,rpm,orderList)

% From the amplitude spectrum plot, observe the following components:
% The filtered component at the 17th order and its harmonic at the 34th order
% The second filtered component at the 51st order and its harmonic at the 102nd order
% The residual mesh components for gears 5 and 6 at the 10th order
% The filtered shaft component at the 1st and 2nd orders
% The amplitudes on the spectrum plot match the amplitudes of individual signals
% Copyright 2018 The MathWorks, Inc.
%
%