Signal processing functionality

The signal processing functionality provides several standard functions for processing digitized analog signals.

Using the signal processing functionality

To use the signal processing functionality you first need to initialize a DwfLibrary instance. The signal processing functionality can then be accessed via its spectrum attribute, which is an instance of the Spectrum class:

from pydwf import DwfLibrary, DwfWindow

dwf = DwfLibrary()

# Make an 11-element Hamming window.
(hamming_window, noise_equivalent_bandwidth) = dwf.spectrum.window(11, DwfWindow.Hamming)

Spectrum reference

class Spectrum

The Spectrum class provides access to the signal processing functionality of a DwfLibrary.

Attention

Users of pydwf should not create instances of this class directly.

It is instantiated during initialization of a DwfLibrary and subsequently assigned to its public spectrum attribute for access by the user.

window(n: int, winfunc: DwfWindow, beta: float | None = None) Tuple[ndarray, float]

Return the window coefficients for the specified window, and the window’s noise-equivalent bandwidth.

Parameters:
  • n (int) – The length of the window to be generated.

  • winfunc (DwfWindow) – The type of window to be generated.

  • beta (Optional[float]) – Parameter for the Kaiser window; unused for other windows. If not specified, a value of 0.5 is used.

Returns:

A two-element tuple. The first element of the tuple is a numpy array of length n, containing the window coefficients. All windows supported are symmetric, and they are scaled to have the sum of their values equal to n.

The second element of the tuple is the noise equivalent bandwidth of the signal. For a coefficient array w, this value is equal to len(w) * np.sum(w**2) / np.sum(w)**2.

Return type:

Tuple[np.ndarray, float]

Note

When working in Python, it may be better to use the functionality provided in the scipy.signal.windows package instead.

fft(data: ndarray) Tuple[ndarray, ndarray]

Perform a Fast Fourier Transform.

Parameters:

data – The real-valued data to be transformed. This must be a 1-dimensional array with a length that is a power of 2.

Returns:

A tuple (magnitude, phase); both the magnitude and phase arrays are real-valued 1D numpy arrays with the same length as the input array data. The first array contains the non-negative magnitude of the signal for this particular frequency; the second array contains its phase.

Return type:

Tuple[np.ndarray, np.ndarray]

Note

The scaling of the FFT calculated is unusual. Compared to Matlab, numpy, and most other implementations, it is scaled by a factor (2 / n), with n the number of points in the input array.

To get from the (magnitude, phase) representation as returned by this function to a vector of complex number that is comparable to what e.g. the numpy.fft.rfft function returns, you can do:

z = (len(magnitude) / 2.0) * magnitude * np.exp(phase * 1j)

When working in Python, it may be better to simply use the numpy.fft.rfft() function directly. It is faster and provides support for data vectors with a length that is not a power of two.

transform(data: ndarray, num_bins: int, first_freq: float, last_freq: float) Tuple[ndarray, ndarray]

Perform a Chirp-Z transform.

Parameters:
  • data – The real-valued data to be transformed.

  • num_bins (int) – The number of bins to be calculated.

  • first_freq (float) – The frequency of the first bin, scaled to 0.5 * sample_frequency.

  • 1 (Should be between 0 and) –

  • inclusive.

  • last_freq (float) – The frequency of the last bin, scaled to 0.5 * sample_frequency.

  • 1

  • inclusive.

Returns:

A tuple (magnitude, phase); both the magnitude and phase arrays are real-valued 1D numpy arrays with the same length as the input array data. The first array contains the non-negative magnitude of the signal for this particular frequency; the second array contains its phase.

Return type:

Tuple[np.ndarray, np.ndarray]

Note

The scaling of the FFT calculated is unusual. Compared to Matlab, numpy, and most other implementations, it is scaled by a factor (2 / n), with n the number of points in the input array.

To get from the (magnitude, phase) representation as returned by this function to a vector of complex number that is comparable to what e.g. the scipy.signal.czt function returns, you can do:

z = (len(data) / 2.0) * magnitude * np.exp(phase * 1j)

When working in Python, it may be better to simply use the scipy.signal.czt() function directly.

property dwf

Return the DwfLibrary instance of which we are an attribute.

Returns:

The DwfLibrary instance.

Return type:

DwfLibrary