%% Use a Customized Power Spectrogram to Compute the Conditional Spectral Moment
% By default, tfsmoment calls the function pspectrum internally to generate the power spectrogram that tfsmoment uses for the moment computation.  You can also import an existing power spectrogram for tfsmoment 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 been generated with customized options. Compare the resulting spectral-moment histogram with one that tfsmoment generates using its pspectrum default options.
%
% Load the data, which includes two power spectrums and the associated frequency and time vectors.
% The p_inner1_def spectrum was created using the default pspectrum options.  It is equivalent to what tfsmoment computes internally when an input spectrum is not provided in the syntax.
% The p_inner1_MinThr spectrum was created using the MinThreshold pspectrum option.  This option puts a lower bound on nonzero values to screen out low-level noise.  For this example, the threshold was set to screen out noise below the 0.5% level.

load tfmoment_data.mat p_inner1_def f_p_def t_p_def ...
    p_inner1_MinThr f_p_MinThr t_p_MinThr
load tfmoment_data.mat x_inner1 x_baseline1

% Determine the second spectral moments (variance) for both cases.

moment_p_def = tfsmoment(p_inner1_def,f_p_def,t_p_def,2);
moment_p_MinThr = tfsmoment(p_inner1_MinThr,f_p_MinThr,t_p_MinThr,2);

%
% Plot the histograms together.

figure
histogram(moment_p_def);
hold on
histogram(moment_p_MinThr);
hold off
legend('Moment from Default P','Moment from Customized P')
title('Second Spectral Moment for Inner1 from Input Spectrograms')

%
% The histograms have the same overall spread, but the thresholded moment histogram has a higher peak bin at a lower moment magnitude level than the default moment.  This example is for illustration purposes only, but does show the impact that preprocessing in the spectrum computation stage can have.
% Copyright 2018 The MathWorks, Inc.