kwave.utils.math module

Rx(theta)[source]

3D rotation matrix for rotation about x-axis

Args: theta : float. Angle of rotation (in degrees)

Returns: np.array. 3D rotation matrix

Ry(theta)[source]

3D rotation matrix for rotation about y-axis

Args: theta : float. Angle of rotation (in degrees)

Returns: np.array. 3D rotation matrix

Rz(theta)[source]

3D rotation matrix for rotation about z-axis

Args: theta : float. Angle of rotation (in degrees)

Returns: np.array. 3D rotation matrix

compute_linear_transform(pos1, pos2, offset=None)[source]
cosd(angle_in_degrees)[source]
find_closest(A, a)[source]

Returns the value and index of the item in A that is closest to the value a.

This function finds the value and index of the item in the input array A that is closest to the given value a. For vectors, the value and index correspond to the closest element in A. For matrices, value and index are row vectors corresponding to the closest element from each column. For N-D arrays, the function finds the closest value along the first matrix dimension (singleton dimensions are removed before the search). If there is more than one element with the closest value, the index of the first one is returned.

Parameters:
  • A (ndarray) – The array to search.

  • a (float | int) – The value to find.

Returns:

A tuple containing the value and index of the closest element in A to a.

Return type:

Tuple[float | int, Tuple[int, …]]

fourier_shift(data, shift, shift_dim=None)[source]

Shifts an array along one of its dimensions using Fourier interpolation.

Parameters:
  • data (ndarray) – The input array.

  • shift (float) – The amount of shift to apply.

  • shift_dim (int | None) – The dimension along which to shift the array. Default is the last dimension.

Returns:

The shifted array.

Return type:

ndarray

gaussian(x, magnitude=None, mean=0, variance=1)[source]

Returns a Gaussian distribution f(x) with the specified magnitude, mean, and variance. If these values are not specified, the magnitude is normalised and values of variance = 1 and mean = 0 are used. For example running:

import matplotlib.pyplot as plt x = np.arange(-3, 0.05, 3) plt.plot(x, gaussian(x))

will plot a normalised Gaussian distribution.

Note, the full width at half maximum of the resulting distribution can be calculated by FWHM = 2 * sqrt(2 * log(2) * variance).

Parameters:
  • x (int | float | ndarray) – The input values.

  • magnitude (int | float | None) – Bell height. Defaults to normalised.

  • mean (float | None) – Mean or expected value. Defaults to 0.

  • variance (float | None) – Variance, or bell width. Defaults to 1.

Returns:

A Gaussian distribution.

Return type:

int | float | ndarray

get_affine_matrix(translation, rotation)[source]
Parameters:
  • translation (Vector)

  • rotation (int | float | ndarray | Vector)

largest_prime_factor(n)[source]

Finds the largest prime factor of a positive integer.

Parameters:

n (int) – The positive integer to be factored.

Returns:

The largest prime factor of n.

Return type:

int

next_pow2(n)[source]

Calculate the next power of 2 that is greater than or equal to n.

This function takes a positive integer n and returns the smallest power of 2 that is greater than or equal to n.

Parameters:

n (int) – The number to find the next power of 2 for.

Returns:

The smallest power of 2 that is greater than or equal to n.

Return type:

int

norm_var(im)[source]

Calculates the normalized variance of an array of values.

Parameters:

im (ndarray) – The input array.

Returns:

The normalized variance of im.

Return type:

float

primefactors(n)[source]

Finds the prime factors of a given integer.

Parameters:

n (int) – The integer to factor.

Returns:

A list of prime factors of n.

Return type:

List[int]

round_even(x)[source]

Rounds to the nearest even integer.

Parameters:

x – Input value

Returns:

Nearest even integer.

round_odd(x)[source]

Rounds to the nearest odd integer.

Parameters:

x – Input value

Returns:

Nearest odd integer.

rwh_primes(n)[source]

Generates a list of prime numbers less than a given integer.

Parameters:

n (int) – The upper bound for the list of primes.

Returns:

A list of prime numbers less than n.

Return type:

List[int]

sinc(x)[source]

Calculates the sinc function of a given value or array of values.

Parameters:

x (int | float | ndarray) – The value or array of values for which to calculate the sinc function.

Returns:

The sinc function of x.

Return type:

int | float | ndarray

sind(angle_in_degrees)[source]