CWT - Continuous Wavelet Transform

class audioflux.CWT(num=84, radix2_exp=12, samplate=32000, low_fre=None, high_fre=None, bin_per_octave=12, wavelet_type=WaveletContinueType.MORSE, scale_type=SpectralFilterBankScaleType.OCTAVE, gamma=None, beta=None, is_padding=True)

Continuous Wavelet Transform (CWT)

Parameters
num: int

Number of frequency bins to generate, starting at low_fre.

radix2_exp: int

fft_length=2**radix2_exp

samplate: int

Sampling rate of the incoming audio.

low_fre: float or None

Lowest frequency.

  • Linear/Linsapce/Mel/Bark/Erb, low_fre>=0. default: 0.0

  • Octave/Log, low_fre>=32.703. default: 32.703(C1)

high_fre: float or None

Highest frequency. Default is 16000(samplate/2).

  • Linear is not provided, it is based on samplate / (2 ** radix2_exp).

  • Octave is not provided, it is based on musical pitch.

bin_per_octave: int

Number of bins per octave.

Only Octave must be provided.

Usually set to 12, 24 or 36.

scale_type: SpectralFilterBankScaleType

Spectral filter bank type. It determines the type of spectrogram.

See: type.SpectralFilterBankScaleType

wavelet_type: WaveletContinueType

Wavelet Type

See: type.WaveletContinueType

Note

Default gamma/beta values for different wavelet_types:

  • morse: gamma=3 beta=20

  • morlet: gamma=6 beta=2

  • bump: gamma=5 beta=0.6

  • paul: gamma 4

  • dog: gamma 2 beta 2; must even

  • mexican: beta 2

  • hermit: gamma 5 beta 2

  • ricker: gamma 4

gamma: float or None

gamma value

beta: float or None

beta value

is_padding: bool

Whether to use padding.

See also

BFT
NSGT
PWT

Examples

Read 220Hz audio data

>>> import audioflux as af
>>> audio_path = af.utils.sample_path('220')
>>> audio_arr, sr = af.read(audio_path)
>>> # CWT can only input fft_length data
>>> # For radix2_exp=12, then fft_length=4096
>>> audio_arr = audio_arr[..., :4096]

Create CWT object of Octave

>>> from audioflux.type import SpectralFilterBankScaleType, WaveletContinueType
>>> from audioflux.utils import note_to_hz
>>> obj = af.CWT(num=84, radix2_exp=12, samplate=sr,
>>>               low_fre=note_to_hz('C1'), bin_per_octave=12,
>>>               wavelet_type=WaveletContinueType.MORSE,
>>>               scale_type=SpectralFilterBankScaleType.OCTAVE)

Extract spectrogram

>>> import numpy as np
>>> spec_arr = obj.cwt(audio_arr)
>>> spec_arr = np.abs(spec_arr)

Show spectrogram plot

>>> import matplotlib.pyplot as plt
>>> from audioflux.display import fill_spec
>>> fig, ax = plt.subplots()
>>> img = fill_spec(spec_arr, axes=ax,
>>>                 x_coords=obj.x_coords(),
>>>                 y_coords=obj.y_coords(),
>>>                 x_axis='time', y_axis='log',
>>>                 title='CWT-Octave MORSE Spectrogram')
>>> fig.colorbar(img, ax=ax)
../_images/cwt-1.png

Methods

ccwt(data_arr)

Calculate continuous CWT spectrogram.

cwt(data_arr)

Calculate cwt spectrogram.

get_bin_band_arr()

Get bin band array

get_fre_band_arr()

Get an array of frequency bands of different scales.

x_coords()

Get the X-axis coordinate

y_coords()

Get the Y-axis coordinate

get_fre_band_arr()

Get an array of frequency bands of different scales. Based on the scale_type determination of the initialization.

Returns
out: np.ndarray [shape=(fre, )]
get_bin_band_arr()

Get bin band array

Returns
out: np.ndarray [shape=[n_bin,]]
cwt(data_arr)

Calculate cwt spectrogram.

Parameters
data_arr: np.ndarray [shape=(…, 2**radix2_exp)]

Input audio data. The data length must be 2**radix2_exp.

Returns
out: np.ndarray [shape=(…, fre, time), dtype=np.complex]

The matrix of CWT

ccwt(data_arr)

Calculate continuous CWT spectrogram.

Calculate the cwt every fft/2, then divide the x-axis of each cwt into four parts, and take the middle two parts for splicing (the first part will be spliced with the first part of the first cwt, and the tail will be spliced with the last part of the last cwt).

Parameters
data_arr: np.ndarray [shape=((…, n)]

Input audio data. The data length must be an integer multiple of (2**radix2_exp)//2.

Returns
out: np.ndarray [shape=(…, fre, time)]

The matrix of continuous CWT

y_coords()

Get the Y-axis coordinate

Returns
out: np.ndarray [shape=(fre,)]
x_coords()

Get the X-axis coordinate

Returns
out: np.ndarray [shape=(time,)]