Skip to content

snr

snr

Signal-to-noise ratio (SNR) calculation.

signal_to_noise_ratio

signal_to_noise_ratio(signal: ndarray, dwell_time: float, window_width: float | int = 3000) -> float

Calculate the signal to noise ratio in dB.

Parameters:

  • signal (ndarray) –

    Centered spectrum of the acquired signal

  • dwell_time (float) –

    Dwell-time of the acquired signal

  • window_width (float | int, default: 3000 ) –

    Estimated window width of the signal in Hz

Returns:

  • Signal to noise ratio in dB –
Source code in src/console/utilities/snr.py
def signal_to_noise_ratio(signal: np.ndarray, dwell_time: float, window_width: float | int = 3000) -> float:
    """Calculate the signal to noise ratio in dB.

    Parameters
    ----------
    signal
        Centered spectrum of the acquired signal
    dwell_time
        Dwell-time of the acquired signal
    window_width
        Estimated window width of the signal in Hz

    Returns
    -------
        Signal to noise ratio in dB
    """
    peak = np.max(np.abs(signal[..., :]))
    fft_freq = np.fft.fftshift(np.fft.fftfreq(signal.shape[-1], dwell_time))

    # Define start and end indices of the window
    half_width = int(window_width / 2)
    peak_position = np.argmax(np.abs(signal[..., :]))
    left_window_idx = np.argmin(np.abs(fft_freq + fft_freq[peak_position] + half_width))
    right_window_idx = np.argmin(np.abs(fft_freq + fft_freq[peak_position] - half_width))

    # Extract pure noise from outside the window containing the peak
    noise = np.concatenate((signal[..., :left_window_idx], signal[..., right_window_idx:]))

    # Return snr in dB
    return 20 * np.log10(peak / np.mean(np.abs(noise)))