lazy_lpc Module

Linear Predictive Coding (LPC) module

Summary of module contents:

Name Description
ParCorError Error when trying to find the partial correlation coefficients (reflection coefficients) and there’s no way to find them.
toeplitz Find the toeplitz matrix as a list of lists given its first line/column.
levinson_durbin Solve the Yule-Walker linear system of equations.
lpc This is a StrategyDict instance object called lpc. Strategies stored: 5.
parcor Find the partial correlation coefficients (PARCOR), or reflection coefficients, relative to the lattice implementation of a given LTI FIR LinearFilter with a constant denominator (i.e., LPC analysis filter, or any filter without feedback).
parcor_stable Tests whether the given filter is stable or not by using the partial correlation coefficients (reflection coefficients) of the given filter.
lsf Find the Line Spectral Frequencies (LSF) from a given FIR filter.
lsf_stable Tests whether the given filter is stable or not by using the Line Spectral Frequencies (LSF) of the given filter. Needs NumPy.
exception ParCorError[source]

Bases: ZeroDivisionError

Error when trying to find the partial correlation coefficients (reflection coefficients) and there’s no way to find them.

toeplitz(vect)[source]

Find the toeplitz matrix as a list of lists given its first line/column.

levinson_durbin(acdata, order=None)[source]

Solve the Yule-Walker linear system of equations.

They’re given by:

\[R . a = r\]

where \(R\) is a simmetric Toeplitz matrix where each element are lags from the given autocorrelation list. \(R\) and \(r\) are defined (Python indexing starts with zero and slices don’t include the last element):

\[ \begin{align}\begin{aligned}R[i][j] = acdata[abs(j - i)]\\r = acdata[1 : order + 1]\end{aligned}\end{align} \]
Parameters:
  • acdata – Autocorrelation lag list, commonly the acorr function output.
  • order – The order of the resulting ZFilter object. Defaults to len(acdata) - 1.
Returns:

A FIR filter, as a ZFilter object. The mean squared error over the given data (variance of the white noise) is in its “error” attribute.

See also

acorr
Calculate the autocorrelation of a given block.
lpc
Calculate the Linear Predictive Coding (LPC) coefficients.
parcor
Partial correlation coefficients (PARCOR), or reflection coefficients, relative to the lattice implementation of a filter, obtained by reversing the Levinson-Durbin algorithm.
Examples:
>>> data = [2, 2, 0, 0, -1, -1, 0, 0, 1, 1]
>>> acdata = acorr(data)
>>> acdata
[12, 6, 0, -3, -6, -3, 0, 2, 4, 2]
>>> ldfilt = levinson_durbin(acorr(data), 3)
>>> ldfilt
1 - 0.625 * z^-1 + 0.25 * z^-2 + 0.125 * z^-3
>>> ldfilt.error # Squared! See lpc for more information about this
7.875

The Levinson-Durbin algorithm used to solve the equations needs \(O(order^2)\) floating point operations.

parcor(fir_filt)[source]

Find the partial correlation coefficients (PARCOR), or reflection coefficients, relative to the lattice implementation of a given LTI FIR LinearFilter with a constant denominator (i.e., LPC analysis filter, or any filter without feedback).

Parameters:fir_filt – A ZFilter object, causal, LTI and with a constant denominator.
Returns:A generator that results in each partial correlation coefficient from iterative decomposition, reversing the Levinson-Durbin algorithm.
Examples:
>>> filt = levinson_durbin([1, 2, 3, 4, 5, 3, 2, 1])
>>> filt
1 - 0.275 * z^-1 - 0.275 * z^-2 - 0.4125 * z^-3 + 1.5 * z^-4 - 0.9125 * z^-5 - 0.275 * z^-6 - 0.275 * z^-7
>>> round(filt.error, 4)
1.9125
>>> k_generator = parcor(filt)
>>> k_generator
<generator object parcor at ...>
>>> [round(k, 7) for k in k_generator]
[-0.275, -0.3793103, -1.4166667, -0.2, -0.25, -0.3333333, -2.0]

See also

levinson_durbin
Levinson-Durbin algorithm for solving Yule-Walker equations (Toeplitz matrix linear system).
parcor_stable(filt)[source]

Tests whether the given filter is stable or not by using the partial correlation coefficients (reflection coefficients) of the given filter.

Parameters:filt – A LTI filter as a LinearFilter object.
Returns:A boolean that is true only when all correlation coefficients are inside the unit circle. Critical stability (i.e., when outer coefficient has magnitude equals to one) is seem as an instability, and returns False.

See also

parcor
Partial correlation coefficients generator.
lsf_stable
Tests filter stability with Line Spectral Frequencies (LSF) values.
lsf(fir_filt)[source]

Find the Line Spectral Frequencies (LSF) from a given FIR filter.

Parameters:filt – A LTI FIR filter as a LinearFilter object.
Returns:A tuple with all LSFs in rad/sample, alternating from the forward prediction and backward prediction filters, starting with the lowest LSF value.
lsf_stable(filt)[source]

Tests whether the given filter is stable or not by using the Line Spectral Frequencies (LSF) of the given filter. Needs NumPy.

Parameters:filt – A LTI filter as a LinearFilter object.
Returns:A boolean that is true only when the LSF values from forward and backward prediction filters alternates. Critical stability (both forward and backward filters has the same LSF value) is seem as an instability, and returns False.

See also

lsf
Gets the Line Spectral Frequencies from a filter. Needs NumPy.
parcor_stable
Tests filter stability with partial correlation coefficients (reflection coefficients).

lazy_lpc.lpc StrategyDict

This is a StrategyDict instance object called lpc. Strategies stored: 5.

Strategy lpc.autocor (Default). Aliases available are lpc.acorr, lpc.autocorrelation, lpc.auto_correlation. Docstring starts with:

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using the Levinson-Durbin algorithm or Numpy pseudo-inverse for linear system solving, when needed.

Strategy lpc.covar. Aliases available are lpc.cov, lpc.covariance, lpc.ncovar, lpc.ncov, lpc.ncovariance. Docstring starts with:

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the covariance method, assuming a zero-mean stochastic process, using numpy.linalg.pinv as a linear system solver.

Strategy lpc.kautocor. Aliases available are lpc.kacorr, lpc.kautocorrelation, lpc.kauto_correlation. Docstring starts with:

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using the Levinson-Durbin algorithm.

Strategy lpc.kcovar. Aliases available are lpc.kcov, lpc.kcovariance. Docstring starts with:

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation is based on the covariance method, assuming a zero-mean stochastic process, finding the coefficients iteratively and greedily like the lattice implementation in Levinson-Durbin algorithm, although the lag matrix found from the given block don’t have to be toeplitz. Slow, but this strategy don’t need NumPy.

Strategy lpc.nautocor. Aliases available are lpc.nacorr, lpc.nautocorrelation, lpc.nauto_correlation. Docstring starts with:

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using numpy.linalg.pinv as a linear system solver.

Note

This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.

covar(blk, order=None)

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the covariance method, assuming a zero-mean stochastic process, using numpy.linalg.pinv as a linear system solver.

autocor(blk, order=None)

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using the Levinson-Durbin algorithm or Numpy pseudo-inverse for linear system solving, when needed.

Parameters:
  • blk – An iterable with well-defined length. Don’t use this function with Stream objects!
  • order – The order of the resulting ZFilter object. Defaults to len(blk) - 1.
Returns:

A FIR filter, as a ZFilter object. The mean squared error over the given block is in its “error” attribute.

Hint

See lpc.kautocor example, which should apply equally for this strategy.

See also

levinson_durbin
Levinson-Durbin algorithm for solving Yule-Walker equations (Toeplitz matrix linear system).
lpc.nautocor
LPC coefficients from linear system solved with Numpy pseudo-inverse.
lpc.kautocor
LPC coefficients obtained with Levinson-Durbin algorithm.
kcovar(blk, order=None)

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation is based on the covariance method, assuming a zero-mean stochastic process, finding the coefficients iteratively and greedily like the lattice implementation in Levinson-Durbin algorithm, although the lag matrix found from the given block don’t have to be toeplitz. Slow, but this strategy don’t need NumPy.

kautocor(blk, order=None)

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using the Levinson-Durbin algorithm.

Parameters:
  • blk – An iterable with well-defined length. Don’t use this function with Stream objects!
  • order – The order of the resulting ZFilter object. Defaults to len(blk) - 1.
Returns:

A FIR filter, as a ZFilter object. The mean squared error over the given block is in its “error” attribute.

Examples:
>>> data = [-1, 0, 1, 0] * 4
>>> len(data) # Small data
16
>>> filt = lpc.kautocor(data, 2)
>>> filt # The analysis filter
1 + 0.875 * z^-2
>>> filt.numerator # List of coefficients
[1, 0.0, 0.875]
>>> filt.error # Prediction error (squared!)
1.875

See also

levinson_durbin
Levinson-Durbin algorithm for solving Yule-Walker equations (Toeplitz matrix linear system).
lpc.autocor
LPC coefficients by using one of the autocorrelation method strategies.
lpc.nautocor
LPC coefficients from linear system solved with Numpy pseudo-inverse.
nautocor(blk, order=None)

Find the Linear Predictive Coding (LPC) coefficients as a ZFilter object, the analysis whitening filter. This implementation uses the autocorrelation method, using numpy.linalg.pinv as a linear system solver.

Parameters:
  • blk – An iterable with well-defined length. Don’t use this function with Stream objects!
  • order – The order of the resulting ZFilter object. Defaults to len(blk) - 1.
Returns:

A FIR filter, as a ZFilter object. The mean squared error over the given block is in its “error” attribute.

Hint

See lpc.kautocor example, which should apply equally for this strategy.

See also

lpc.autocor
LPC coefficients by using one of the autocorrelation method strategies.
lpc.kautocor
LPC coefficients obtained with Levinson-Durbin algorithm.