Skip to content

Commit

Permalink
Set minimum ylim for phase/group delay plots
Browse files Browse the repository at this point in the history
Fixes Make plots have minimum `ylim` #179
  • Loading branch information
mhostetter committed Dec 3, 2023
1 parent ea36164 commit cfa7b57
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 1 addition & 3 deletions docs/examples/fir-filters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@
"source": [
"plt.figure(figsize=(10, 5))\n",
"sdr.plot.group_delay(fir)\n",
"plt.ylim(fir.delay - 2, fir.delay + 2)\n",
"plt.show()"
]
},
Expand All @@ -315,7 +314,6 @@
"source": [
"plt.figure(figsize=(10, 5))\n",
"sdr.plot.group_delay(fir, x_axis=\"log\")\n",
"plt.ylim(fir.delay - 2, fir.delay + 2)\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -381,7 +379,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.8.10"
},
"orig_nbformat": 4
},
Expand Down
12 changes: 7 additions & 5 deletions src/sdr/plot/_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .._filter import FIR, IIR
from .._helper import export
from ._helper import real_or_complex_plot
from ._helper import min_ylim, real_or_complex_plot
from ._rc_params import RC_PARAMS
from ._units import freq_units, time_units

Expand Down Expand Up @@ -618,6 +618,8 @@ def phase_delay(
else:
plt.plot(f, tau_phi, **kwargs)

min_ylim(tau_phi, 2 / sample_rate, sample_rate)

plt.grid(True, which="both")
if "label" in kwargs:
plt.legend()
Expand Down Expand Up @@ -668,8 +670,7 @@ def group_delay(
h_srrc = sdr.root_raised_cosine(0.5, 10, 10)
@savefig sdr_plot_group_delay_1.png
plt.figure(figsize=(8, 4)); \
sdr.plot.group_delay(h_srrc); \
plt.ylim(48, 52)
sdr.plot.group_delay(h_srrc);
See the :ref:`iir-filters` example.
Expand All @@ -687,8 +688,7 @@ def group_delay(
@savefig sdr_plot_group_delay_3.png
plt.figure(figsize=(8, 4)); \
sdr.plot.group_delay(h_srrc, x_axis="two-sided"); \
plt.ylim(48, 52)
sdr.plot.group_delay(h_srrc, x_axis="two-sided");
.. ipython:: python
Expand Down Expand Up @@ -739,6 +739,8 @@ def group_delay(
else:
plt.plot(f, tau_g, **kwargs)

min_ylim(tau_g, 2 / sample_rate, sample_rate)

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

if label:
plt.legend()


def min_ylim(y: npt.NDArray, separation: float, sample_rate: float):
ymin, ymax = plt.gca().get_ylim()
if ymax - ymin < separation:
# Find the mean of the signal rounded to the nearest sample
mean = int(round(np.mean(y) / sample_rate))
ymin = mean - separation / 2
ymax = mean + separation / 2
plt.gca().set_ylim(ymin, ymax)

0 comments on commit cfa7b57

Please sign in to comment.