Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ratio conversions #130

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,67 @@
],
"cSpell.words": [
"Albersheim",
"Alouini",
"arange",
"arcsin",
"astype",
"atleast",
"awgn",
"baseband",
"beamwidth",
"BPSK",
"correlator",
"cumsum",
"downsample",
"downsampled",
"downsamples",
"downsampling",
"dtype",
"exponentials",
"feedforward",
"figsize",
"frombuffer",
"hexdump",
"intersymbol",
"isrealobj",
"issubdtype",
"linestyle",
"linewidth",
"lowpass",
"mathbb",
"matplotlib",
"multirate",
"ndarray",
"ndim",
"newaxis",
"Nyquist",
"overdamped",
"packbits",
"PAPR",
"passband",
"periodogram",
"periodograms",
"polyphase",
"Proakis",
"pyplot",
"QPSK",
"resampler",
"resamplers",
"savefig",
"scipy",
"sidelobes",
"sinc",
"stopband",
"unaliased",
"underdamped",
"unpackbits",
"upsampled",
"upsamples",
"upsampling"
"upsampling",
"xlabel",
"xlim",
"ylabel",
"ylim",
"Zadoff"
]
}
5 changes: 5 additions & 0 deletions docs/api/conversions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Decibels

.. python-apigen-group:: conversions-decibels

Ratios
------

.. python-apigen-group:: conversions-ratios

From $E_b/N_0$
--------------

Expand Down
98 changes: 98 additions & 0 deletions src/sdr/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,104 @@ def linear(
raise ValueError(f"Argument 'type' must be 'value', 'power', or 'voltage', not {type!r}.")


##############################################################################
# Ratios
##############################################################################


@export
def percent(x: npt.ArrayLike) -> np.ndarray:
r"""
Converts from a ratio to a percentage.

Arguments:
x: The input ratio.

Returns:
The percentage.

Examples:
Convert 0.5 to 50%.

.. ipython:: python

sdr.percent(0.5)

Convert 0.25 to 25%.

.. ipython:: python

sdr.percent(0.25)

Group:
conversions-ratios
"""
x = np.asarray(x)
return 100 * x


@export
def ppm(x: npt.ArrayLike) -> np.ndarray:
r"""
Converts from a ratio to parts per million (ppm).

Arguments:
x: The input ratio.

Returns:
The parts per million (ppm).

Examples:
Convert 0.005 to 5000 ppm.

.. ipython:: python

sdr.ppm(0.005)

Convert 0.000025 to 25 ppm.

.. ipython:: python

sdr.ppm(0.000025)

Group:
conversions-ratios
"""
x = np.asarray(x)
return 1e6 * x


@export
def ppb(x: npt.ArrayLike) -> np.ndarray:
r"""
Converts from a ratio to parts per billion (ppb).

Arguments:
x: The input ratio.

Returns:
The parts per billion (ppb).

Examples:
Convert 0.000005 to 5000 ppb.

.. ipython:: python

sdr.ppb(0.000005)

Convert 0.000000025 to 25 ppb.

.. ipython:: python

sdr.ppb(0.000000025)

Group:
conversions-ratios
"""
x = np.asarray(x)
return 1e9 * x


##############################################################################
# From Eb/N0
##############################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/sdr/_link_budget/_antenna.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@export
def wavelength(freq: float) -> float:
r"""
Calculates the wavelength $\lambda$ of a electromagnetic wave with frequency $f$.
Calculates the wavelength $\lambda$ of an electromagnetic wave with frequency $f$.

$$\lambda = \frac{c}{f}$$

Expand Down
15 changes: 15 additions & 0 deletions tests/conversions/test_percent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np

import sdr


def test_0_5():
assert sdr.percent(0.5) == 50


def test_0_25():
assert sdr.percent(0.25) == 25


def test_array():
assert np.array_equal(sdr.percent(np.array([0.5, 0.25])), np.array([50, 25]))
15 changes: 15 additions & 0 deletions tests/conversions/test_ppb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np

import sdr


def test_0_00005():
assert sdr.ppb(0.000005) == 5000


def test_0_00000025():
assert sdr.ppb(0.000000025) == 25


def test_array():
assert np.array_equal(sdr.ppb(np.array([0.000005, 0.000000025])), np.array([5000, 25]))
15 changes: 15 additions & 0 deletions tests/conversions/test_ppm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np

import sdr


def test_0_005():
assert sdr.ppm(0.005) == 5000


def test_0_000025():
assert sdr.ppm(0.000025) == 25


def test_array():
assert np.array_equal(sdr.ppm(np.array([0.005, 0.000025])), np.array([5000, 25]))