cutcutcodec.core.signal.window

Window a signal to control gibbs effects.

The optimal dpss window is implemented here, but it is not very stable computationally. All functions are based on the Kaiser window, which is a good compromise between stability and optimality.

Functions

alpha_to_att(alpha)

Empirical estimation based on regression.

alpha_to_band(alpha)

Empirical estimation based on regression.

att_to_alpha(att)

Inverse of the empirical estimation based on regression.

band_to_alpha(band)

Inverse of the empirical estimation based on regression.

dpss(nb_samples, alpha[, dtype])

Compute the Discrete Prolate Spheroidal Sequences (DPSS).

find_win_law([nb_samples, nb_alphas, ...])

For each beta parameter, associate the frequency properties.

kaiser(nb_samples, alpha[, dtype])

Compute the Kaiser–Bessel window.

Details

cutcutcodec.core.signal.window.alpha_to_att(alpha: Real) float[source]

Empirical estimation based on regression.

The fitted model is \(\eta = a*\alpha^2 + b*\alpha + c + d*\tanh(e*\alpha)\).

This function is strictly increasing.

Bijection of att_to_alpha().

Examples

>>> import torch
>>> from cutcutcodec.core.signal.window import alpha_to_att, find_win_law
>>> alphas, atts, _ = find_win_law()
>>> pred = [alpha_to_att(a) for a in alphas.tolist()]
>>> # import matplotlib.pyplot as plt
>>> # _ = plt.plot(alphas.numpy(force=True), atts.numpy(force=True))
>>> # _ = plt.plot(alphas.numpy(force=True), pred)
>>> # plt.show()
>>>
cutcutcodec.core.signal.window.alpha_to_band(alpha: Real) float[source]

Empirical estimation based on regression.

The fitted model is \(band = a*\alpha^2 + b*\alpha + c + d*tanh(e*\alpha)\).

This function is strictly increasing.

Bijection of band_to_alpha().

Examples

>>> import torch
>>> from cutcutcodec.core.signal.window import alpha_to_band, find_win_law
>>> alphas, _, bands = find_win_law()
>>> pred = [alpha_to_band(a) for a in alphas.tolist()]
>>> # import matplotlib.pyplot as plt
>>> # _ = plt.plot(alphas.numpy(force=True), bands.numpy(force=True))
>>> # _ = plt.plot(alphas.numpy(force=True), pred)
>>> # plt.show()
>>>
cutcutcodec.core.signal.window.att_to_alpha(att: Real) float[source]

Inverse of the empirical estimation based on regression.

Bijection of alpha_to_att().

As there is no closed form for this function, it is approximated using the tangent method.

Examples

>>> from cutcutcodec.core.signal.window import alpha_to_att, att_to_alpha
>>> round(alpha_to_att(att_to_alpha(20.0)), 6)
20.0
>>> round(alpha_to_att(att_to_alpha(40.0)), 6)
40.0
>>> round(alpha_to_att(att_to_alpha(80.0)), 6)
80.0
>>> round(alpha_to_att(att_to_alpha(120.0)), 6)
120.0
>>> round(alpha_to_att(att_to_alpha(160.0)), 6)
160.0
>>>
cutcutcodec.core.signal.window.band_to_alpha(band: Real) float[source]

Inverse of the empirical estimation based on regression.

Bijection of alpha_to_band().

As there is no closed form for this function, it is approximated using the tangent method.

Examples

>>> from cutcutcodec.core.signal.window import alpha_to_band, band_to_alpha
>>> round(alpha_to_band(band_to_alpha(0.9)), 6)
0.9
>>> round(alpha_to_band(band_to_alpha(1.8)), 6)
1.8
>>> round(alpha_to_band(band_to_alpha(3.4)), 6)
3.4
>>> round(alpha_to_band(band_to_alpha(4.9)), 6)
4.9
>>> round(alpha_to_band(band_to_alpha(6.4)), 6)
6.4
>>>
cutcutcodec.core.signal.window.dpss(nb_samples: Integral, alpha: Real, dtype=torch.float64) Tensor[source]

Compute the Discrete Prolate Spheroidal Sequences (DPSS).

It is similar to the scipy function scipy.signal.windows.dpss.

DPSS windows

Parameters

nb_samplesint

The window size, it has to be >= 3.

alphafloat

Standardized half bandwidth.

dtypetorch.dtype, default=float64

The data type of the window samples: torch.float64 or torch.float32.

Returns

windowtorch.Tensor

The 1d symetric window, normalized with the maximum value at 1.

Examples

>>> import torch
>>> from cutcutcodec.core.signal.window import dpss
>>> dpss(1024, 2.0)
tensor([0.0158, 0.0163, 0.0169,  ..., 0.0169, 0.0163, 0.0158],
       dtype=torch.float64)
>>>
>>> # comparison with kaiser
>>> alpha, nbr = 5.0, 129
>>> win_dpss = dpss(nbr, alpha)
>>> win_kaiser = torch.kaiser_window(
...     nbr, periodic=False, beta=alpha*torch.pi, dtype=torch.float64
... )
>>> gain_dpss = 20*torch.log10(abs(torch.fft.rfft(win_dpss, 100000)))
>>> gain_dpss -= torch.max(gain_dpss)
>>> gain_kaiser = 20*torch.log10(abs(torch.fft.rfft(win_kaiser, 100000)))
>>> gain_kaiser -= torch.max(gain_kaiser)
>>>
>>> # import matplotlib.pyplot as plt
>>> # fig, (ax1, ax2) = plt.subplots(2)
>>> # _ = ax1.plot(win_dpss, label="dpss")
>>> # _ = ax1.plot(win_kaiser, label="kaiser")
>>> # _ = ax1.legend()
>>> # _ = ax2.plot(torch.linspace(0, 0.5, 50001), gain_dpss, label="dpss")
>>> # _ = ax2.plot(torch.linspace(0, 0.5, 50001), gain_kaiser, label="kaiser")
>>> # _ = ax2.axvline(x=alpha/nbr)
>>> # _ = ax2.legend()
>>> # plt.show()
>>>
cutcutcodec.core.signal.window.find_win_law(nb_samples: Integral = 129, nb_alphas: Integral = 1000, alpha_min: Real = 0.001, alpha_max: Real = 15.0, win: str = 'kaiser') tuple[Tensor, Tensor, Tensor][source]

For each beta parameter, associate the frequency properties.

Parameters

nb_samplesint, default=129

The window size, it has to be >= 3.

nb_alphasint, default=1000

The number of alpha points.

alpha_minfloat, default=ALPHA_MIN

The minimal inclusive alpha value.

alpha_maxfloat, default=15.0

The maximal inclusive alpha value.

winstr, default=”kaiser”

The windows type, “kaiser” or “dpss”

Returns

alphastorch.Tensor

The apha values.

attstorch.Tensor

The real positive attenuation of the secondaries lobs in dB.

bandstorch.Tensor

The normalised size of the main lob.

Examples

>>> import torch
>>> from cutcutcodec.core.signal.window import find_win_law
>>> alphas, atts, bands = find_win_law()
>>>
>>> # import matplotlib.pyplot as plt
>>> # _ = plt.plot(alphas.numpy(force=True), atts.numpy(force=True), label="attenuation")
>>> # _ = plt.plot(alphas.numpy(force=True), bands.numpy(force=True), label="band")
>>> # _ = plt.legend()
>>> # plt.show()
>>>
cutcutcodec.core.signal.window.kaiser(nb_samples: Integral, alpha: Real, dtype=torch.float64) Tensor[source]

Compute the Kaiser–Bessel window.

It is an approximation of cutcutcodec.core.signal.window.dpss().

Kaiser windows

Parameters

nb_samplesint

The window size, it has to be >= 3.

alphafloat

Standardized half bandwidth.

dtypetorch.dtype, default=float64

The data type of the window samples: torch.float64 or torch.float32.

Returns

windowtorch.Tensor

The 1d symetric window, normalized with the maximum value at 1.