_savitzky_golay.py 13 KB

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