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 sdr.power() #409

Merged
merged 2 commits into from
Jul 11, 2024
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: 31 additions & 0 deletions src/sdr/_measurement/_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@
from .._helper import convert_output, export, verify_arraylike, verify_bool


@export
def power(
x: npt.ArrayLike,
db: bool = False,
) -> float:
r"""
Measures the instantaneous power of a time-domain signal $x[n]$.

$$P = \left| x[n] \right|^2$$

Arguments:
x: The time-domain signal $x[n]$ to measure.
db: Indicates whether to return the result in decibels (dB).

Returns:
The instantaneous power. If `db=False`, $P$ is returned.
If `db=True`, $10 \log_{10} P$ is returned.

Group:
measurement-power
"""
x = verify_arraylike(x, complex=True)
verify_bool(db)

P = np.abs(x) ** 2
if db:
P = to_db(P, type="power")

return convert_output(P)


@export
def peak_power(
x: npt.ArrayLike,
Expand Down
8 changes: 8 additions & 0 deletions tests/measurements/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
import sdr


def test_power():
rng = np.random.default_rng()
A = rng.uniform(2, 10) # Signal amplitude
N = 50
x = A * np.exp(1j * np.linspace(0, 2 * np.pi, N, endpoint=False))
assert np.allclose(sdr.power(x), A**2)


def test_peak_power():
rng = np.random.default_rng()
A = rng.uniform(2, 10) # Signal amplitude
Expand Down
Loading