Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

""" 

The Filter test suite. 

""" 

 

from obspy.signal.headers import lib_name, lib_extension 

from obspy.signal import xcorr 

import ctypes as C 

import numpy as np 

import os 

import unittest 

 

 

class UtilTestCase(unittest.TestCase): 

    """ 

    Test cases for L{obspy.signal.util}. 

    """ 

    def test_xcorr(self): 

        """ 

        """ 

        # example 1 - all samples are equal 

        np.random.seed(815)  # make test reproducable 

        tr1 = np.random.randn(10000).astype('float32') 

        tr2 = tr1.copy() 

        shift, corr = xcorr(tr1, tr2, 100) 

        self.assertEquals(shift, 0) 

        self.assertAlmostEquals(corr, 1, 2) 

        # example 2 - all samples are different 

        tr1 = np.ones(10000, dtype='float32') 

        tr2 = np.zeros(10000, dtype='float32') 

        shift, corr = xcorr(tr1, tr2, 100) 

        self.assertEquals(shift, 0) 

        self.assertAlmostEquals(corr, 0, 2) 

        # example 3 - shift of 10 samples 

        tr1 = np.random.randn(10000).astype('float32') 

        tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) 

        shift, corr = xcorr(tr1, tr2, 100) 

        self.assertEquals(shift, -10) 

        self.assertAlmostEquals(corr, 1, 2) 

        shift, corr = xcorr(tr2, tr1, 100) 

        self.assertEquals(shift, 10) 

        self.assertAlmostEquals(corr, 1, 2) 

        # example 4 - shift of 10 samples + small sine disturbance 

        tr1 = (np.random.randn(10000) * 100).astype('float32') 

        var = np.sin(np.arange(10000, dtype='float32') * 0.1) 

        tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) * 0.9 

        tr2 += var 

        shift, corr = xcorr(tr1, tr2, 100) 

        self.assertEquals(shift, -10) 

        self.assertAlmostEquals(corr, 1, 2) 

        shift, corr = xcorr(tr2, tr1, 100) 

        self.assertEquals(shift, 10) 

        self.assertAlmostEquals(corr, 1, 2) 

 

    def test_SRLXcorr(self): 

        """ 

        Tests if example in ObsPy paper submitted to the Electronic 

        Seismologist section of SRL is still working. The test shouldn't be 

        changed because the reference gets wrong. 

        """ 

        np.random.seed(815) 

        data1 = np.random.randn(1000).astype('float32') 

        data2 = data1.copy() 

 

        window_len = 100 

        corp = np.empty(2 * window_len + 1, dtype='float64') 

 

        path = os.path.dirname(__file__) 

        name = os.path.join(path, os.pardir, os.pardir, 'lib', 

                            lib_name + lib_extension) 

        lib = C.CDLL(name) 

        # 

        shift = C.c_int() 

        coe_p = C.c_double() 

        lib.X_corr(data1.ctypes.data_as(C.c_void_p), 

                   data2.ctypes.data_as(C.c_void_p), 

                   corp.ctypes.data_as(C.c_void_p), 

                   window_len, len(data1), len(data2), 

                   C.byref(shift), C.byref(coe_p)) 

 

        self.assertAlmostEquals(0.0, shift.value) 

        self.assertAlmostEquals(1.0, coe_p.value) 

 

 

def suite(): 

    return unittest.makeSuite(UtilTestCase, 'test') 

 

 

if __name__ == '__main__': 

    unittest.main(defaultTest='suite')