From ControlTheoryPro.com

1 Introduction to psdData Class Methods
In Stochastic Controls Power Spectral Densities (PSDs) are often used to represent or define disturbance and noise environments. However, there are several implementation issues that must be dealt with when using PSDs.
The basics of a PSD are as follows:
 The PSD is represented in MATLAB by a vector of frequencies and magnitudes at those frequencies.
 PSDs typically (but not always) have units of magnitude squared (^2) over Hz.
 The primary metric used to compare one PSD to another is the Root Mean Square (RMS).
 As with most metrics the RMS does not tell you everything.
 Particularly important is where in the frequency range does most of the PSD energy resides. More specifically, "Is the energy in the low, mid, or high frequency range by comparison to your designed rejection and performance requirements?"
Some of the PSD implementation issues are:
 PSD measurements are often very coarse particularly in the low frequencies. The result of this is that low frequency measurements might be at 0.01 Hz, 0.1 Hz, 1 Hz, 2 Hz, 5 Hz, 10 Hz, ... For noise PSDs this is not usually an issue. However, disturbance PSDs are often dominated by low frequency content. The RMS is an integration under the curve and coarse measurements often lead to inaccurate RMS calculations.
 Addition and subtraction of PSDs must occur at the same frequency and this often dictates the use of an interpolated PSD.
 Measured data comes in the form that is easiest to capture with the least noise. This often leads to measurements in rate (from good MEMS gyros) or acceleration (from good MEMS accelerometers). While the measurements are in rate or acceleration the disturbances that need to be in any simulation may need to be integrated to a position PSD.
Methods for handling implementation issues:
 Issue: Coarse PSD > Solution: interpolation method
 Issue: Addition & Subtraction > Solution: overloaded plus & minus methods
 Issue: Measured Data in the wrong units > Solution: integrate and differentiate methods
2 psdData Class Methods
The psdData Class object uses 2 types of methods attributes:
 Public: Methods the user can call
 Private: Methods only other psdData methods can call
Private methods are only available to other methods and are not discussed.
2.1 Display of the psdData Class Methods
Let's define a hypothetical psdData class object:
>> a = psdData('Test PSD', time, data, 'type', [], 'interpolate', 5000);
Then the displayed Methods and values are: >> methods(a) Methods for class psdData: RMS integrate plot psdData differentiate interpolate plotTime setUnits getUnits minus plus
2.1.1 psdData Method: interpolate
One of the biggest problems with using PSDs is where (what frequencies) the PSD was measured or calaculated. This is an issue for RMS calculation, addition and subtraction, and frequency domain modeling. The interpolate method is probably the single most useful method in this class.
The interpolate method allows the psdData class to calculate the PSD magnitude at a finer resolution or at any user provided frequencies.
[obj] = interpolate(obj, numPnts, freqs)
Input Arguments:
 psdData Class object
 Number of Points for interpolation (Optional, defaults to MinNumPnts property)
 Frequency vectors (Optional, allows user to provide a vector of frequency points or end points)
Outputs:
 psdData Class object
Examples:
>> b = interpolate(a); >> b = interpolate(a, 2000); >> b = interpolate(a, 10000, [0.1, 1E4]); >> b = interpolate(a, [], freqs);
2.1.2 psdData Method: RMS
The RMS of a PSD is the most common metric. The RMS method allows the psdData class to calculate the RMS as it accumulates at each frequency point. The accumulation at each frequency is good for plotting the RMS and determining at a glance where the main contributors to the total RMS reside. However, the metric is typically the total RMS which can be determined by using max(rms) or max(backRms).
function [freq, rms, backRms] = RMS(obj, intType)
Input Arguments:
 psdData Class object
 Integration Type (Optional, see 'inttype' in the plot and plotTime methods for details)
Outputs:
 Vector of Frequencies
 Forward RMS (vector)
 Backward RMS (vector)
Examples:
>> [f, r, b] = RMS(a); >> [f, r, b] = RMS(a, 'trap');
2.1.3 psdData Methods: integrate & differentiate
Currently there are some very good, not so expensive, MEMS angular rate gyros and MEMS accelerometers. As a result a lot of the data I deal with comes in rate or acceleration but my simulation typically needs position (occasionally, acceleration) for the disturbance inputs. So the integration and differentiation methods were written so that I could easily get a PSD in the correct units.
[intObj] = integrate(obj) [diffObj] = differentiate(obj)
Input Arguments:
 psdData Class object
Outputs:
 psdData Class object
Examples:
>> b = integrate(a); >> c = differentiate(a);
2.1.4 psdData Methods: plus & minus
PSDs can be added and subtracted, however, the 2 PSDs must have magnitude values at the same frequency points. So the overloaded methods plus and minus were written to interpolate, handle frequency unit differences, and some general clean up.
[addObj] = plus(obj1, obj2) [subObj] = minus(obj1, obj2)
Input Arguments:
 psdData Class object
 psdData Class object
Outputs:
 psdData Class object
Examples: Assume that a and b are psdData objects then
 c = a + b;
 c = a  b;
 d = b  a;
c & d are therefore also psdData objects.
2.1.5 psdData Methods:setUnits & getUnits
Currently the psdData class does not handle changing from magnitude unit to another (i.e. meters to feet or radians to degrees). The class does convert between 'Hz' and 'rad/sec' and the plot and plotTime methods do include units on all figures generated through these methods.
The getUnits method allows for the return of the units as currently known by the class. The setUnits allows the user to set all relevant units.
function [units] = getUnits(obj)
Input Arguments:
 psdData Class object
Outputs:
 String
Examples:
>> getUnits(a) ans = rad/sec
function [obj] = setUnits(obj, varargin)
Input Arguments:
 psdData Class object
 varargin  Property/Value pairs
Outputs:
 psdData Class object
Valid Property/Value Pairs are:
Parameters  Value 

'time', 'time unit', 'timeunit', 'timescale', 'time scale' 
Any Integer OR any string matching TimeStr (see psdData Properties) 
'base magnitude', 'base mag', 'mag', 'magnitude' 
Any string representing the PSD magnitude units the user desires (without the Hz or rad/sec) 
'base frequency', 'base freq', 'freq', 'frequency' 
'Hz' or 'rad/sec' 
'plot frequency', 'plot freq', 'plot', 'plot unit', 'plot units' 
'Hz' or 'rad/sec' 
Examples:
>> a = setUnits(a, 'time', 1); >> a = setUnits(a, 'time unit', 1); >> a = setUnits(a, 'time', 'rate'); >> a = setUnits(a, 'base freq', 'rad/sec'); >> a = setUnits(a, 'base freq', 'Hz'); >> a = setUnits(a, 'plot freq', 'Hz'); >> a = setUnits(a, 'plot', 'rad/sec');
2.1.6 psdData Methods: plot & plotTime
There are a few standard plots that I like to see when validating measured data or reporting model results. I've incorporated those plots into the plot and plotTime methods.
 [h] = plot(h, a, varargin)
 [h] = plotTime(h, a, varargin)
Input Arguments:
 Figure handle (Optional)
 psdData object to be plotted
 varargin
Outputs:
 Figure handle to figure with PSD plot
Notes on varargin
 The first inputs, after the figure handle, must be psdData objects. Not a cell array of objects but simply the objects themselves  plot([], a, b, c, 'Title', 'My Plot', ...);
 After the last psdData object is entered then Property/Value pairs can be entered. Any valid axes property can be used just as if you had plotted the figure from the command prompt. Additional Property/Value pairs are:
Parameters  Value 

'plottype', 'type' 
1, 2, or 3  1 = PSD only, 2 = RMS only, 3 = RMS & PSD > Defaults to 3 
'units', 'unit', 'plotfrequnit' 
'Hz' or 'rad/sec' 
'backsum' 
TRUE or FALSE 
'inttype' 
'average', 'ave', 'mean' OR 'trapezoidal', 'trapezoid', 'trap' "Average"  This is mathematically equivalent to a trapezoidal integration Notes: freq = freqVec(2:end); forward = sqrt(((mag_1 + mag_2) / 2) * (f_2  f_1)); "Trapezoidal"  This uses Matlab's cumtrapz instead of the RMS equation above Notes: freq = freqVec; forward = sqrt(abs(cumtrapz(freqVec, magVec))); 
'name', 'title' 
Sets the top axis' Title property as well and the figure's Name property to the string provided 
'legend' 
Provides a legend with the names of the PSDs at the designated location, see MATLAB's help on legends for possible location strings 
'rms legend' 
Provides a legend with the names and max RMS of the PSDs at the designated location, see MATLAB's help on legends for possible location strings 
'stats legend'  only valid for plotTime 
Provides a legend with the names and stats (mean and standard deviation) of the Time Series at the designated location, see MATLAB's help on legends for possible location strings 
Examples:
3 Notes
 psdData assumes that any PSD provided to it is 1sided
 psdData creates 1sided PSDs from Time Series Data
 The integrate, differentiate, plus, and minus methods will provide results based on the PSD meaning that Time Series data is not present in the resulting psdData object (it will still be present in the original objects though)
 Calculation of the PSD from time series data requires the pwelch function and pwelch is part of the Signal Processing Toolbox.