【python】scipy.signal.savgol_filter()

Table of contents

role​

parameter settings

Example


effect

Apply Savitzky-Golay filter to the array.

SG filtering method (Savitzky Golay Filter) was originally proposed by Savitzky and Golay in 1964. It is widely used in data flow smoothing and denoising. It is a filtering method based on local polynomial least squares fitting in the time domain. Its core idea is also to perform weighted filtering on the data within the window, but its weighting weight is obtained by least squares fitting of a given high-order polynomial. Its advantage is that while filtering and smoothing, it can ensure that the shape and width of the signal are consistent, and can more effectively retain the change information of the signal. It can better maintain the observation information of the data, and it will be more suitable in some occasions that pay attention to data changes. .

This is a one-dimensional filter. If the dimension of x is greater than 1, axis determines the axis on which the filter is applied.

parameter settings

scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)
  • x (array_like):要过滤的数据。If x is not a single or double precision floating point array, it will be converted to type before filtering.numpy.float64
  • window_length (int): The length of the filter window (i.e. the number of coefficients). If mode is ' interp ', window_length must be less than or equal to the size of x.
  • polyorder (int): Order of the polynomial used to fit the samples. Polyorder must be less than window_length.
  • deriv (int, optional): The order of the derivative to be calculated. This must be a non-negative integer. The default value is 0, which means no distinction is made when filtering data.
  • delta (float, optional): The spacing of samples to which the filter will be applied. Only used when deriv > is 0. The default value is 1.0.
  • axis (int, optional): Array x-axis to which the filter applies. The default value is -1.
  • mode (str, optional): Must be ` mirror `, ` constant `, ` nearest `, ` wrap ` or ` interp `. This determines the type of extension used on the fill signal to which the filter is applied. When mode is 'constant', the padding value is specified by cval. See the comments for more details on `mirror`, `constant`, `wrap` and `nearest`. When ` interp ` mode is selected (default), extensions are not used. Instead, a multi-order polynomial fits the last window_length value of the edge, and this polynomial is used to calculate the last window_length output value.
  • cval (scalar, optional): Value padding beyond the edges of the input, if mode is ' constant '. The default value is 0.0.
  • Return value : Y, filtered data. array with the same shape as x

Example

100 numbers were randomly generated and a picture was drawn.

# 调用
import matplotlib.pyplot as plt
import random
import scipy.signal

# 生成随机列表
loss=[random.randint(0,50)for i in range(100)]

plt.figure()
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'

# loss曲线
plt.plot(loss, 'g', linewidth=2, label='loss')
# 平滑
plt.plot(scipy.signal.savgol_filter(loss, 50, 3), 'b', linestyle='--', linewidth=2,label='smooth train loss')

plt.grid(True)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()

 Output result:

 

Official:  scipy.signal.savgol_filter — SciPy v1.10.1 Manual

Guess you like

Origin blog.csdn.net/m0_70813473/article/details/130293479