예제 #1
0
    def test_firls(self):
        N = 11  # number of taps in the filter
        a = 0.1  # width of the transition band

        # design a halfband symmetric low-pass filter
        h = firls(11, [0, a, 0.5 - a, 0.5], [1, 1, 0, 0], nyq=0.5)

        # make sure the filter has correct # of taps
        assert_equal(len(h), N)

        # make sure it is symmetric
        midx = (N - 1) // 2
        assert_array_almost_equal(h[:midx], h[:-midx - 1:-1])

        # make sure the center tap is 0.5
        assert_almost_equal(h[midx], 0.5)

        # For halfband symmetric, odd coefficients (except the center)
        # should be zero (really small)
        hodd = np.hstack((h[1:midx:2], h[-midx + 1::2]))
        assert_array_almost_equal(hodd, 0)

        # now check the frequency response
        w, H = freqz(h, 1)
        f = w / 2 / np.pi
        Hmag = np.abs(H)

        # check that the pass band is close to unity
        idx = np.logical_and(f > 0, f < a)
        assert_array_almost_equal(Hmag[idx], 1, decimal=3)

        # check that the stop band is close to zero
        idx = np.logical_and(f > 0.5 - a, f < 0.5)
        assert_array_almost_equal(Hmag[idx], 0, decimal=3)
예제 #2
0
    def test_nyq(self):
        """Test the nyq keyword."""
        nyquist = 1000
        width = 40.0
        relative_width = width / nyquist
        ntaps, beta = kaiserord(120, relative_width)
        taps = firwin(ntaps,
                      cutoff=[300, 700],
                      window=('kaiser', beta),
                      pass_zero=False,
                      scale=False,
                      nyq=nyquist)

        # Check the symmetry of taps.
        assert_array_almost_equal(taps[:ntaps // 2],
                                  taps[ntaps:ntaps - ntaps // 2 - 1:-1])

        # Check the gain at a few samples where we know it should be approximately 0 or 1.
        freq_samples = np.array([
            0.0, 200, 300 - width / 2, 300 + width / 2, 500, 700 - width / 2,
            700 + width / 2, 800, 1000
        ])
        freqs, response = freqz(taps, worN=np.pi * freq_samples / nyquist)
        assert_array_almost_equal(
            np.abs(response), [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0],
            decimal=5)
예제 #3
0
    def test05(self):
        """Test firwin2 for calculating Type IV filters"""
        ntaps = 1500

        freq = [0.0, 1.0]
        gain = [0.0, 1.0]
        taps = firwin2(ntaps, freq, gain, window=None, antisymmetric=True)
        assert_array_almost_equal(taps[:ntaps // 2], -taps[ntaps // 2:][::-1])

        freqs, response = freqz(taps, worN=2048)
        assert_array_almost_equal(abs(response), freqs / np.pi, decimal=4)
예제 #4
0
    def test06(self):
        """Test firwin2 for calculating Type III filters"""
        ntaps = 1501

        freq = [0.0, 0.5, 0.55, 1.0]
        gain = [0.0, 0.5, 0.0, 0.0]
        taps = firwin2(ntaps, freq, gain, window=None, antisymmetric=True)
        assert_equal(taps[ntaps // 2], 0.0)
        assert_array_almost_equal(taps[:ntaps // 2],
                                  -taps[ntaps // 2 + 1:][::-1])

        freqs, response1 = freqz(taps, worN=2048)
        response2 = np.interp(freqs / np.pi, freq, gain)
        assert_array_almost_equal(abs(response1), response2, decimal=3)
예제 #5
0
 def mse(self, h, bands):
     """Compute mean squared error versus ideal response across frequency
     band.
       h -- coefficients
       bands -- list of (left, right) tuples relative to 1==Nyquist of
         passbands
     """
     w, H = freqz(h, worN=1024)
     f = w / np.pi
     passIndicator = np.zeros(len(w), bool)
     for left, right in bands:
         passIndicator |= (f >= left) & (f < right)
     Hideal = np.where(passIndicator, 1, 0)
     mse = np.mean(abs(abs(H) - Hideal)**2)
     return mse
예제 #6
0
 def test02(self):
     width = 0.04
     beta = 12.0
     # ntaps must be odd for positive gain at Nyquist.
     ntaps = 401
     # An ideal highpass filter.
     freq = [0.0, 0.5, 0.5, 1.0]
     gain = [0.0, 0.0, 1.0, 1.0]
     taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
     freq_samples = np.array(
         [0.0, 0.25, 0.5 - width, 0.5 + width, 0.75, 1.0])
     freqs, response = freqz(taps, worN=np.pi * freq_samples)
     assert_array_almost_equal(np.abs(response),
                               [0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
                               decimal=5)
예제 #7
0
 def test03(self):
     width = 0.02
     ntaps, beta = kaiserord(120, width)
     # ntaps must be odd for positive gain at Nyquist.
     ntaps = int(ntaps) | 1
     freq = [0.0, 0.4, 0.4, 0.5, 0.5, 1.0]
     gain = [1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
     taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
     freq_samples = np.array([
         0.0, 0.4 - width, 0.4 + width, 0.45, 0.5 - width, 0.5 + width,
         0.75, 1.0
     ])
     freqs, response = freqz(taps, worN=np.pi * freq_samples)
     assert_array_almost_equal(np.abs(response),
                               [1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
                               decimal=5)
예제 #8
0
 def test01(self):
     width = 0.04
     beta = 12.0
     ntaps = 400
     # Filter is 1 from w=0 to w=0.5, then decreases linearly from 1 to 0 as w
     # increases from w=0.5 to w=1  (w=1 is the Nyquist frequency).
     freq = [0.0, 0.5, 1.0]
     gain = [1.0, 1.0, 0.0]
     taps = firwin2(ntaps, freq, gain, window=('kaiser', beta))
     freq_samples = np.array([
         0.0, 0.25, 0.5 - width / 2, 0.5 + width / 2, 0.75, 1.0 - width / 2
     ])
     freqs, response = freqz(taps, worN=np.pi * freq_samples)
     assert_array_almost_equal(np.abs(response),
                               [1.0, 1.0, 1.0, 1.0 - width, 0.5, width],
                               decimal=5)
예제 #9
0
    def test_lowpass(self):
        width = 0.04
        ntaps, beta = kaiserord(120, width)
        taps = firwin(ntaps, cutoff=0.5, window=('kaiser', beta), scale=False)

        # Check the symmetry of taps.
        assert_array_almost_equal(taps[:ntaps // 2],
                                  taps[ntaps:ntaps - ntaps // 2 - 1:-1])

        # Check the gain at a few samples where we know it should be approximately 0 or 1.
        freq_samples = np.array(
            [0.0, 0.25, 0.5 - width / 2, 0.5 + width / 2, 0.75, 1.0])
        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)