Skip to content

Commit

Permalink
Add sdr.euclidean()
Browse files Browse the repository at this point in the history
Closes #64
  • Loading branch information
mhostetter committed Nov 19, 2023
1 parent 54aef53 commit 4811531
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api/measurement.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Voltage

.. python-apigen-group:: measurement-voltage

Distance
--------

.. python-apigen-group:: measurement-distance

Modulation
----------

Expand Down
1 change: 1 addition & 0 deletions src/sdr/_measurement/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
A subpackage for various measurement routines.
"""
from ._distance import *
from ._energy import *
from ._errors import *
from ._modulation import *
Expand Down
38 changes: 38 additions & 0 deletions src/sdr/_measurement/_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
A module containing various distance measurement functions.
"""
from __future__ import annotations

import numpy as np
import numpy.typing as npt

from .._helper import export


@export
def euclidean(
x: npt.NDArray,
y: npt.NDArray,
axis: int | tuple[int, ...] | None = None,
) -> npt.NDArray:
r"""
Measures the Euclidean distance between two signals $x[n]$ and $y[n]$.
$$d = \sqrt{\sum_{n=0}^{N-1} \left| x[n] - y[n] \right|^2}$$
Arguments:
x: The time-domain signal $x[n]$.
y: The time-domain signal $y[n]$.
axis: Axis or axes along which to compute the distance. The default is `None`, which computes the distance
across the entire array.
Returns:
The Euclidean distance between $x[n]$ and $y[n]$.
Group:
measurement-distance
"""
x = np.asarray(x)
y = np.asarray(y)
d = np.sqrt(np.sum(np.abs(x - y) ** 2, axis=axis))
return d

0 comments on commit 4811531

Please sign in to comment.