CLM Utility Functions#

General#

get_length(obj)#

Returns length in samples of a sound file, list, ndarray, or generator

random(x=1.0)#

Returns random number +- x

get_channels(obj)#

Returns number of channels of a sound file, list, ndarray

Sampling rate#

get_srate(obj=None)#

Return current sample rate

set_srate(r)#

set current sample rate.

Conversions#

radians2hz(radians)#

convert radians per sample to frequency: hz = rads * srate / (2 * pi).

Parameters:

radians – frequency in radians

Returns:

frequency in hertz

Return type:

float

hz2radians(hz)#

convert frequency in hz to radians per sample: hz * 2 * pi / srate.

Parameters:

hz – frequency in hertz

Returns:

frequency in radians

Return type:

float

degrees2radians(degrees)#

convert degrees to radians: degrees * 2 * pi / 360.

Parameters:

degrees – angle in degrees

Returns:

angle in radians

Return type:

float

radians2degrees(radians)#

convert radians to degrees: rads * 360 / (2 * pi).

Parameters:

radians – angle in radians

Returns:

degree

Return type:

float

db2linear(x)#

convert decibel value db to linear value: pow(10, db / 20).

Parameters:

x – decibel

Returns:

linear amplitude

Return type:

float

linear2db(x)#

convert linear value to decibels 20 * log10(lin).

Parameters:

x – linear amplitude

Returns:

decibel

Return type:

float

seconds2samples(secs)#

use mus_srate to convert seconds to samples.

Parameters:

secs – time in seconds

Returns:

time in samples

Return type:

int

samples2seconds(samples)#

use mus_srate to convert samples to seconds.

Parameters:

samples – number of samples

Returns:

time in seconds

Return type:

float

Even/Odd multiples and weighting#

odd_multiple(x, y)#

return y times the nearest odd integer to x.

Parameters:
  • x

  • y

Returns:

nearest odd integer as a float

Return type:

float

even_multiple(x, y)#

return y times the nearest even integer to x.

Parameters:
  • x

  • y

Returns:

nearest even integer as a float

Return type:

float

odd_weight(x)#

return a number between 0.0 (x is even) and 1.0 (x is odd).

Parameters:

x

Return weight:

Return type:

float

even_weight(x)#

return a number between 0.0 (x is odd) and 1.0 (x is even).

Parameters:

x

Return weight:

Return type:

float

ring_modulate(s1, s2)#

return s1 * s2 (sample by sample multiply).

Parameters:
  • s1 – input 1

  • s2 – input 2

Returns:

result

Return type:

float

amplitude_modulate(s1, s2, s3)#

carrier in1 in2): in1 * (carrier + in2).

Parameters:
  • s1 – input 1

  • s2 – input 2

  • s3 – input 3

Returns:

result

Return type:

float

contrast_enhancement(insig, fm_index=1.0)#

returns insig (index 1.0)): sin(sig * pi / 2 + fm_index * sin(sig * 2 * pi)) contrast_enhancement passes its input to sin as a kind of phase modulation.

Parameters:
  • insig – input

  • fm_index

Returns:

result

Return type:

float

dot_product(data1, data2)#

returns v1 v2 (size)): sum of v1[i] * v2[i] (also named scalar product).

Parameters:
  • data1 – input 1

  • data2 – input 2

Returns:

result

Return type:

float

polynomial(coeffs, x)#

evaluate a polynomial at x. coeffs are in order of degree, so coeff[0] is the constant term.

Parameters:
  • coeffs – coefficients where coeffs[0] is the constant term, and so on.

  • x – input

Returns:

result

Return type:

float

array_interp(fn, x)#

taking into account wrap-around (size is size of data), with linear interpolation if phase is not an integer.

Parameters:
  • fn – input array

  • x – interp point

Returns:

result

Return type:

float

bessi0(x)#

bessel function of zeroth order

mus_interpolate(interp_type, x, table, y1=0.0)#

interpolate in data (‘table’ is a ndarray) using interpolation ‘type’, such as Interp.linear.

Parameters:
  • interp_type – type of interpolation

  • x – interpolation value

  • table – table to interpolate in

Returns:

result

Return type:

float

FFT and Convolution#

make_fft_window(window_type, size, beta=0.0, alpha=0.0)#

fft data window (a ndarray). type is one of the sndlib fft window identifiers such as window.kaiser, beta is the window family parameter, if any.

Parameters:
  • window_type – type of window

  • size – window size

  • beta – beta parameter if needed

  • alpha – alpha parameter if needed

Returns:

window

Return type:

np.ndarray

fft(rdat, idat, fft_size, sign)#

return the fft of rl and im which contain the real and imaginary parts of the data; len should be a power of 2, dir = 1 for fft, -1 for inverse-fft.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

  • fft_size – must be power of two

  • sign – 1 for fft, -1 for inverse-fft

Returns:

tuple of copy of rdat and idat

Return type:

tuple

rectangular2polar(rdat, idat)#

convert real/imaginary data in rdat and idat from rectangular form (fft output) to polar form (a spectrum).

Parameters:
  • rdat – real data

  • imaginary – imaginary data

Return::return:

tuple of magnitude and phases

Return type:

tuple

rectangular2magnitudes(rdat, idat)#

convert real/imaginary data in rl and im from rectangular form (fft output) to polar form, but ignore the phases.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

Returns:

magnitude

Return type:

np.ndarray

polar2rectangular(rdat, idat)#

in-place operation. convert real/imaginary data in rl and im from polar (spectrum) to rectangular (fft).

Parameters:
  • rdat – magnitude data

  • imaginary – phases data

Returns:

real and imaginary

Return type:

tuple

spectrum(rdat, idat, window, norm_type=Spectrum.IN_DB)#

real and imaginary data in ndarrays rl and im, returns (in rl) the spectrum thereof; window is the fft data window (a ndarray as returned by make_fft_window and type determines how the spectral data is scaled: spectrum.in_db= data in db, spectrum.normalized (default) = linear and normalized spectrum.raw = linear and un-normalized.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

  • window – fft window

  • norm_type – normalization type

Returns:

spectrum

Return type:

np.ndarray

autocorrelate(data)#

autocorrelation of data (a ndarray).

Parameters:

data – data

Returns:

autocorrelation result

Return type:

np.ndarray

correlate(data1, data2)#

cross-correlation of data1 and data2 (both ndarrays).

Parameters:
  • data1 – data 1

  • data2 – data 2

Returns:

correlation result

Return type:

np.ndarray

mus_fft(rdat, idat, fft_size, sign)#

in-place operation. return the fft of rl and im which contain the real and imaginary parts of the data; len should be a power of 2, dir = 1 for fft, -1 for inverse-fft.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

  • fft_size – must be power of two

  • sign – 1 for fft, -1 for inverse-fft

Returns:

result written into rdat

Return type:

np.ndarray

mus_rectangular2polar(rdat, idat)#

in-place operation. convert real/imaginary data in s rl and im from rectangular form (fft output) to polar form (a spectrum).

Parameters:
  • rdat – real data

  • imaginary – imaginary data

Returns:

magnitude written into rdat, idat contains phases

Return type:

np.ndarray

mus_rectangular2magnitudes(rdat, idat)#

in-place operation. convert real/imaginary data in rl and im from rectangular form (fft output) to polar form, but ignore the phases.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

Returns:

magnitude written into rdat

Return type:

np.ndarray

mus_polar2rectangular(rdat, idat)#

in-place operation. convert real/imaginary data in rl and im from polar (spectrum) to rectangular (fft).

Parameters:
  • rdat – magnitude data

  • imaginary – phases data

Returns:

real data written into rdat, idat contains imaginary

Return type:

np.ndarray

mus_convolution(rl1, rl2, fft_size=512)#

in-place operation. convolution of ndarrays v1 with v2, using fft of size len (a power of 2), result in v1.

Parameters:
  • rl1 – input data 1

  • rl2 – input data 2

  • fft_size – fft size

Returns:

convolved output. also written into rl1

Return type:

np.ndarray

mus_spectrum(rdat, idat, window, norm_type=Spectrum.IN_DB)#

in-place operation. real and imaginary data in ndarrays rl and im, returns (in rl) the spectrum thereof; window is the fft data window (a ndarray as returned by make_fft_window and type determines how the spectral data is scaled: spectrum.in_db= data in db, spectrum.normalized (default) = linear and normalized spectrum.raw = linear and un-normalized.

Parameters:
  • rdat – real data

  • imaginary – imaginary data

  • window – fft window

  • norm_type – normalization type

Returns:

spectrum

Return type:

np.ndarray

mus_autocorrelate(data)#

in place autocorrelation of data (a ndarray).

Parameters:

data – data

Returns:

autocorrelation result

Return type:

np.ndarray

mus_correlate(data1, data2)#

in place cross-correlation of data1 and data2 (both ndarrays).

Parameters:
  • data1 – data 1

  • data2 – data 2

Returns:

correlation result written into data1

Return type:

np.ndarray

mus_cepstrum(data)#

return cepstrum of signal

Parameters:

data – samples to analyze

Returns:

cepstrum. also written into data

Return type:

np.ndarray

Partials#

partials2wave(partials, wave=None, table_size=None, norm=True)#

take a list or np.ndarray of partials (harmonic number and associated amplitude) and produce a waveform for use in table_lookup.

Parameters:
  • partials – list or np.ndarray of partials (harm and amp)

  • wave – array to write wave into. if not provided, one will be allocated

  • table_size – size of table

  • norm – whether to normalize partials

Returns:

array provided in wave or new array.

Return type:

np.ndarray

phase_partials2wave(partials, wave=None, table_size=None, norm=True)#

take a list of partials (harmonic number, amplitude, initial phase) and produce a waveform for use in table_lookup.

Parameters:
  • partials – list or np.ndarray of partials (harm, amp, phase)

  • wave – array to write wave into. if not provided, one will be allocated

  • table_size – size of table

  • norm – whether to normalize partials

Returns:

array provided in wave or new array.

Return type:

np.ndarray

partials2polynomial(partials, kind=Polynomial.FIRST_KIND)#

returns a chebyshev polynomial suitable for use with the polynomial generator to create (via waveshaping) the harmonic spectrum described by the partials argument.

Parameters:
  • partials – list or np.ndarray of partials (harm and amp)

  • kind – Polynomial.EITHER_KIND, Polynomial.FIRST_KIND, Polynomial.SECOND_KIND, Polynomial.BOTH_KINDS

Returns:

chebyshev polynomial

Return type:

np.ndarray

normalize_partials(partials)#

scales the partial amplitudes in the list/array or list ‘partials’ by the inverse of their sum (so that they add to 1.0). creates new array

Parameters:

partials – list or np.ndarray of partials (harm and amp)

Returns:

normalized partials

Return type:

np.ndarray

Chebyshev#

chebyshev_tu_sum(x, tcoeffs, ucoeffs)#

returns the sum of the weighted chebyshev polynomials tn and un, with phase x

Parameters:
  • x – input

  • tcoeffs – tn

  • ucoeffs – un

Return type:

float

chebyshev_t_sum(x, tcoeffs)#

returns the sum of the weighted chebyshev polynomials tn

Parameters:
  • x – nput

  • tcoeffs – tn

Return type:

float

chebyshev_u_sum(x, ucoeffs)#

returns the sum of the weighted chebyshev polynomials un

Parameters:
  • x – input

  • ucoeffs – un

Return type:

float