KSVD Dictionary Learning

This module implements the K-SVD dictionary learning algorithm, along with related functions.

KSVD.py - K-SVD algorithm for dictionary learning and sparse coding using Orthogonal Matching Pursuit (OMP).

This module implements the K-SVD algorithm, which is widely used for dictionary learning. The algorithm iteratively updates dictionary elements to best represent a set of input signals. This module also includes utility functions to update dictionary elements using Singular Value Decomposition (SVD) and handle redundant dictionary elements.

Example usage:

>>> from KSVD import KSVD
>>> data = np.random.randn(100, 200)  # Random signals
>>> params = {'K': 50, 'num_iterations': 10, 'initialization_method': 'DataElements', 'L': 5, 'preserve_dc_atom': 0}
>>> dictionary, coefficients = KSVD(data, params)
CompSensePack.dictionaries.KSVD.I_clearDictionary(dictionary, coeff_matrix, data)[source]

Clear or replace redundant dictionary elements.

Parameters

dictionarynumpy.ndarray

The dictionary matrix to be updated.

coeff_matrixnumpy.ndarray

The coefficient matrix representing the sparse codes for the data.

datanumpy.ndarray

The original data matrix.

Returns

dictionarynumpy.ndarray

The updated dictionary with redundant elements replaced.

CompSensePack.dictionaries.KSVD.I_findBetterDictionaryElement(data, dictionary, j, coeff_matrix, numCoefUsed=1)[source]

Update the j-th dictionary element using the current sparse representation.

Parameters

datanumpy.ndarray

The data matrix (n x N), where n is the signal dimension and N is the number of signals.

dictionarynumpy.ndarray

The current dictionary matrix (n x K), where K is the number of dictionary atoms.

jint

The index of the dictionary element to be updated.

coeff_matrixnumpy.ndarray

The sparse coefficient matrix (K x N), representing the sparse codes for the signals.

numCoefUsedint, optional (default=1)

The number of coefficients used in the sparse representation.

Returns

betterDictionaryElementnumpy.ndarray

The updated dictionary element (vector).

coeff_matrixnumpy.ndarray

The updated coefficient matrix with the new coefficients for the updated dictionary element.

newVectAddedint

A flag indicating if a new vector was added to the dictionary.

CompSensePack.dictionaries.KSVD.KSVD(data, param)[source]

K-SVD algorithm for dictionary learning.

The K-SVD algorithm is an iterative method for updating the dictionary and sparse coefficients to best represent the input signals. The dictionary is updated using the singular value decomposition (SVD) of the data approximation error.

Parameters

datanumpy.ndarray

The data matrix (n x N) containing N signals of dimension n.

paramdict

A dictionary containing parameters for the K-SVD algorithm: - ‘K’: int, the number of dictionary atoms. - ‘num_iterations’: int, the number of iterations to run the K-SVD algorithm. - ‘initialization_method’: str, how to initialize the dictionary (‘DataElements’ or ‘GivenMatrix’). - ‘initial_dictionary’: numpy.ndarray, the initial dictionary (if ‘GivenMatrix’ is used). - ‘L’: int, the number of non-zero coefficients for sparse coding (used in OMP). - ‘preserve_dc_atom’: int, flag to preserve a DC atom (default is 0).

Returns

dictionarynumpy.ndarray

The learned dictionary of size (n x K).

coef_matrixnumpy.ndarray

The sparse coefficient matrix (K x N), representing the sparse representation of the data.

CompSensePack.dictionaries.KSVD.svds_vector(v)[source]

Handle SVD for a vector or a 2D matrix with one dimension equal to 1.

Parameters

vnumpy.ndarray

Input vector or 2D matrix with one dimension equal to 1.

Returns

unumpy.ndarray

The left singular vector (normalized).

sfloat

The singular value (the norm of the vector).

vtnumpy.ndarray

The right singular vector, which is always [[1]] for vectors.

Raises

ValueError

If the input is not a vector or a 2D array with one dimension equal to 1.