Coverage for webgeodyn/filters/time.py: 0%

56 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-18 09:33 +0000

1import numpy as np 

2from webgeodyn.filters import coeffilter, modelfilter 

3from scipy.signal import butter, lfilter 

4 

5 

6@coeffilter 

7def removemean(coefs): 

8 """ Subtracts the temporal mean from the value of 

9 spherical harmonic coefficients. 

10 """ 

11 return coefs - np.mean(coefs, axis=0) 

12 

13 

14@modelfilter 

15def subsample(model, n): 

16 """ Subsamples by n the time axis of the whole model. """ 

17 print('Subsampling, 1 over ', n) 

18 for measureName in model.measures: 

19 if (measureName in model.measures) and (model.measures[measureName] is not None): 

20 newcoef = model.measures[measureName].data[::n] 

21 model.measures[measureName].setData(newcoef) 

22 if (measureName in model.measures_rms) and (model.measures_rms[measureName] is not None): 

23 newcoef_rms = model.measures_rms[measureName].data[::n] 

24 model.measures_rms[measureName].setData(newcoef_rms) 

25 model.times = model.times[::n] 

26 return model 

27 

28 

29@modelfilter 

30def subtime(model, it0, it1): 

31 """ Extracts a time window given by [it0, it1] of the whole model. """ 

32 for measureName in model.measures: 

33 if (measureName in model.measures) and (model.measures[measureName] is not None): 

34 newcoef = model.measures[measureName].data[it0:it1] 

35 model.measures[measureName].setData(newcoef) 

36 if (measureName in model.measures_rms) and (model.measures_rms[measureName] is not None): 

37 newcoef_rms = model.measures_rms[measureName].data[it0:it1] 

38 model.measures_rms[measureName].setData(newcoef_rms) 

39 

40 model.times = model.times[it0:it1] 

41 return model 

42 

43 

44@modelfilter 

45def mean(model): 

46 """ Performs the temporal mean of the whole model. """ 

47 for measureName in model.measures: 

48 if (measureName in model.measures) and (model.measures[measureName] is not None): 

49 newcoef = np.mean(model.measures[measureName].data,axis=0,keepdims=True) 

50 model.measures[measureName].setData(newcoef) 

51 if (measureName in model.measures_rms) and (model.measures_rms[measureName] is not None): 

52 newcoef_rms = np.mean(model.measures_rms[measureName].data,axis=0,keepdims=True) 

53 model.measures_rms[measureName].setData(newcoef_rms) 

54 

55 model.times = ['Mean'] 

56 return model 

57 

58 

59def butterfilter(btype, fs, order, Wn): 

60 """ Butterworth filter from scipy. """ 

61 nyq = 0.5 * fs 

62 if type(Wn) is list: 

63 Wn_nyq = [Wn[0] / nyq, Wn[1] / nyq] 

64 else: 

65 Wn_nyq = Wn / nyq 

66 b, a = butter(order, Wn_nyq, btype=btype, analog=False) 

67 return b, a 

68 

69 

70@coeffilter 

71def butterworth(coefs, btype, fs, order, Wn): 

72 """ Applies a Butterworth filter from scipy to coefs. 

73 TODO : Check fs, Wn 

74 Parameters : 

75 btype : {'lowpass', 'highpass', 'bandpass', 'bandstop'} 

76 Type of the Butterworth filter 

77 fs : float 

78 Sample frequency 

79 order : int 

80 Order of the Butterworth filter 

81 Wn : ? 

82 """ 

83 b, a = butterfilter(btype, fs, order, Wn) 

84 return lfilter(b, a, coefs, axis=0) 

85 

86 

87def deriv_series(A, t): 

88 """ Derivates A with respect to t. """ 

89 [n,nt]=A.shape # Extracts the time dimension nt 

90# print(A.shape) 

91# dAdt = (A[:,1:nt] - A[:,0:nt-1]) / (t[1:nt]-t[0:nt-1]) 

92# tm = (t[0:nt-1]+t[1:nt])/2. 

93 dAdt = (A[:,2:nt] - A[:,0:nt-2]) / (t[2:nt]-t[0:nt-2]) 

94 tm = (t[0:nt-2]+t[2:nt])/2. 

95# print(dAdt.shape) 

96 

97 return dAdt, tm