Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BUG: signal: freqz could return one more value than requested if N wa…

…s large (ticket #1441).

Also fixed a couple mistakes in the docstrings for freqz and freqs, and added tests for freqz.
  • Loading branch information...
commit 75caf2705d2af41d61f1e67177546473b7f88141 1 parent d730665
@WarrenWeckesser WarrenWeckesser authored
View
8 scipy/signal/filter_design.py
@@ -66,7 +66,7 @@ def freqs(b, a, worN=None, plot=None):
plot : callable
A callable that takes two arguments. If given, the return parameters
`w` and `h` are passed to plot. Useful for plotting the frequency
- response inside `freqz`.
+ response inside `freqs`.
Returns
-------
@@ -125,7 +125,7 @@ def freqz(b, a=1, worN=None, whole=0, plot=None):
Otherwise, compute the response at frequencies given in worN
whole : bool, optional
Normally, frequencies are computed from 0 to pi (upper-half of
- unit-circle. If whole is False, compute frequencies from 0 to 2*pi.
+ unit-circle. If whole is True, compute frequencies from 0 to 2*pi.
plot : callable
A callable that takes two arguments. If given, the return parameters
`w` and `h` are passed to plot. Useful for plotting the frequency
@@ -175,10 +175,10 @@ def freqz(b, a=1, worN=None, whole=0, plot=None):
lastpoint = pi
if worN is None:
N = 512
- w = numpy.arange(0,lastpoint,lastpoint/N)
+ w = numpy.linspace(0, lastpoint, N, endpoint=False)
elif isinstance(worN, types.IntType):
N = worN
- w = numpy.arange(0,lastpoint,lastpoint/N)
+ w = numpy.linspace(0, lastpoint, N, endpoint=False)
else:
w = worN
w = atleast_1d(w)
View
41 scipy/signal/tests/test_filter_design.py
@@ -2,9 +2,10 @@
import numpy as np
from numpy.testing import TestCase, assert_array_almost_equal, \
- assert_array_equal, assert_raises, assert_
+ assert_array_equal, assert_raises, assert_equal, assert_, \
+ run_module_suite
-from scipy.signal import tf2zpk, zpk2tf, bessel, BadCoefficients
+from scipy.signal import tf2zpk, zpk2tf, BadCoefficients, freqz
class TestTf2zpk(TestCase):
@@ -52,3 +53,39 @@ def test_identity(self):
assert_(isinstance(b, np.ndarray))
assert_array_equal(a, a_r)
assert_(isinstance(a, np.ndarray))
+
+
+class TestFreqz(TestCase):
+
+ def test_ticket1441(self):
+ """Regression test for ticket 1441."""
+ # Because freqz previously used arange instead of linspace,
+ # when N was large, it would return one more point than
+ # requested.
+ N = 100000
+ w, h = freqz([1.0], worN=N)
+ assert_equal(w.shape, (N,))
+
+ def test_basic(self):
+ w, h = freqz([1.0], worN=8)
+ assert_array_almost_equal(w, np.pi * np.arange(8.0) / 8)
+ assert_array_almost_equal(h, np.ones(8))
+
+ def test_basic_whole(self):
+ w, h = freqz([1.0], worN=8, whole=True)
+ assert_array_almost_equal(w, 2 * np.pi * np.arange(8.0) / 8)
+ assert_array_almost_equal(h, np.ones(8))
+
+ def test_plot(self):
+
+ def plot(w, h):
+ assert_array_almost_equal(w, np.pi * np.arange(8.0) / 8)
+ assert_array_almost_equal(h, np.ones(8))
+
+ assert_raises(ZeroDivisionError,
+ freqz, [1.0], worN=8, plot=lambda w, h: 1 / 0)
+ freqz([1.0], worN=8, plot=plot)
+
+
+if __name__ == "__main__":
+ run_module_suite()
Please sign in to comment.
Something went wrong with that request. Please try again.