Evaluation and Plotting

This module includes functions for evaluating and plotting signal results.

eval.py - Functions for Signal-to-Noise Ratio (SNR) calculation and signal plotting.

This module provides utility functions for: 1. Calculating the Signal-to-Noise Ratio (SNR) between an original and a reconstructed signal. 2. Plotting the original and reconstructed signals together, with options to display the SNR and save the plot.

Note:

  • Requires matplotlib and numpy.

  • Useful for comparing signals and visualizing differences in various signal processing applications.

Example usage:

>>> import numpy as np
>>> from eval import calculate_snr, plot_signals
>>> original_signal = np.random.rand(100)
>>> reconstructed_signal = np.random.rand(100)
>>> snr = calculate_snr(original_signal, reconstructed_signal)
>>> plot_signals(original_signal, reconstructed_signal, snr=snr, save_path='./plots')
CompSensePack.eval.calculate_snr(signal, recovered_signal)[source]

Calculates the Signal-to-Noise Ratio (SNR) between the original signal and the recovered signal.

Parameters

signalnumpy.ndarray

The original signal.

recovered_signalnumpy.ndarray

The recovered signal after some processing or recovery algorithm.

Returns

snrfloat

The Signal-to-Noise Ratio (SNR) in decibels (dB).

Notes

  • The SNR is calculated as 20 * log10(norm(signal) / norm(signal - recovered_signal)).

  • A higher SNR value indicates a better recovery, with less error relative to the original signal.

  • If the signals are identical, the SNR would be infinite. If the recovered signal has no similarity, the SNR will be very low or negative.

Example

>>> original = np.random.rand(100)
>>> recovered = original + np.random.normal(0, 0.1, 100)
>>> snr = calculate_snr(original, recovered)
>>> print(f"SNR: {snr:.2f} dB")
CompSensePack.eval.plot_signals(original_signal, reconstructed_signal, snr=None, original_name='Original Signal', reconstructed_name='Reconstructed Signal', save_path=None, filename=None, start_pct=0.0, num_samples=None, show_snr_box=False)[source]

Plots a section of the original signal and the reconstructed signal on the same plot with the given names, displays the Signal-to-Noise Ratio (SNR) in a text box if enabled, and saves the plot to a specified directory.

Parameters

original_signalnumpy.ndarray

The original signal to be plotted.

reconstructed_signalnumpy.ndarray

The reconstructed signal to be plotted.

snrfloat, optional (default=None)

The Signal-to-Noise Ratio to display. If None, it will be computed using the original and reconstructed signals.

original_namestr, optional (default=”Original Signal”)

The name to display for the original signal in the plot.

reconstructed_namestr, optional (default=”Reconstructed Signal”)

The name to display for the reconstructed signal in the plot.

save_pathstr, optional

The directory path where the plot should be saved. If None, the plot will not be saved.

filenamestr, optional

The name of the file to save the plot as. If None and save_path is provided, a default name will be generated.

start_pctfloat, optional (default=0.0)

The percentage (between 0 and 1) of the way through the signal to start plotting. For example, 0.5 means start from the halfway point of the signals.

num_samplesint, optional (default=None)

The number of samples to plot from the start point. If None, it will plot to the end of the signals.

show_snr_boxbool, optional (default=False)

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

Returns

None

This function does not return any value. It either displays or saves the plot.

Notes

  • Ensure the original and reconstructed signals have the same length; otherwise, a ValueError will be raised.

  • The plot shows a section of the signals starting at start_pct and plots num_samples samples.

  • The SNR can be displayed in a text box if show_snr_box=True and the SNR value is provided or calculated.

Example

>>> original = np.sin(np.linspace(0, 10, 100))
>>> reconstructed = original + np.random.normal(0, 0.1, 100)
>>> plot_signals(original, reconstructed, snr=20, save_path='./plots')