Skip to main content

A library for calculating SNR and SNDR from power spectral density

Project description

MySNR Package

MySNR is a Python package designed to calculate Signal-to-Noise Ratio (SNR) and Signal-to-Noise and Distortion Ratio (SNDR) from Power Spectral Density (PSD). This package provides functions for analyzing frequency data, estimating signal power, harmonic power, and noise power, and computing SNR and SNDR metrics.

Installation

You can install MySNR using pip:

pip install mysnr

Usage

Importing the Package

To use the MySNR package, you first need to import it into your Python script or Jupyter notebook.

import mysnr

Example Code

Here's an example of how to use the MySNR package:

import numpy as np
from scipy.signal import periodogram
import matplotlib.pyplot as plt

# Generate a sample signal with noise and harmonics
N = int(1E6)    # total number of samples
T_samp = 100E-9 # sampling interval
f = 3.0E3       # fundamental signal frequency

signal_power = 0.5
harmonic_power = 0.045
noise_power = 0.1

t = np.linspace(0.0, N*T_samp, N, endpoint=False)                           # time vector
y = np.sqrt(signal_power * 2) * np.sin(f * 2.0 * np.pi*t)                  # base signal, power = 0.5 * 1**2 = 0.5
y += np.sqrt(harmonic_power * 2) * np.sin(2*f * 3.0 * np.pi *t)             # third harmonic, power = 0.5 * 0.3**2 = 0.045
y += np.random.normal(0, np.sqrt(noise_power), len(t))                      # add white noise over the full band

# Compute the PSD using periodogram
py_xf, py_yf = periodogram(x=y, fs=1/T_samp, window='hann', scaling='density')

# Find harmonics and calculate SNR and SNDR
harmonic_bins = mysnr.findBinnedHarmonics(py_yf, py_xf, f, 0.5/T_samp, 3)
signal_peak_bin = mysnr.freqToBin(py_xf, f)
signal_bins = mysnr.getIndizesAroundPeak(py_yf, signal_peak_bin)

print(f"Expected Signal Power: {signal_power}")
print(f"Calculated Signal power: {mysnr.bandpower(py_yf[signal_bins], tau=T_samp*N)}")

print(f"Expected Harm Power: {harmonic_power}")
print(f"Calculated Harmonic Power: {mysnr.bandpower(py_yf[harmonic_bins], tau=T_samp*N)}")

print(f"Expected SNR: {10*np.log10(signal_power/noise_power)}")
print(f"Calculated SNR: {10*np.log10(mysnr.snr(py_yf, py_xf, T_samp=T_samp, N=N, bw=None))}")

print(f"Expected SNDR: {10*np.log10(signal_power/(noise_power + harmonic_power))}")
print(f"Calculated SNDR: {10*np.log10(mysnr.sndr(py_yf, py_xf, T_samp=T_samp, N=N, bw=None))}")

# Plot the PSD
fig, fftax = plt.subplots()
py_yfdb = 10*np.log10(np.abs(py_yf))

fftax.semilogx(py_xf, py_yfdb)
fftax.semilogx(py_xf[harmonic_bins], py_yfdb[harmonic_bins])
fftax.semilogx(py_xf[signal_bins], py_yfdb[signal_bins])

plt.grid(which='minor', linestyle=':', linewidth=0.5, color='lightgray')  # Minor grid lines
plt.show()

Functions

  • bandpower(psd, mode='psd', tau=1): Estimate the bandpower from a power spectral density.
  • getIndizesAroundPeak(psd, peakIndex, searchWidth=1000): Find indices around a peak in the PSD.
  • freqToBin(fAxis, f): Convert a frequency to its corresponding bin index.
  • findBinnedHarmonics(psd, faxis, fund, bw, nHarmonics=6, aliased=False): Find binned harmonics of a fundamental frequency.
  • snr(psd, faxis, T_samp, N, bw=None): Calculate the Signal-to-Noise Ratio (SNR).
  • sndr(psd, faxis, T_samp, N, bw=None): Calculate the Signal-to-Noise and Distortion Ratio (SNDR).

Contributing

Contributions are welcome! If you have any suggestions or bug reports, please open an issue on the GitHub repository.

Acknowledgements:

This project is inspired by https://github.com/hrtlacek/SNR/blob/main/SNR.ipynb .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mysnr-0.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mysnr-0.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file mysnr-0.1.tar.gz.

File metadata

  • Download URL: mysnr-0.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mysnr-0.1.tar.gz
Algorithm Hash digest
SHA256 842cb59127792c272061d0fc9da4f9b1c4590d0db94e28bfc9a7985eb451dd11
MD5 e6180e0c4fb5ec0877f5a6b0d9c8a264
BLAKE2b-256 610f0d037d0c134e235611cba0d0ace4b405f14dff5ea3aa9b966917f6caad09

See more details on using hashes here.

File details

Details for the file mysnr-0.1-py3-none-any.whl.

File metadata

  • Download URL: mysnr-0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mysnr-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98ea65b1c15cd7da90d618420ea8db0da789b6ad8b84610ba37110fe4cc84c77
MD5 eb3bf8ca714d52e68e518448475ff30b
BLAKE2b-256 32fd9c5804c5ce764d73ae024a4aecb8cd15e2b8ac06d841f62a504bf0842f5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page