pySW4.utils.spectral module

Python module for spectral analysis.

author:Shahar Shani-Kadmiel (s.shanikadmiel@tudelft.nl)
copyright:Shahar Shani-Kadmiel
license:This code is distributed under the terms of the GNU Lesser General Public License, Version 3 (https://www.gnu.org/copyleft/lesser.html)
fourier_spectrum(data, womean=False, winsize=None, stepsize=None, delta=None, verbose=False)[source]

Compute the Fourier spectrum of a signal either as a whole or as a series of windows with or without overlaps for smoother spectrum.

The reason for windowing a signal into smaller chunks is to smooth a ‘noisy’ Fourier spectrum of a very long signal. The frequency resolution of the FFT is related to the number of points (npts) passed into the FFT. The number of evenly spaced frequencies, sometimes refered to as bins, resolved between 0 and the nyquist frequency, 0.5/delta are (0.5*npts)+1 if npts is even or 0.5*(npts+1) if npts is odd.

So the FFT of a signal of length 2048 npts and delta of 0.01 will have a nyquist frequency of 50 Hz and 1025 (aka FFT size or bins) resolved frequencies making the lowest resolved frequency 0.0488 Hz. Had the signal been longer, say 10000 points (100 seconds), the FFT size would be 5001 and the frequency resolution would be 0.01 Hz making the the spectrum very noisy numerically with many frequencies that don’t contain any real information.

Pay attention to the stepsize parameter. Leave it None if you are not sure what is the correct value. If a high energy peak in the time domain is picked up by more than one window due to ovelap, that can be accumulated to affect the over all amplitude in the frequency domain. On the other hand, if by slicing the signal into windows you introduce step-functions as the windowed signal starts with a steep rise or ends with a steep fall, the resulting frequency spectrum will contain false amplitudes at high frequencies as a result. Setting an overlap will get rid of this problem as these unwanted effects will be canceled out by one another or averaged by several windows.

Note

This function is a convinient wrapper for the fft() function.

Parameters:

data : ndarray or Trace instance

If a sequence (ndarray) of time-history values is passed delta must be supplied as well. Otherwise pass an Trace instance.

womean : bool

Remove the mean of the signal before performing the transform. Default is False, transform as is. In cases where there is a shift in the signal such that the baseline is not zero, setting womean=True will result in a smaller DC.

winsize : int

By default winsize is None, taking the FFT of the entire signal as-is. Otherwise, winsize sets the size of the sliding window, taking winsize points of the signal at a time. Works fastest when winsize is a whole power of 2, i.e., 128, 512, 1024, 2048, 4096 etc.

stepsize : int

The number of points by which the window slides each time. By default, stepsize is None, making it equal to winsize, no overlap. Setting stepsize to half winsize is common practice and will cause a 50% overlap.

delta : float

The dt from one sample to the next such that 1/delta is the sampling rate.

verbose : bool

If set to True some information about the process is printed.

Returns:

2 ndarray

  • frequency array and
  • amplitude array
next_power_2(x)[source]
psde(data, womean=False, winsize='default', stepsize=None, delta=None, verbose=False)[source]

Wrapper for matplotlib.mlab.psd() for conveniently estimating the power spectral density of a signal.

See fourier_spectrum() documentation for keyword argument explanation and rational.

Returns:

2 ndarray

  • frequency array and
  • amplitude array