sdynpy.core.sdynpy_data.SpectrumArray

class SpectrumArray(shape, nelements, buffer=None, offset=0, strides=None, order=None)[source]

Bases: NDDataArray

Data array used to store linear spectra (for example scaled FFT results)

__init__()

Methods

apply_transformation(transformation[, ...])

Applies response transformations spectra.

ifft([norm, rtol, atol, odd_num_samples])

Computes a time signal from the frequency spectrum

interpolate_by_zero_pad(...[, ...])

Interpolates a spectrum by zero padding or truncating its time response

plot([one_axis, subplots_kwargs, ...])

Plot the spectra

plot_spectrogram([abscissa, axis, ...])

Plots a spectrogram

Attributes

function_type

Returns the function type of the data array

apply_transformation(transformation, invert_transformation=False)[source]

Applies response transformations spectra.

Parameters
  • transformation (Matrix) – The transformation to apply to the spectra. It should be a SDynPy matrix object with the “transformed” coordinates on the rows and the “physical” coordinates on the columns. The matrix can be either 2D or 3D (for a frequency dependent transform).

  • invert_reference_transformation (bool, optional) – Whether or not to invert the transformation when applying it to the spectra. The default is False, which is standard practice. The row/column ordering in the transformation should be flipped if this is set to true.

Raises

ValueError – If the physical degrees of freedom in the transformation does not match the spectra.

Returns

transformed_spectra – The spectra with the transformation applied.

Return type

SpectrumArray

property function_type

Returns the function type of the data array

ifft(norm='backward', rtol=1, atol=1e-08, odd_num_samples=False, **scipy_irfft_kwargs)[source]

Computes a time signal from the frequency spectrum

Parameters
  • norm (str, optional) – The type of normalization applied to the fft computation. The default is “backward”.

  • rtol (float, optional) – Relative tolerance used in the abcsissa spacing check. The default is 1e-5.

  • atol (float, optional) – Relative tolerance used in the abscissa spacing check. The default is 1e-8.

  • odd_num_samples (bool, optional) – If True, then it is assumed that the output signal has an odd number of samples, meaning the signal will have a length of 2*(m-1)+1 where m is the number of frequency lines. Otherwise, the default value of 2*(m-1) is used, assuming an even signal. This is ignored if num_samples is specified.

  • scipy_irfft_kwargs – Additional keywords that will be passed to SciPy’s irfft function.

Raises
  • ValueError – Raised if the spectra passed to this function do not have equally spaced abscissa.

  • NotImplementedError – Raised if the user specifies scaling.

Returns

The time history of the SpectrumArray.

Return type

TimeHistoryArray

Notes

Note that the ifft uses the rfft function from scipy to compute the inverse fast fourier transform. This function is not round-trip equivalent for odd functions, because by default it assumes an even signal length. For an odd signal length, the user must either specify odd_num_samples = True or set num_samples to the correct number of samples.

interpolate_by_zero_pad(time_response_padded_length, return_time_response=False, odd_num_samples=False)[source]

Interpolates a spectrum by zero padding or truncating its time response

Parameters
  • time_response_padded_length (int) – Length of the final zero-padded time response

  • return_time_response (bool, optional) – If True, the zero-padded impulse response function will be returned. If False, it will be transformed back to a transfer function prior to being returned.

  • odd_num_samples (bool, optional) – If True, then it is assumed that the spectrum has been constructed from a signal with an odd number of samples. Note that this function uses the rfft function from scipy to compute the inverse fast fourier transform. The irfft function is not round-trip equivalent for odd functions, because by default it assumes an even signal length. For an odd signal length, the user must either specify odd_num_samples = True to make it round-trip equivalent.

Returns

Spectrum array with appropriately spaced abscissa

Return type

SpectrumArray or TimeHistoryArray

Notes

This function will automatically set the last frequency line of the SpectrumArray to zero because it won’t be accurate anyway. If time_response_padded_length is less than the current function’s num_elements, then it will be truncated instead of zero-padded.

plot(one_axis=True, subplots_kwargs={}, plot_kwargs={}, abscissa_markers=None, abscissa_marker_labels=None, abscissa_marker_type='vline', abscissa_marker_plot_kwargs={})[source]

Plot the spectra

Parameters
  • one_axis (bool, optional) – Set to True to plot all data on one axis. Set to False to plot data on multiple subplots. one_axis can also be set to a matplotlib axis to plot data on an existing axis. The default is True.

  • subplots_kwargs (dict, optional) – Keywords passed to the matplotlib subplots function to create the figure and axes. The default is {}.

  • plot_kwargs (dict, optional) – Keywords passed to the matplotlib plot function. The default is {}.

  • abscissa_markers (ndarray, optional) – Array containing abscissa values to mark on the plot to denote significant events.

  • abscissa_marker_labels (str or ndarray) – Array of strings to label the abscissa_markers with, or alternatively a format string that accepts index and abscissa inputs (e.g. ‘{index:}: {abscissa:0.2f}’). By default no label will be applied.

  • abscissa_marker_type (str) – The type of marker to use. This can either be the string ‘vline’ or a valid matplotlib symbol specifier (e.g. ‘o’, ‘x’, ‘.’).

  • abscissa_marker_plot_kwargs (dict) – Additional keyword arguments used when plotting the abscissa label markers.

Returns

axis – On which the data were plotted

Return type

matplotlib axis or array of axes

plot_spectrogram(abscissa=None, axis=None, subplots_kwargs={}, pcolormesh_kwargs={'shading': 'auto'}, log_scale=True)[source]

Plots a spectrogram

Parameters
  • abscissa (np.ndarray) – Optional argument to specify as the abscissa values. If not specified, this will be the index of the flattened SpectrumArray.

  • axis (matplotlib.axis, optional) – An optional argument that specifies the axis to plot the spectrogram on

  • subplots_kwargs (dict, optional) – Optional keywords to specify to the subplots function that creates a new figure if axis is not specified.

  • pcolormesh_kwargs (dict, optional) – Optional arguments to pass to the pcolormesh function

  • log_scale (bool) – If True, the colormap will be applied logarithmically

Returns

ax – The axis on which the spectrogram was plotted

Return type

matplotlib.axis