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.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.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mysnr-0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 554d14586cb973d8d48922d693487f2f06dca6348942b113e02a4b660114c73e
MD5 f98ffe083ffe1e25ae0a300f7eca45fa
BLAKE2b-256 ae5a5af91f42b0ddca96ab543a0e264043cf9d8295e08dc35718b9f3243d2fe4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mysnr-0.1.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7309a9a5e820d3aac69b82468ba84f10ade2dfb87fe8863fdfb3a62da8cb3d73
MD5 5516123bdf35f6a25cd1b3ff6bb70298
BLAKE2b-256 b27370c19b2c9ea630d31792db0d66dc3ce538409e10f8c393b349f55103a0f2

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