Пример #1
0
    def test_first_derivative(self):
        x0 = np.arange(0, 2.0 * np.pi, 0.01)
        xx = []
        for x in x0:
            if np.random.random() < .3:
                xx.append(x)
        x0 = np.array(xx)

        testf = np.array([np.sin(x) for x in x0])
        true_deriv = np.array([np.cos(x) for x in x0])

        wwidth = 5
        deriv = np.empty_like(x0)
        for ix, (wx, wy) in enumerate(zip(sliding_window(x0, width=wwidth, fixed_width=True),
                                          sliding_window(testf, width=wwidth, fixed_width=True))):
            x = x0[ix]
            coeff = fd_coefficients(x, wx, k=1)
            deriv[ix] = coeff.dot(wy)

        np.testing.assert_almost_equal(deriv, true_deriv, 6)
Пример #2
0
    def test_sliding_window(self):
        n = 0
        seq = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        width = 2
        for ix in sliding_window(seq, width=width, fixed_width=False):
            self.assertLessEqual(len(ix), 2 * width + 1)
            self.assertGreaterEqual(seq[n], np.min(ix))
            self.assertLessEqual(seq[n], np.max(ix))
            n += 1

        self.assertEqual(n, len(seq), "need n={} == len(seq)=={}".format(n, len(seq)))
Пример #3
0
    def test_sliding_window_fixed_width(self):
        n = 0
        seq = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        width = 2
        for ix in sliding_window(seq, width=width, fixed_width=True):
            assert len(ix) == 2 * width + 1
            assert seq[n] >= np.min(ix)
            assert seq[n] <= np.max(ix)
            n += 1

        assert n == len(seq), "need n={} == len(seq)=={}".format(n, len(seq))
Пример #4
0
    def test_first_derivative(self):
        x0 = np.arange(0, 2.0 * np.pi, 0.05)
        xx = []
        for x in x0:
            if np.random.random() < .3:
                xx.append(x)
        x0 = np.array(xx)

        testf = np.array([np.sin(x) for x in x0])
        true_deriv = [np.cos(x) for x in x0]

        wwidth = 5
        deriv = np.empty_like(x0)
        for ix, (wx, wy) in enumerate(
                zip(sliding_window(x0, width=wwidth, fixed_width=True),
                    sliding_window(testf, width=wwidth, fixed_width=True))):
            x = x0[ix]
            coeff = fd_coefficients(x, wx, k=1)
            deriv[ix] = coeff.dot(wy)
        plt.plot(x0, deriv, 'o')
        plt.plot(x0, true_deriv)
        plt.show()
        print(np.array(deriv) - np.array(true_deriv))