_savitzky_golay.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import numpy as np
  2. from scipy.linalg import lstsq
  3. from scipy._lib._util import float_factorial
  4. from scipy.ndimage import convolve1d
  5. from ._arraytools import axis_slice
  6. def savgol_coeffs(window_length, polyorder, deriv=0, delta=1.0, pos=None,
  7. use="conv"):
  8. """Compute the coefficients for a 1-D Savitzky-Golay FIR filter.
  9. Parameters
  10. ----------
  11. window_length : int
  12. The length of the filter window (i.e., the number of coefficients).
  13. polyorder : int
  14. The order of the polynomial used to fit the samples.
  15. `polyorder` must be less than `window_length`.
  16. deriv : int, optional
  17. The order of the derivative to compute. This must be a
  18. nonnegative integer. The default is 0, which means to filter
  19. the data without differentiating.
  20. delta : float, optional
  21. The spacing of the samples to which the filter will be applied.
  22. This is only used if deriv > 0.
  23. pos : int or None, optional
  24. If pos is not None, it specifies evaluation position within the
  25. window. The default is the middle of the window.
  26. use : str, optional
  27. Either 'conv' or 'dot'. This argument chooses the order of the
  28. coefficients. The default is 'conv', which means that the
  29. coefficients are ordered to be used in a convolution. With
  30. use='dot', the order is reversed, so the filter is applied by
  31. dotting the coefficients with the data set.
  32. Returns
  33. -------
  34. coeffs : 1-D ndarray
  35. The filter coefficients.
  36. See Also
  37. --------
  38. savgol_filter
  39. Notes
  40. -----
  41. .. versionadded:: 0.14.0
  42. References
  43. ----------
  44. A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of Data by
  45. Simplified Least Squares Procedures. Analytical Chemistry, 1964, 36 (8),
  46. pp 1627-1639.
  47. Jianwen Luo, Kui Ying, and Jing Bai. 2005. Savitzky-Golay smoothing and
  48. differentiation filter for even number data. Signal Process.
  49. 85, 7 (July 2005), 1429-1434.
  50. Examples
  51. --------
  52. >>> import numpy as np
  53. >>> from scipy.signal import savgol_coeffs
  54. >>> savgol_coeffs(5, 2)
  55. array([-0.08571429, 0.34285714, 0.48571429, 0.34285714, -0.08571429])
  56. >>> savgol_coeffs(5, 2, deriv=1)
  57. array([ 2.00000000e-01, 1.00000000e-01, 2.07548111e-16, -1.00000000e-01,
  58. -2.00000000e-01])
  59. Note that use='dot' simply reverses the coefficients.
  60. >>> savgol_coeffs(5, 2, pos=3)
  61. array([ 0.25714286, 0.37142857, 0.34285714, 0.17142857, -0.14285714])
  62. >>> savgol_coeffs(5, 2, pos=3, use='dot')
  63. array([-0.14285714, 0.17142857, 0.34285714, 0.37142857, 0.25714286])
  64. >>> savgol_coeffs(4, 2, pos=3, deriv=1, use='dot')
  65. array([0.45, -0.85, -0.65, 1.05])
  66. `x` contains data from the parabola x = t**2, sampled at
  67. t = -1, 0, 1, 2, 3. `c` holds the coefficients that will compute the
  68. derivative at the last position. When dotted with `x` the result should
  69. be 6.
  70. >>> x = np.array([1, 0, 1, 4, 9])
  71. >>> c = savgol_coeffs(5, 2, pos=4, deriv=1, use='dot')
  72. >>> c.dot(x)
  73. 6.0
  74. """
  75. # An alternative method for finding the coefficients when deriv=0 is
  76. # t = np.arange(window_length)
  77. # unit = (t == pos).astype(int)
  78. # coeffs = np.polyval(np.polyfit(t, unit, polyorder), t)
  79. # The method implemented here is faster.
  80. # To recreate the table of sample coefficients shown in the chapter on
  81. # the Savitzy-Golay filter in the Numerical Recipes book, use
  82. # window_length = nL + nR + 1
  83. # pos = nL + 1
  84. # c = savgol_coeffs(window_length, M, pos=pos, use='dot')
  85. if polyorder >= window_length:
  86. raise ValueError("polyorder must be less than window_length.")
  87. halflen, rem = divmod(window_length, 2)
  88. if pos is None:
  89. if rem == 0:
  90. pos = halflen - 0.5
  91. else:
  92. pos = halflen
  93. if not (0 <= pos < window_length):
  94. raise ValueError("pos must be nonnegative and less than "
  95. "window_length.")
  96. if use not in ['conv', 'dot']:
  97. raise ValueError("`use` must be 'conv' or 'dot'")
  98. if deriv > polyorder:
  99. coeffs = np.zeros(window_length)
  100. return coeffs
  101. # Form the design matrix A. The columns of A are powers of the integers
  102. # from -pos to window_length - pos - 1. The powers (i.e., rows) range
  103. # from 0 to polyorder. (That is, A is a vandermonde matrix, but not
  104. # necessarily square.)
  105. x = np.arange(-pos, window_length - pos, dtype=float)
  106. if use == "conv":
  107. # Reverse so that result can be used in a convolution.
  108. x = x[::-1]
  109. order = np.arange(polyorder + 1).reshape(-1, 1)
  110. A = x ** order
  111. # y determines which order derivative is returned.
  112. y = np.zeros(polyorder + 1)
  113. # The coefficient assigned to y[deriv] scales the result to take into
  114. # account the order of the derivative and the sample spacing.
  115. y[deriv] = float_factorial(deriv) / (delta ** deriv)
  116. # Find the least-squares solution of A*c = y
  117. coeffs, _, _, _ = lstsq(A, y)
  118. return coeffs
  119. def _polyder(p, m):
  120. """Differentiate polynomials represented with coefficients.
  121. p must be a 1-D or 2-D array. In the 2-D case, each column gives
  122. the coefficients of a polynomial; the first row holds the coefficients
  123. associated with the highest power. m must be a nonnegative integer.
  124. (numpy.polyder doesn't handle the 2-D case.)
  125. """
  126. if m == 0:
  127. result = p
  128. else:
  129. n = len(p)
  130. if n <= m:
  131. result = np.zeros_like(p[:1, ...])
  132. else:
  133. dp = p[:-m].copy()
  134. for k in range(m):
  135. rng = np.arange(n - k - 1, m - k - 1, -1)
  136. dp *= rng.reshape((n - m,) + (1,) * (p.ndim - 1))
  137. result = dp
  138. return result
  139. def _fit_edge(x, window_start, window_stop, interp_start, interp_stop,
  140. axis, polyorder, deriv, delta, y):
  141. """
  142. Given an N-d array `x` and the specification of a slice of `x` from
  143. `window_start` to `window_stop` along `axis`, create an interpolating
  144. polynomial of each 1-D slice, and evaluate that polynomial in the slice
  145. from `interp_start` to `interp_stop`. Put the result into the
  146. corresponding slice of `y`.
  147. """
  148. # Get the edge into a (window_length, -1) array.
  149. x_edge = axis_slice(x, start=window_start, stop=window_stop, axis=axis)
  150. if axis == 0 or axis == -x.ndim:
  151. xx_edge = x_edge
  152. swapped = False
  153. else:
  154. xx_edge = x_edge.swapaxes(axis, 0)
  155. swapped = True
  156. xx_edge = xx_edge.reshape(xx_edge.shape[0], -1)
  157. # Fit the edges. poly_coeffs has shape (polyorder + 1, -1),
  158. # where '-1' is the same as in xx_edge.
  159. poly_coeffs = np.polyfit(np.arange(0, window_stop - window_start),
  160. xx_edge, polyorder)
  161. if deriv > 0:
  162. poly_coeffs = _polyder(poly_coeffs, deriv)
  163. # Compute the interpolated values for the edge.
  164. i = np.arange(interp_start - window_start, interp_stop - window_start)
  165. values = np.polyval(poly_coeffs, i.reshape(-1, 1)) / (delta ** deriv)
  166. # Now put the values into the appropriate slice of y.
  167. # First reshape values to match y.
  168. shp = list(y.shape)
  169. shp[0], shp[axis] = shp[axis], shp[0]
  170. values = values.reshape(interp_stop - interp_start, *shp[1:])
  171. if swapped:
  172. values = values.swapaxes(0, axis)
  173. # Get a view of the data to be replaced by values.
  174. y_edge = axis_slice(y, start=interp_start, stop=interp_stop, axis=axis)
  175. y_edge[...] = values
  176. def _fit_edges_polyfit(x, window_length, polyorder, deriv, delta, axis, y):
  177. """
  178. Use polynomial interpolation of x at the low and high ends of the axis
  179. to fill in the halflen values in y.
  180. This function just calls _fit_edge twice, once for each end of the axis.
  181. """
  182. halflen = window_length // 2
  183. _fit_edge(x, 0, window_length, 0, halflen, axis,
  184. polyorder, deriv, delta, y)
  185. n = x.shape[axis]
  186. _fit_edge(x, n - window_length, n, n - halflen, n, axis,
  187. polyorder, deriv, delta, y)
  188. def savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0,
  189. axis=-1, mode='interp', cval=0.0):
  190. """ Apply a Savitzky-Golay filter to an array.
  191. This is a 1-D filter. If `x` has dimension greater than 1, `axis`
  192. determines the axis along which the filter is applied.
  193. Parameters
  194. ----------
  195. x : array_like
  196. The data to be filtered. If `x` is not a single or double precision
  197. floating point array, it will be converted to type ``numpy.float64``
  198. before filtering.
  199. window_length : int
  200. The length of the filter window (i.e., the number of coefficients).
  201. If `mode` is 'interp', `window_length` must be less than or equal
  202. to the size of `x`.
  203. polyorder : int
  204. The order of the polynomial used to fit the samples.
  205. `polyorder` must be less than `window_length`.
  206. deriv : int, optional
  207. The order of the derivative to compute. This must be a
  208. nonnegative integer. The default is 0, which means to filter
  209. the data without differentiating.
  210. delta : float, optional
  211. The spacing of the samples to which the filter will be applied.
  212. This is only used if deriv > 0. Default is 1.0.
  213. axis : int, optional
  214. The axis of the array `x` along which the filter is to be applied.
  215. Default is -1.
  216. mode : str, optional
  217. Must be 'mirror', 'constant', 'nearest', 'wrap' or 'interp'. This
  218. determines the type of extension to use for the padded signal to
  219. which the filter is applied. When `mode` is 'constant', the padding
  220. value is given by `cval`. See the Notes for more details on 'mirror',
  221. 'constant', 'wrap', and 'nearest'.
  222. When the 'interp' mode is selected (the default), no extension
  223. is used. Instead, a degree `polyorder` polynomial is fit to the
  224. last `window_length` values of the edges, and this polynomial is
  225. used to evaluate the last `window_length // 2` output values.
  226. cval : scalar, optional
  227. Value to fill past the edges of the input if `mode` is 'constant'.
  228. Default is 0.0.
  229. Returns
  230. -------
  231. y : ndarray, same shape as `x`
  232. The filtered data.
  233. See Also
  234. --------
  235. savgol_coeffs
  236. Notes
  237. -----
  238. Details on the `mode` options:
  239. 'mirror':
  240. Repeats the values at the edges in reverse order. The value
  241. closest to the edge is not included.
  242. 'nearest':
  243. The extension contains the nearest input value.
  244. 'constant':
  245. The extension contains the value given by the `cval` argument.
  246. 'wrap':
  247. The extension contains the values from the other end of the array.
  248. For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8], and
  249. `window_length` is 7, the following shows the extended data for
  250. the various `mode` options (assuming `cval` is 0)::
  251. mode | Ext | Input | Ext
  252. -----------+---------+------------------------+---------
  253. 'mirror' | 4 3 2 | 1 2 3 4 5 6 7 8 | 7 6 5
  254. 'nearest' | 1 1 1 | 1 2 3 4 5 6 7 8 | 8 8 8
  255. 'constant' | 0 0 0 | 1 2 3 4 5 6 7 8 | 0 0 0
  256. 'wrap' | 6 7 8 | 1 2 3 4 5 6 7 8 | 1 2 3
  257. .. versionadded:: 0.14.0
  258. Examples
  259. --------
  260. >>> import numpy as np
  261. >>> from scipy.signal import savgol_filter
  262. >>> np.set_printoptions(precision=2) # For compact display.
  263. >>> x = np.array([2, 2, 5, 2, 1, 0, 1, 4, 9])
  264. Filter with a window length of 5 and a degree 2 polynomial. Use
  265. the defaults for all other parameters.
  266. >>> savgol_filter(x, 5, 2)
  267. array([1.66, 3.17, 3.54, 2.86, 0.66, 0.17, 1. , 4. , 9. ])
  268. Note that the last five values in x are samples of a parabola, so
  269. when mode='interp' (the default) is used with polyorder=2, the last
  270. three values are unchanged. Compare that to, for example,
  271. `mode='nearest'`:
  272. >>> savgol_filter(x, 5, 2, mode='nearest')
  273. array([1.74, 3.03, 3.54, 2.86, 0.66, 0.17, 1. , 4.6 , 7.97])
  274. """
  275. if mode not in ["mirror", "constant", "nearest", "interp", "wrap"]:
  276. raise ValueError("mode must be 'mirror', 'constant', 'nearest' "
  277. "'wrap' or 'interp'.")
  278. x = np.asarray(x)
  279. # Ensure that x is either single or double precision floating point.
  280. if x.dtype != np.float64 and x.dtype != np.float32:
  281. x = x.astype(np.float64)
  282. coeffs = savgol_coeffs(window_length, polyorder, deriv=deriv, delta=delta)
  283. if mode == "interp":
  284. if window_length > x.shape[axis]:
  285. raise ValueError("If mode is 'interp', window_length must be less "
  286. "than or equal to the size of x.")
  287. # Do not pad. Instead, for the elements within `window_length // 2`
  288. # of the ends of the sequence, use the polynomial that is fitted to
  289. # the last `window_length` elements.
  290. y = convolve1d(x, coeffs, axis=axis, mode="constant")
  291. _fit_edges_polyfit(x, window_length, polyorder, deriv, delta, axis, y)
  292. else:
  293. # Any mode other than 'interp' is passed on to ndimage.convolve1d.
  294. y = convolve1d(x, coeffs, axis=axis, mode=mode, cval=cval)
  295. return y