pykanto.signal.filter#

Functions and methods to perform spectrogram filtering, dereverberating, bandpassing, etc.

Functions

dereverberate(spectrogram[, echo_range, ...])

Reduce echo in a spectrogram by subtracting a delayed version of itself.

dereverberate_jit(spectrogram[, echo_range, ...])

Reduce echo in a spectrogram by subtracting a delayed version of itself.

gaussian_blur(array[, gauss_sigma, max])

Gaussian blur a 2d numpy array, rescale to desired range to account for loss.

get_norm_spectral_envelope(mel_spectrogram, ...)

Returns a spectral envelope of sorts - useful to quickly characterise a song's frequency distribution.

hz_to_mel_lib(hz, minmax_freq, parameters)

Convert hz to mel frequencies

mel_to_hz(mel_bin, dataset)

Returns the original frequency from a mel bin index.

mels_to_hzs(dataset)

Returns the original frequencies from the mel bins used in a dataset.

norm(x)

Normalise a numpy array to [0,1].

normalise(S, min_level_db)

Normalise a numpy array to [0,1] and clip to min_level_db.

Classes

kernels()

Class holding kernels for use in filtering.

pykanto.signal.filter.dereverberate(spectrogram: numpy.ndarray, echo_range: int = 100, echo_reduction: float = 0.5, hop_length: int = 128, sr: int = 22050) numpy.ndarray[source]#

Reduce echo in a spectrogram by subtracting a delayed version of itself. Based in JS code from Robert Lachlan.

Parameters
  • spectrogram (np.ndarray) – Data to dereverberate.

  • echo_range (int, optional) – How many frames to dereverb. Defaults to 100.

  • echo_reduction (float, optional) – How much reduction to perform. Defaults to 0.5.

  • hop_length (int, optional) – Hop length used to create the spectrogram. Defaults to 128.

  • sr (int, optional) – Sampling ratio. Defaults to 22050.

Returns

De-echoed spectrogram.

Return type

np.ndarray

pykanto.signal.filter.dereverberate_jit(spectrogram: np.ndarray, echo_range: int = 100, echo_reduction: float = 0.5, hop_length: int = 128, sr: int = 22050) np.ndarray#

Reduce echo in a spectrogram by subtracting a delayed version of itself. Based in JS code from Robert Lachlan.

Parameters
  • spectrogram (np.ndarray) – Data to dereverberate.

  • echo_range (int, optional) – How many frames to dereverb. Defaults to 100.

  • echo_reduction (float, optional) – How much reduction to perform. Defaults to 0.5.

  • hop_length (int, optional) – Hop length used to create the spectrogram. Defaults to 128.

  • sr (int, optional) – Sampling ratio. Defaults to 22050.

Returns

De-echoed spectrogram.

Return type

np.ndarray

pykanto.signal.filter.get_norm_spectral_envelope(mel_spectrogram: numpy.ndarray, mindb: int, kernel_size: int = 5) numpy.ndarray[source]#

Returns a spectral envelope of sorts - useful to quickly characterise a song’s frequency distribution. Minmax rescaled to [0,1]

Parameters
  • mel_spectrogram (np.ndarray) – Source spectrogram

  • mindb (int) – Min value to threshold.

  • kernel_size (int, optional) – That. Defaults to 10.

Returns

[description]

Return type

np.ndarray

pykanto.signal.filter.gaussian_blur(array: numpy.ndarray, gauss_sigma: int = 3, max: int = 0)[source]#

Gaussian blur a 2d numpy array, rescale to desired range to account for loss.

Parameters
  • array (np.ndarray) – An array to be blurred

  • gauss_sigma (int, optional) – Standard deviation for Gaussian kernel. Defaults to 3.

  • max (int, optional) – Max value in returned array. Defaults to 0.

Returns

Blurred and interpolated array

Return type

np.ndarray

class pykanto.signal.filter.kernels[source]#

Class holding kernels for use in filtering.

erosion_kern = array([[0, 0, 0],        [1, 1, 1],        [0, 0, 0]])#
dilation_kern = array([[0, 1, 0],        [0, 1, 0],        [0, 1, 0],        [0, 1, 0],        [0, 1, 0]])#
pykanto.signal.filter.norm(x: np.ndarray) np.ndarray[source]#

Normalise a numpy array to [0,1].

Parameters

x (np.ndarray) – Array to normalise.

Returns

Normalised array.

Return type

np.ndarray

pykanto.signal.filter.normalise(S: numpy.ndarray, min_level_db: int) numpy.ndarray[source]#

Normalise a numpy array to [0,1] and clip to min_level_db.

Parameters
  • S (np.ndarray) – Array to normalise.

  • min_level_db (int) – Threshold, in relative dB.

Returns

Normalised and clipped array.

Return type

np.ndarray

pykanto.signal.filter.hz_to_mel_lib(hz: int, minmax_freq: Tuple[int, int], parameters)[source]#

Convert hz to mel frequencies

Parameters
  • hz (int) – [description]

  • minmax_freq (Tuple) – [description]

  • parameters ([type]) – [description]

Returns

[description]

Return type

[type]

pykanto.signal.filter.mel_to_hz(mel_bin: int, dataset: KantoData) int[source]#

Returns the original frequency from a mel bin index. Requires a KantoData object with set parameters.

Parameters
  • mel_bin (int) – Mel bin to convert

  • dataset (KantoData) – KantoData object

Returns

Approximate original frequency in hertzs

Return type

int

pykanto.signal.filter.mels_to_hzs(dataset: KantoData) np.ndarray[int][source]#

Returns the original frequencies from the mel bins used in a dataset. Requires a KantoData object with set parameters.

Parameters

dataset (KantoData) – KantoData object

Returns

Approximate original frequencies in hertzs

Return type

np.ndarray[int]