Helper functions

ssp.keras.ops.helper.circular_hard_lowpass(n, spectral_radius, truly_symmetric=False)[source]

2-D binary mask of shape (n,n) that can be multiplied with the FFT frequencies, e.g.,

circular_hard_lowpass = [
  [0, 0, 1, 0, 0],
  [0, 1, 1, 1, 0],
  [1, 1, 1, 1, 1],
  [0, 1, 1, 1, 0],
  [0, 0, 1, 0, 0]
]
Parameters:
  • n (int) – Grid size of the data, square grid (n,n)

  • spectral_radius (int) – Radius of the lowpass on the (n,n) grid

  • truly_symmetric (bool, optional) – If this is set, ops.linspace is used insted of ops.arange to achieve a truly symmetric filter (for odd n). Defaults to False.

Returns:

hard_lowpass – Binary 2-D mask of size (n,n)

Return type:

KerasTensor

ssp.keras.ops.helper.fftfreq(n, d=1, rad=False)[source]

Return the Discrete Fourier Transform sample frequencies.

The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

Given a window length n and a sample spacing d:

f = [0, 1, ...,   n/2-1,     -n/2, ..., -1] / (d*n)   if n is even
f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n)   if n is odd
Parameters:
  • n (int) – Window length.

  • d (scalar, optional) – Sample spacing (inverse of the sampling rate). Defaults to 1.

  • rad (bool, optional) – If this is set, the angular frequency omega=2*pi*f is returned. Defaults to False.

Returns:

f – Tensor of length n containing the sample frequencies.

Return type:

KerasTensor

Examples

>>> from keras import ops
>>> from ssp.keras.ops import fft, fftfreq
>>> signal = ops.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float)
>>> fourier = fft(signal)
>>> n = ops.size(signal)
>>> timestep = 0.1
>>> freq = fftfreq(n, d=timestep)
>>> freq
array([ 0.  ,  1.25,  2.5 , ..., -3.75, -2.5 , -1.25])
ssp.keras.ops.helper.hard_lowpass(n, spectral_radius, truly_symmetric=False)[source]

1-D binary mask of shape n that can be multiplied with the FFT frequencies

hard_lowpass = [0, ..., 0, 1, ..., 1, 0, ..., 0]
Parameters:
  • n (int) – Grid size of the data

  • spectral_radius (int) – Width of the lowpass

  • truly_symmetric (bool, optional) – If this is set, ops.linspace is used insted of ops.arange to achieve a truly symmetric filter (for odd n). Defaults to False.

Returns:

hard_lowpass – Binary 1-D mask of size (n,)

Return type:

KerasTensor

ssp.keras.ops.helper.squeeze_or_expand_to_same_rank(x1, x2, axis=-1, expand_rank_1: bool = True) tuple[source]

Squeeze/expand along axis if ranks differ from expected by exactly 1.

Parameters:
  • x1 (KerasTensor) – first input tensor

  • x2 (KerasTensor) – second input tensor

  • axis (int, optional) – axis to squeeze or expand along. Defaults to -1.

  • expand_rank_1 (bool, optional) – Defaults to True

Returns:

x1, x2 – Tuple of (x1, x2) with the same shape

Return type:

(KerasTensor, KerasTensor)