LinearAxis

class ketos.audio.utils.axis.LinearAxis(bins, extent, label=None)[source]

Linear axis.

Args:
bins: int

Number of bins

extent: tuple(float,float)

Axis range, e.g. (0., 100.)

label: str

Descriptive label. Optional

Attributes:
bins: int

Number of bins

x_min: float

Left edge of first bin

dx: float

Bin width

label: str

Descriptive label.

Methods

bin(x[, truncate, closed_right])

Get bin number corresponding to a given value.

low_edge(b)

Get the lower-edge value of a given bin.

resize(bins)

Resize the axis.

zero_offset()

Shift axis lower boundary to zero.

bin(x, truncate=False, closed_right=False)[source]

Get bin number corresponding to a given value.

By default bins are closed on the left and open on the right, i.e., [a,b). Use the argument closed_right to reverse this.

If the value lies outside the axis range, a negative bin number or a bin number above N-1 will be returned. This behaviour can be changed using the argument ‘truncate’.

Args:
x: array-like

Value

truncate: bool

Return 0 if x is below the lower axis boundary and N-1 if x is above the upper boundary. Default is False.

closed_right: bool

If False, bin is closed on the left and open on the right. If True, bin is open on the left and closed on the right. Default is False.

Returns:
b: array-like

Bin number

Example:
>>> from ketos.audio.utils.axis import LinearAxis
>>> #Linear axis between 0. and 100. with 200 bins.
>>> ax = LinearAxis(bins=200, extent=(0.,100.))
>>> #Get bin number corresponding to x=0.6
>>> b = ax.bin(0.6)
>>> print(b)
1
>>> #Get several bin numbes in one call 
>>> b = ax.bin([0.6,11.1])
>>> print(b)
[ 1 22]
>>> #Get bin number for values at bin edges
>>> b = ax.bin([0.0,0.5,1.0,100.])
>>> print(b)
[  0   1   2 199]
>>> #Note that when the value sits between two bins, 
>>> #the higher bin number is returned.
>>> #This behaviour can be reversed using the closed_right 
>>> #argument,
>>> b = ax.bin([0.0,0.5,1.0,100.], closed_right=True)
>>> print(b)
[  0   0   1 199]
>>> #Note that the lower edge of the first bin and the 
>>> #upper edge of the last bin are special cases: for 
>>> #these values, the first (0) and last (199) bin 
>>> #numbers are always returned.
>>> #Get bin numbers outside the axis range
>>> b = ax.bin([-2.1, 100.1])
>>> print(b)
[ -5 200]
>>> b = ax.bin([-2.1, 100.1], truncate=True)
>>> print(b)
[  0 199]
low_edge(b)[source]

Get the lower-edge value of a given bin.

Args:
b: array-like

Bin number.

Returns:
x: array-like

Lower-edge bin value

Example:
>>> from ketos.audio.utils.axis import LinearAxis
>>> #Linear axis between 12. and 22. with 5 bins.
>>> ax = LinearAxis(bins=5, extent=(12.,22.))
>>> #Get lower-edge values of bins 1 and 4
>>> x = ax.low_edge([1,4])
>>> print(x)
[14. 20.]
resize(bins)[source]

Resize the axis.

This operation changes the number of bins, but preserves the axis range.

Args:
bins: int

Number of bins

zero_offset()[source]

Shift axis lower boundary to zero.