%% Use an Existing Power Spectrogram to Compute the Conditional Temporal Moment
% By default, tfsmoment calls the function pspectrum internally to generate the power spectrogram that tftmoment uses for the moment computation.  You can also import an existing power spectrogram for tftmoment to use instead.  This capability is useful if you already have a power spectrogram as a starting point, or if you want to customize the pspectrum options by generating the spectrogram explicitly first.
% Input a power spectrogram that has already been generated using default options. Compare the resulting temporal-moment plot with one that tftmoment generates using its own pspectrum default options.  The results should be the same.
%
% Load the data, which consists of simulated vibration measurements for a system with a fault that causes periodic resonances.  p is the previously computed spectrogram, fp and tp are the frequency and time vectors associated with p, x is the original vector of measurements,and  fs is the sampling frequency,.

load tftmoment_example p fp tp x fs

%
% Determine the second temporal moment using the spectrogram and its associated frequency and time vectors.  Plot the moment.

[momentT_p,f_p] = tftmoment(p,fp,tp,2);
figure
subplot(2,1,1)
plot(f_p,momentT_p)
title('Second Temporal Moment using Input Spectrogram ')

% Now find and plot the second temporal moments using the original data and sampling rate.

[momentT,f] = tftmoment(x,fs,2);
subplot(2,1,2)
plot(f,momentT)
title('Second Temporal Moment using Measurement Data')

% As expected, the plots match since the default pspectrum options were used for both.  This result demonstrates the equivalence between the two approaches when there is no customization.
% Copyright 2018 The MathWorks, Inc.