forked from XiaoxG/skloe_outfile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff1d.py
28 lines (22 loc) · 897 Bytes
/
diff1d.py
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
# -*- coding: utf-8 -*-
import warnings
import numpy as np
def diff1d(y, dx):
"""Calculate the first-order derivative of the signal y.
@param: y - the signal
@param: dx - interval"""
y = np.asarray(y, dtype='float')
dy = np.zeros_like(y)
if y.shape[0] <= 5:
warnings.warn("Array size is too small!")
return dy
else:
dy[0] = (-y[2] + 4 * y[1] - 3 * y[0]) / (2 * dx)
dy[1] = (-y[3] + 6 * y[2] - 3 * y[1] - 2 * y[0]) / (6 * dx)
dy[2] = (8 * (y[3] - y[1]) - (y[4] - y[0])) / (12 * dx)
dy[3:-3] = (45 * (y[4:-2] - y[2:-4]) - 9 *
(y[5:-1] - y[1:-5]) + (y[6:] - y[:-6])) / (60 * dx)
dy[-3] = (8 * (y[-2] - y[-4]) - (y[-1] - y[-5])) / (12 * dx)
dy[-2] = (2 * y[-1] + 3 * y[-2] - 6 * y[-3] + y[-4]) / (6 * dx)
dy[-1] = (3 * y[-1] - 4 * y[-2] + y[-3]) / (2 * dx)
return dy