Skip to content

Commit

Permalink
Check parameter consistensy in signal.iirdesign (#12061)
Browse files Browse the repository at this point in the history
* Check if wp,ws are consistent

Ref. #11567

* Update documentation with a little explanation

...and add a colon after 'if'

* Minor changes

* Add tests for exceptions

* Fix pyflakes in new test

* Add new consistensy check and test
  • Loading branch information
erheron committed Jun 9, 2020
1 parent 99b8660 commit a49e960
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
20 changes: 18 additions & 2 deletions scipy/signal/filter_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -2075,8 +2075,10 @@ def iirdesign(wp, ws, gpass, gstop, analog=False, ftype='ellip', output='ba',
Parameters
----------
wp, ws : float
Passband and stopband edge frequencies.
wp, ws : float or array like, shape (2,)
Passband and stopband edge frequencies. Possible values are scalars
(for lowpass and highpass filters) or ranges (for bandpass and bandstop
filters).
For digital filters, these are in the same units as `fs`. By default,
`fs` is 2 half-cycles/sample, so these are normalized from 0 to 1,
where 1 is the Nyquist frequency. For example:
Expand All @@ -2087,6 +2089,8 @@ def iirdesign(wp, ws, gpass, gstop, analog=False, ftype='ellip', output='ba',
- Bandstop: wp = [0.1, 0.6], ws = [0.2, 0.5]
For analog filters, `wp` and `ws` are angular frequencies (e.g., rad/s).
Note, that for bandpass and bandstop filters passband must lie strictly
inside stopband or vice versa.
gpass : float
The maximum loss in the passband (dB).
gstop : float
Expand Down Expand Up @@ -2183,6 +2187,18 @@ def iirdesign(wp, ws, gpass, gstop, analog=False, ftype='ellip', output='ba',
wp = atleast_1d(wp)
ws = atleast_1d(ws)

if wp.shape[0] != ws.shape[0] or wp.shape not in [(1,), (2,)]:
raise ValueError("wp and ws must have one or two elements each, and"
"the same shape, got %s and %s"
% (wp.shape, ws.shape))
if wp.shape[0] == 2:
if wp[0] < 0 or ws[0] < 0:
raise ValueError("Values for wp, ws can't be negative")
if not((ws[0] < wp[0] and wp[1] < ws[1]) or
(wp[0] < ws[0] and ws[1] < wp[1])):
raise ValueError("Passband must lie strictly inside stopband"
" or vice versa")

band_type = 2 * (len(wp) - 1)
band_type += 1
if wp[0] >= ws[0]:
Expand Down
26 changes: 23 additions & 3 deletions scipy/signal/tests/test_filter_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
butter, buttord, cheb1ap, cheb1ord, cheb2ap,
cheb2ord, cheby1, cheby2, ellip, ellipap, ellipord,
firwin, freqs_zpk, freqs, freqz, freqz_zpk,
group_delay, iirfilter, iirnotch, iirpeak, lp2bp,
lp2bs, lp2hp, lp2lp, normalize, sos2tf, sos2zpk,
sosfreqz, tf2sos, tf2zpk, zpk2sos, zpk2tf,
group_delay, iirdesign, iirfilter, iirnotch, iirpeak,
lp2bp, lp2bs, lp2hp, lp2lp, normalize, sos2tf,
sos2zpk, sosfreqz, tf2sos, tf2zpk, zpk2sos, zpk2tf,
bilinear_zpk, lp2lp_zpk, lp2hp_zpk, lp2bp_zpk,
lp2bs_zpk)
from scipy.signal.filter_design import (_cplxreal, _cplxpair, _norm_factor,
Expand Down Expand Up @@ -3611,6 +3611,26 @@ def test_fs_param(self):
assert_allclose(abs(hp[2]), 1, rtol=1e-10)


class TestIIRDesign(object):

def test_exceptions(self):
with pytest.raises(ValueError, match="the same shape"):
iirdesign(0.2, [0.1, 0.3], 1, 40)
with pytest.raises(ValueError, match="the same shape"):
iirdesign(np.array([[0.3, 0.6], [0.3, 0.6]]),
np.array([[0.4, 0.5], [0.4, 0.5]]), 1, 40)
with pytest.raises(ValueError, match="can't be negative"):
iirdesign([0.1, 0.3], [-0.1, 0.5], 1, 40)
with pytest.raises(ValueError, match="strictly inside stopband"):
iirdesign([0.1, 0.4], [0.5, 0.6], 1, 40)
with pytest.raises(ValueError, match="strictly inside stopband"):
iirdesign([0.5, 0.6], [0.1, 0.4], 1, 40)
with pytest.raises(ValueError, match="strictly inside stopband"):
iirdesign([0.3, 0.6], [0.4, 0.7], 1, 40)
with pytest.raises(ValueError, match="strictly inside stopband"):
iirdesign([0.4, 0.7], [0.3, 0.6], 1, 40)


class TestIIRFilter(object):

def test_symmetry(self):
Expand Down

0 comments on commit a49e960

Please sign in to comment.