[Scipy-svn] r6647 - in trunk/scipy/signal: . tests

scipy-svn@scip... scipy-svn@scip...
Tue Aug 17 19:35:48 CDT 2010


Author: charris
Date: 2010-08-17 19:35:47 -0500 (Tue, 17 Aug 2010)
New Revision: 6647

Modified:
   trunk/scipy/signal/signaltools.py
   trunk/scipy/signal/tests/test_filter_design.py
Log:
ENH: Fix remez to recognize 'hilbert' option. Thanks to kumanna.

Modified: trunk/scipy/signal/signaltools.py
===================================================================
--- trunk/scipy/signal/signaltools.py	2010-07-30 17:33:38 UTC (rev 6646)
+++ trunk/scipy/signal/signaltools.py	2010-08-18 00:35:47 UTC (rev 6647)
@@ -514,6 +514,9 @@
     type --- The type of filter:
              'bandpass' : flat response in bands.
              'differentiator' : frequency proportional response in bands.
+             'hilbert' : filter with odd symmetry, that is, type III
+                         (for even order) or type IV (for odd order)
+                         linear phase filters
 
   Outputs: (out,)
 
@@ -523,9 +526,9 @@
     """
     # Convert type
     try:
-        tnum = {'bandpass':1, 'differentiator':2}[type]
+        tnum = {'bandpass':1, 'differentiator':2, 'hilbert':3}[type]
     except KeyError:
-        raise ValueError, "Type must be 'bandpass', or 'differentiator'"
+        raise ValueError("Type must be 'bandpass', 'differentiator', or 'hilbert'")
 
     # Convert weight
     if weight is None:

Modified: trunk/scipy/signal/tests/test_filter_design.py
===================================================================
--- trunk/scipy/signal/tests/test_filter_design.py	2010-07-30 17:33:38 UTC (rev 6646)
+++ trunk/scipy/signal/tests/test_filter_design.py	2010-08-18 00:35:47 UTC (rev 6647)
@@ -1,9 +1,9 @@
 import warnings
 
 import numpy as np
-from numpy.testing import TestCase, assert_array_almost_equal
+from numpy.testing import *
 
-from scipy.signal import tf2zpk, bessel, BadCoefficients, kaiserord, firwin, freqz
+from scipy.signal import tf2zpk, bessel, BadCoefficients, kaiserord, firwin, freqz, remez
 
 
 class TestTf2zpk(TestCase):
@@ -40,7 +40,7 @@
 
 
 class TestFirWin(TestCase):
-    
+
     def test_lowpass(self):
         width = 0.04
         ntaps, beta = kaiserord(120, width)
@@ -49,3 +49,34 @@
         freqs, response = freqz(taps, worN=np.pi*freq_samples)
         assert_array_almost_equal(np.abs(response),
                                     [1.0, 1.0, 1.0, 0.0, 0.0, 0.0], decimal=5)
+
+class TestRemez(TestCase):
+
+    def test_hilbert(self):
+        N = 11 # number of taps in the filter
+        a = 0.1 # width of the transition band
+
+        # design an unity gain hilbert bandpass filter from w to 0.5-w
+        h = remez(11, [ a, 0.5-a ], [ 1 ], type='hilbert')
+
+        # make sure the filter has correct # of taps
+        assert_(len(h) == N, "Number of Taps")
+
+        # make sure it is type III (anti-symmtric tap coefficients)
+        assert_array_almost_equal(h[:(N-1)/2], -h[:-(N-1)/2-1:-1])
+
+        # Since the requested response is symmetric, all even coeffcients
+        # should be zero (or in this case really small)
+        assert_((abs(h[1::2]) < 1e-15).all(), "Even Coefficients Equal Zero")
+
+        # now check the frequency response
+        w, H = freqz(h, 1)
+        f = w/2/np.pi
+        Hmag = abs(H)
+
+        # should have a zero at 0 and pi (in this case close to zero)
+        assert_((Hmag[ [0,-1] ] < 0.02).all(), "Zero at zero and pi")
+
+        # check that the pass band is close to unity
+        idx = (f > a) * (f < 0.5-a)
+        assert_((abs(Hmag[idx] - 1) < 0.015).all(), "Pass Band Close To Unity")



More information about the Scipy-svn mailing list