PsdData Methods


Jump to: navigation, search
PsdData Methods
Green carrot left.gif
psdData Class Example psdData Properties
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

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:

  1. The PSD is represented in MATLAB by a vector of frequencies and magnitudes at those frequencies.
  2. PSDs typically (but not always) have units of magnitude squared (^2) over Hz.
  3. 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:

  1. 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.
  2. Addition and subtraction of PSDs must occur at the same frequency and this often dictates the use of an interpolated PSD.
  3. 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:

  1. Issue: Coarse PSD --> Solution: interpolation method
  2. Issue: Addition & Subtraction --> Solution: overloaded plus & minus methods
  3. 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:

  1. Public: Methods the user can call
  2. 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:

  1. psdData Class object
  2. Number of Points for interpolation (Optional, defaults to MinNumPnts property)
  3. Frequency vectors (Optional, allows user to provide a vector of frequency points or end points)


  1. psdData Class object


               >> 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:

  1. psdData Class object
  2. Integration Type (Optional, see 'inttype' in the plot and plotTime methods for details)


  1. Vector of Frequencies
  2. Forward RMS (vector)
  3. Backward RMS (vector)


               >> [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:

  1. psdData Class object


  1. psdData Class object


               >> 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:

  1. psdData Class object
  2. psdData Class object


  1. 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:

  1. psdData Class object


  1. String


 >> getUnits(a)
       ans =

function [obj] = setUnits(obj, varargin)

Input Arguments:

  1. psdData Class object
  2. varargin - Property/Value pairs


  1. psdData Class object

Valid Property/Value Pairs are:

Table 1: Property/Value Pairs for setUnits & getUnits Methods
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'


 >> 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:

  1. Figure handle (Optional)
  2. psdData object to be plotted
  3. varargin


  1. 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:
Table 2: Property/Value Pairs for plot & plotTime Methods
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'




'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


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


plot([], a, 'Title', 'My PSD')
plot([], a, 'Title', 'My PSD', 'PlotType', 1)
plot([], a, 'Title', 'My PSD', 'PlotType', 2)
plot([], a, b, 'Title', 'My 2 PSDs', 'legend', 'SouthWest')
plot([], a, b, 'Title', 'My 2 PSDs', 'rms legend', 'SouthWest')
plotTime([], a, b, 'Title', 'My 2 Time Series', 'legend', 'SouthWest')
plotTime([], a, b, 'Title', 'My 2 Time Series', 'stats legend', 'SouthWest')

3 Notes

  • psdData assumes that any PSD provided to it is 1-sided
  • psdData creates 1-sided 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.