cutcutcodec.core.signal.psd

Tools for the Power Spectral Density (PSD) estimation.

Functions

intercorr(signal_1, signal_2, win, stride[, ...])

Compute the average intercorrelation of 2 signal using the Welch method.

welch(signal_1[, signal_2, band])

Estimate the power spectral density (PSD) ie intercorrelation with the Welch method.

Details

cutcutcodec.core.signal.psd.intercorr(signal_1: Tensor, signal_2: Tensor, win: Tensor, stride: int, return_std: bool = False) Tensor[source]

Compute the average intercorrelation of 2 signal using the Welch method.

Parameters

signal_1, signal_2torch.Tensor

The 2 broadcastable real temporal signals.

wintorch.Tensor

The 1d full window used, see cutcutcodec.core.signal.window.

strideint

The gap between two sliding windows, stride >= 1.

return_stdboolean, default=False

If True, return the standard deviation of the psd estimation.

Returns

psdtorch.Tensor

The average psd of each slices. Complex if intercorrelation and real if autocorrelation.

stdtorch.Tensor, if return_std == True

The unbiaised real standard deviation between all psd in each segment for each frequency band.

cutcutcodec.core.signal.psd.welch(signal_1: Tensor, signal_2: Tensor | None = None, band: Real | None = None)[source]

Estimate the power spectral density (PSD) ie intercorrelation with the Welch method.

Welch method

It is based on cutcutcodec.core.signal.psd.intercorr().

The slices are smoothed using a Kaiser window, calculated with cutcutcodec.core.signal.window.kaiser().

\[\begin{split}\begin{cases} \Delta f = \frac{1 + 2 \beta}{n_w} \\ \end{cases}\end{split}\]

Parameters

signal_1torch.Tensor

A real stationary time signal.

signal_2default = signal_1

Another real stationary time signal. If provided, the intercorrelation is calculated. The returned signal is therefore a complex signal. If omitted (default), the autocorrelation is calculated, and the returned signal is therefore real positive.

bandfloat, optional

The normlised frequency resolution (Delta f) in \(\left(r \in \left]0, \frac{1}{2}\right[\right)\), for a sample rate of 1. Higher it is, better is the frequency resolution but the greater is the variance.

Returns

psdtorch.Tensor

The correlation of signals, estimated using the Welch method. In the case of autocorrelation, this is an estimation of the power spectral density.

Seealso

Examples

>>> import math
>>> import torch
>>> import matplotlib.pyplot as plt
>>> from cutcutcodec.core.signal.psd import welch

Generate a test signal, a 2 Vrms sine wave at 1234 Hz, corrupted by 0.001 V**2/Hz of white noise sampled at 10 kHz.

>>> fs, N, amp, freq = 10e3, 1e5, 2*math.sqrt(2), 1234.0
>>> noise_power = 0.001 * fs / 2
>>> time = torch.arange(N) / fs
>>> x = amp*torch.sin(2*torch.pi*freq*time)
>>> x += torch.randn(time.shape) * math.sqrt(noise_power)

Compute and plot the power spectral density.

>>> psd = welch(x, band=2/1024)
>>> f = torch.fft.rfftfreq(2*psd.shape[-1]-1, 1/fs)
>>> _ = plt.semilogy(f, psd)
>>> _ = plt.xlabel('frequency [Hz]')
>>> _ = plt.ylabel('PSD [V**2/Hz]')
>>> # plt.show()