Compressed Sensing Class

This module contains the main compressed sensing class which integrates various dictionary learning and signal recovery methods.

comp_sense_class.py - Compressed Sensing Class

This module provides a compressedSensing class that implements a framework for performing compressed sensing using various measurement matrices, dictionary learning methods, and sparse signal recovery algorithms.

The class includes methods for signal division, compression, dictionary generation, signal recovery, and plotting of reconstructed signals. The SL0 algorithm is used for sparse signal recovery, and dictionary learning can be performed using DCT, MOD, or K-SVD.

Dependencies:
  • NumPy

  • CompSensePack modules (measurement_matrix, dictionaries, SL0, eval)

class CompSensePack.comp_sense_class.compressedSensing(signal, BLOCK_LEN=16, CR=4, matrix_type='gaussian')[source]

Bases: object

A class to perform compressed sensing, including signal compression, dictionary learning, and signal recovery.

The compressedSensing class provides an interface to apply compressed sensing on a given signal using a variety of measurement matrices and dictionary learning techniques, followed by signal recovery using the SL0 algorithm.

Parameters

signalnumpy.ndarray

The input signal, must be a valid array of real numbers.

BLOCK_LENint, optional (default=16)

The number of rows in the measurement matrix Phi.

CRint, optional (default=4)

Compression ratio (controls the number of rows in Phi). Must be a positive integer and BLOCK_LEN / CR > 1.

matrix_typestr, optional (default=’gaussian’)

Type of the matrix to generate (‘gaussian’, ‘DBBD’, etc.).

Attributes

signalnumpy.ndarray

The original input signal.

BLOCK_LENint

The number of rows in the measurement matrix.

CRint

Compression ratio.

COMP_LENint

Compression length (number of rows in Phi).

Phinumpy.ndarray

The measurement matrix used for compression.

original_phinumpy.ndarray

The original measurement matrix used for compression.

training_setnumpy.ndarray or None

The training set extracted from the original signal.

training_matrixnumpy.ndarray or None

The reshaped matrix for training.

test_setnumpy.ndarray or None

The test set extracted from the original signal.

Ynumpy.ndarray or None

The compressed version of the test set.

dictionarynumpy.ndarray or None

The generated dictionary for signal recovery.

coeff_matrixnumpy.ndarray or None

Coefficient matrix generated from MOD or K-SVD.

reconstructed_signalnumpy.ndarray or None

The reconstructed signal after SL0 recovery.

is_kronbool

Whether the Kronecker compression has been activated.

Y_kron()[source]

Reshapes the compressed signal Y into its Kronecker form by concatenating multiple columns.

Raises

ValueError

If the Kronecker factor (KRON_FACT) is not set or if the signal hasn’t been compressed.

clear()[source]

Resets the class to its state after instantiation.

This method resets attributes like BLOCK_LEN, COMP_LEN, Phi, and clears any training set, coefficient matrix, and reconstructed signal from the object.

compress_test_set()[source]

Compresses the test set using the original measurement matrix (original_phi).

This method applies the measurement matrix Phi to the test set to generate the compressed signal Y.

Raises

RuntimeError

If the test set has already been compressed.

ValueError

If the test set is not defined (i.e., if divide_signal() hasn’t been called).

divide_signal(training_percentage)[source]

Divides the signal into a training set and a test set based on the given percentage.

This method splits the original signal into a training set and a test set. The training set is then reshaped into a matrix to be used for dictionary learning.

Parameters

training_percentagefloat

The percentage of the signal to be used for training (between 0 and 1).

Raises

ValueError

If the size of the training set is smaller than the block length.

extract_model()[source]

Extracts the current state of the model for future recovery.

This method creates a snapshot of the important model parameters, including the measurement matrix (Phi), dictionary, Kronecker compression status, and the original measurement matrix (original_phi).

Returns

modeldict

A dictionary containing the following keys: - ‘phi’ : numpy.ndarray

The current measurement matrix being used (self.Phi).

  • ‘dict’numpy.ndarray or None

    The generated dictionary used for signal recovery (self.dictionary).

  • ‘is_kron’bool

    A flag indicating whether Kronecker compression is activated.

  • ‘original_phi’numpy.ndarray

    The original measurement matrix before any Kronecker adjustments were made.

generate_dictionary(dictionary_type='dct', mod_params=None, ksvd_params=None)[source]

Generates the dictionary based on the specified type (DCT, MOD, or K-SVD).

Parameters

dictionary_typestr

The type of dictionary to generate (‘dct’, ‘mod’, ‘ksvd’).

mod_paramsdict, optional

Dictionary of parameters for the MOD algorithm, if using MOD.

ksvd_paramsdict, optional

Dictionary of parameters for the K-SVD algorithm, if using K-SVD.

Raises

ValueError

If the training matrix is not defined or if the necessary parameters for MOD/K-SVD are not provided.

get_coeff_matrix()[source]

Retrieves the coefficient matrix generated by MOD or K-SVD.

Raises

ValueError

If the coefficient matrix has not been generated.

get_compressed_signal()[source]

Retrieves the compressed signal Y.

get_dictionary()[source]

Retrieves the generated dictionary.

get_measurement_matrix()[source]

Retrieves the measurement matrix Phi.

get_original_signal()[source]

Retrieves the original test signal that was passed to the class.

get_reconstructed_signal()[source]

Retrieves the reconstructed signal after SL0 recovery.

Raises

ValueError

If the signal has not been reconstructed.

get_snr()[source]

Computes and returns the Signal-to-Noise Ratio (SNR) between the original test set and the reconstructed signal.

Raises

ValueError

If the test set or reconstructed signal is not found.

get_test_set()[source]

Retrieves the test set.

Raises

ValueError

If the test set is not defined.

get_theta()[source]

Retrieves theta (Phi @ dictionary).

Raises

ValueError

If theta has not been computed yet.

get_theta_pinv()[source]

Retrieves theta_pinv (pseudoinverse of Phi @ dictionary).

Raises

ValueError

If theta_pinv has not been computed yet.

get_training_set()[source]

Retrieves the training set.

Raises

ValueError

If the training set is not defined.

kronecker_activate(KRON_FACT)[source]

Activates Kronecker compression, adjusting BLOCK_LEN and reprocessing Phi and Y.

Parameters

KRON_FACTint

The Kronecker factor to use for compression.

Raises

ValueError

If Kronecker compression has already been activated or if the signal hasn’t been compressed.

plot_reconstructed_vs_original(save_path=None, filename=None, start_pct=0.0, num_samples=None, reconstructed_label='Reconstructed Signal', show_snr_box=False)[source]

Plots the original test set against the reconstructed signal, with an option to display the SNR.

Parameters

save_pathstr, optional

Directory path where the plot should be saved.

filenamestr, optional

Name of the file to save the plot as.

start_pctfloat, optional (default=0.0)

The percentage of the way through the signal to start plotting.

num_samplesint, optional

The number of samples to plot from the start point.

reconstructed_labelstr, optional (default=”Reconstructed Signal”)

Label for the reconstructed signal in the plot.

show_snr_boxbool, optional (default=False)

Whether to display the SNR value in a text box on the plot.

Raises

ValueError

If the reconstructed signal or test set is not found.

recover_signal(sl0_params=None)[source]

Recovers the original signal using the SL0 algorithm after compressing the test set.

Parameters

sl0_paramsdict, optional

Parameters for the SL0 algorithm, including ‘sigma_min’, ‘sigma_decrease_factor’, ‘mu_0’, ‘L’, and ‘showProgress’.

Raises

ValueError

If the test set has not been compressed or if the dictionary has not been generated.