Repository: scipy/scipy
File: scipy/signal/tests/test_filter_design.py (Python)

Code before:

        for a in [1, xp.ones(2)]:
            w, h = freqz(xp.ones(2), a, worN=0, include_nyquist=True)
            assert w.shape == (0,)
            assert h.shape == (0,)
            hdt = xp.complex128 if xp_default_dtype(xp) == xp.float64 else xp.complex64
            assert h.dtype == hdt

        w1, h1 = freqz(xp.asarray([1.0]), worN=8, whole = True, include_nyquist=True)
        w2, h2 = freqz(xp.asarray([1.0]), worN=8, whole = True, include_nyquist=False)
        assert_array_almost_equal(w1, w2)
        assert_array_almost_equal(h1, h2)

    # https://github.com/scipy/scipy/issues/17289
    # https://github.com/scipy/scipy/issues/15273
    @pytest.mark.parametrize('whole,nyquist,worN',
                             [(False, False, 32),
                              (False, True, 32),
                              (True, False, 32),
                              (True, True, 32),
                              (False, False, 257),
                              (False, True, 257),
                              (True, False, 257),
                              (True, True, 257)])
    @xfail_xp_backends("cupy", reason="XXX: CuPy's version suspect")
    def test_17289(self, whole, nyquist, worN, xp):
        d = xp.asarray([0.0, 1.0])
        w, Drfft = freqz(d, worN=32, whole=whole, include_nyquist=nyquist)
        _, Dpoly = freqz(d, worN=w)
        xp_assert_close(Drfft, Dpoly)

    def test_fs_validation(self):
        with pytest.raises(ValueError, match="Sampling.*single scalar"):
            freqz([1.0], fs=np.array([10, 20]))

        with pytest.raises(ValueError, match="Sampling.*be none."):
            freqz([1.0], fs=None)


@skip_xp_backends(cpu_only=True, exceptions=["cupy", "torch"])
class TestFreqz_sos:

    def test_freqz_sos_basic(self, xp):
        # Compare the results of freqz and freqz_sos for a low order
        # Butterworth filter.

        N = 500

        b, a = butter(4, 0.2)
        sos = butter(4, 0.2, output='sos')
        w, h = freqz(b, a, worN=N)

Review comment (bug):
```suggestion
    
    @xfail_xp_backends("cupy", reason="XXX: CuPy's version suspect")
```

Code after:

        for a in [1, xp.ones(2)]:
            w, h = freqz(xp.ones(2), a, worN=0, include_nyquist=True)
            assert w.shape == (0,)
            assert h.shape == (0,)
            hdt = xp.complex128 if xp_default_dtype(xp) == xp.float64 else xp.complex64
            assert h.dtype == hdt

        w1, h1 = freqz(xp.asarray([1.0]), worN=8, whole = True, include_nyquist=True)
        w2, h2 = freqz(xp.asarray([1.0]), worN=8, whole = True, include_nyquist=False)
        assert_array_almost_equal(w1, w2)
        assert_array_almost_equal(h1, h2)

    # https://github.com/scipy/scipy/issues/17289
    # https://github.com/scipy/scipy/issues/15273
    @pytest.mark.parametrize('whole,nyquist,worN',
                             [(False, False, 32),
                              (False, True, 32),
                              (True, False, 32),
                              (True, True, 32),
                              (False, False, 257),
                              (False, True, 257),
                              (True, False, 257),
                              (True, True, 257)])
    
    @xfail_xp_backends("cupy", reason="XXX: CuPy's version suspect")
    def test_17289(self, whole, nyquist, worN, xp):
        d = xp.asarray([0.0, 1.0])
        w, Drfft = freqz(d, worN=32, whole=whole, include_nyquist=nyquist)
        _, Dpoly = freqz(d, worN=w)
        xp_assert_close(Drfft, Dpoly)

    def test_fs_validation(self):
        with pytest.raises(ValueError, match="Sampling.*single scalar"):
            freqz([1.0], fs=np.array([10, 20]))

        with pytest.raises(ValueError, match="Sampling.*be none."):
            freqz([1.0], fs=None)


@skip_xp_backends(cpu_only=True, exceptions=["cupy", "torch"])
class TestFreqz_sos:

    def test_freqz_sos_basic(self, xp):
        # Compare the results of freqz and freqz_sos for a low order
        # Butterworth filter.

        N = 500

        b, a = butter(4, 0.2)
        sos = butter(4, 0.2, output='sos')