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

Update examples #190

Merged
merged 5 commits into from
Dec 6, 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
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
],
"python.testing.unittestEnabled": false,
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
// "source.fixAll": true,
"source.organizeImports": true
},
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
// "source.fixAll": true,
"source.organizeImports": true
},
},
Expand Down
63 changes: 25 additions & 38 deletions docs/examples/farrow-resampler.ipynb

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions docs/examples/fir-filters.ipynb

Large diffs are not rendered by default.

135 changes: 49 additions & 86 deletions docs/examples/iir-filters.ipynb

Large diffs are not rendered by default.

55 changes: 22 additions & 33 deletions docs/examples/peak-to-average-power.ipynb

Large diffs are not rendered by default.

112 changes: 58 additions & 54 deletions docs/examples/phase-locked-loop.ipynb

Large diffs are not rendered by default.

121 changes: 49 additions & 72 deletions docs/examples/psk.ipynb

Large diffs are not rendered by default.

107 changes: 33 additions & 74 deletions docs/examples/pulse-shapes.ipynb

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ universal = false
[tool.ruff]
src = ["src"]
extend-include = ["*.ipynb"]
extend-exclude = ["build", "dist", "docs", "src/sdr/_version.py"]
extend-exclude = ["build", "dist", "src/sdr/_version.py"]
line-length = 120

[tool.ruff.lint]
exclude = ["docs/*"]
select = [
"E", # pycodestyle
"F", # Pyflakes
Expand Down
39 changes: 37 additions & 2 deletions src/sdr/_synchronization/_nco.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"""
from __future__ import annotations

from typing import Any, overload

import numpy as np
import numpy.typing as npt
from typing_extensions import Literal

from .._helper import export

Expand Down Expand Up @@ -120,11 +123,30 @@ def reset(self):
"""
self._z_prev = -self.increment

@overload
def __call__(
self,
freq: npt.NDArray[np.float_] | None = None,
phase: npt.NDArray[np.float_] | None = None,
output: Literal["phase", "sine", "cosine"] = "complex-exp",
) -> npt.NDArray[np.float_]:
...

@overload
def __call__(
self,
freq: npt.NDArray[np.float_] | None = None,
phase: npt.NDArray[np.float_] | None = None,
output: Literal["complex-exp"] = "complex-exp",
) -> npt.NDArray[np.complex_]:
...

def __call__(
self,
freq: Any = None,
phase: Any = None,
output: Any = "complex-exp",
) -> Any:
"""
Steps the NCO with variable frequency and/or phase signals.

Expand All @@ -133,9 +155,11 @@ def __call__(
phase increment of the NCO. If `None`, the signal is all zeros.
phase: The variable phase signal $p[n]$ in radians. This input signal varies the per-sample phase offset
of the NCO. If `None`, the signal is all zeros.
output: The format of the output signal $y[n]$. Options are the accumulated phase, sine, cosine, or
complex exponential.

Returns:
The output complex signal $y[n]$.
The output signal $y[n]$.

Examples:
See the :ref:`phase-locked-loop` example.
Expand Down Expand Up @@ -163,7 +187,18 @@ def __call__(
# Add the absolute offset to every sample
y = z + self.offset + phase

y = np.exp(1j * y)
if output == "phase":
pass
elif output == "complex-exp":
y = np.exp(1j * y)
elif output == "sine":
y = np.sin(y)
elif output == "cosine":
y = np.cos(y)
else:
raise ValueError(
f"Argument 'output' must be one of 'phase', 'sine', 'cosine', or 'complex-exp', not {output!r}."
)

if y.size == 1:
y = y[0]
Expand Down
8 changes: 0 additions & 8 deletions src/sdr/plot/_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ def magnitude_response(
decades: The number of decades to plot when `x_axis="log"`.
kwargs: Additional keyword arguments to pass to :func:`matplotlib.pyplot.plot()`.

Note:
The default y-axis lower limit is set to the 10th percentile. This is to crop any deep nulls.

Examples:
See the :ref:`fir-filters` example.

Expand Down Expand Up @@ -361,11 +358,6 @@ def magnitude_response(
else:
plt.plot(f, H, **kwargs)

# Avoid deep nulls
y_max = plt.gca().get_ylim()[1]
y_min = np.percentile(H, 10)
plt.ylim(y_min, y_max)

plt.grid(True, which="both")
if "label" in kwargs:
plt.legend()
Expand Down
4 changes: 2 additions & 2 deletions src/sdr/plot/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def real_or_complex_plot(

def min_ylim(y: npt.NDArray, separation: float, sample_rate: float):
ymin, ymax = plt.gca().get_ylim()
if ymax - ymin < separation:
if ymax - ymin < separation / 4:
# Find the mean of the signal rounded to the nearest sample
mean = int(round(np.mean(y) / sample_rate))
mean = int(round(np.nanmean(y) / sample_rate))
ymin = mean - separation / 2
ymax = mean + separation / 2
plt.gca().set_ylim(ymin, ymax)
8 changes: 0 additions & 8 deletions src/sdr/plot/_spectral_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ def periodogram(
y_axis: The y-axis scaling. Options are to display a linear or logarithmic power spectral density.
kwargs: Additional keyword arguments to pass to :func:`matplotlib.pyplot.plot()`.

Note:
The default y-axis lower limit is set to the 10th percentile. This is to crop any deep nulls.

Group:
plot-spectral-estimation
"""
Expand Down Expand Up @@ -103,11 +100,6 @@ def periodogram(
if "label" in kwargs:
plt.legend()

# Avoid deep nulls
y_max = plt.gca().get_ylim()[1]
y_min = np.percentile(Pxx, 10)
plt.ylim(y_min, y_max)

if sample_rate_provided:
plt.xlabel(f"Frequency ({units}), $f$")
else:
Expand Down
4 changes: 3 additions & 1 deletion tests/synchronization/test_nco.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import sdr


def test_call_no_args():
def test_call_exceptions():
nco = sdr.NCO()
with pytest.raises(ValueError):
nco()
with pytest.raises(ValueError):
nco(np.array([1, 2, 3]), output="invalid")


def test_initial_phase_is_offset():
Expand Down