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
import mysnr
plt.rcParams['lines.marker'] = 'x'
plt.rcParams['axes.grid'] = True
T_samp = 100E-9 # sampling interval
f = 3.0E3 # fundamental signal frequency
k = 5 # number of periods in timeframe
N = int(k/(f * T_samp)) # total number of samples
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
py_xf, py_yf = periodogram(x=y, fs=1/T_samp, window='boxcar', scaling='density')
#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)
snr, signal_bins = mysnr.snr(py_yf, py_xf, T_samp=T_samp, N=N, bw=None, return_bins=True)
sndr, signal_bins, harmonic_bins = mysnr.sndr(py_yf, py_xf, T_samp=T_samp, N=N, bw=None, return_bins=True)
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(snr)}")
print(f"Expected SNDR: {10*np.log10(signal_power/(noise_power + harmonic_power))}")
print(f"Calculated SNDR: {10*np.log10(sndr)}")
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], linestyle='')
fftax.semilogx(py_xf[signal_bins], py_yfdb[signal_bins], linestyle='')
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mysnr-0.2.tar.gz.
File metadata
- Download URL: mysnr-0.2.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cace75dbc10c3f0f3f6c506e6638cda1c104dadb1ea3d25c2f7340c032df2e4
|
|
| MD5 |
4bbe3102c0c892f0db9df11507c9d7d2
|
|
| BLAKE2b-256 |
24c0999079f22ebfce2a823f1d5abe41da614281418a9e73107564586a58cb1a
|
File details
Details for the file mysnr-0.2-py3-none-any.whl.
File metadata
- Download URL: mysnr-0.2-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7665c5f0ebc41f3827f8b5e7a4413392d17edbe4ff421db1e9ba4a3c5e9b46c7
|
|
| MD5 |
2bbb27e7c3629d998af0e81db557fab0
|
|
| BLAKE2b-256 |
a336800be1cd3cf4d627069d32fefaac303c77e00b39dde0f78c53ed11b9cdb7
|