重采样 resample

python实现重采样

官网文档

网址:https://github.com/scipy/scipy/blob/v1.4.1/scipy/signal/signaltools.py

def resample(x, num, t=None, axis=0, window=None):
    """
    Resample `x` to `num` samples using Fourier method along the given axis.
    The resampled signal starts at the same value as `x` but is sampled
    with a spacing of ``len(x) / num * (spacing of x)``.  Because a
    Fourier method is used, the signal is assumed to be periodic.
    Parameters
    ----------
    x : array_like
        The data to be resampled.
    num : int
        The number of samples in the resampled signal.
    t : array_like, optional
        If `t` is given, it is assumed to be the equally spaced sample
        positions associated with the signal data in `x`.
    axis : int, optional
        The axis of `x` that is resampled.  Default is 0.
    window : array_like, callable, string, float, or tuple, optional
        Specifies the window applied to the signal in the Fourier
        domain.  See below for details.
    Returns
    -------
    resampled_x or (resampled_x, resampled_t)
        Either the resampled array, or, if `t` was given, a tuple
        containing the resampled array and the corresponding resampled
        positions.
    
See Also
    --------
    decimate : Downsample the signal after applying an FIR or IIR filter.
    resample_poly : Resample using polyphase filtering and an FIR filter.
    Notes
    -----
    The argument `window` controls a Fourier-domain window that tapers
    the Fourier spectrum before zero-padding to alleviate ringing in
    the resampled values for sampled signals you didn't intend to be
    interpreted as band-limited.
    If `window` is a function, then it is called with a vector of inputs
    indicating the frequency bins (i.e. fftfreq(x.shape[axis]) ).
    If `window` is an array of the same length as `x.shape[axis]` it is
    assumed to be the window to be applied directly in the Fourier
    domain (with dc and low-frequency first).
    For any other type of `window`, the function `scipy.signal.get_window`
    is called to generate the window.
    The first sample of the returned vector is the same as the first
    sample of the input vector.  The spacing between samples is changed
    from ``dx`` to ``dx * len(x) / num``.
    If `t` is not None, then it is used solely to calculate the resampled
    positions `resampled_t`
    As noted, `resample` uses FFT transformations, which can be very
    slow if the number of input or output samples is large and prime;
    see `scipy.fft.fft`.
   
 Examples
    --------
    Note that the end of the resampled data rises to meet the first
    sample of the next cycle:
    >>> from scipy import signal
    >>> x = np.linspace(0, 10, 20, endpoint=False)
    >>> y = np.cos(-x**2/6.0)
    >>> f = signal.resample(y, 100)
    >>> xnew = np.linspace(0, 10, 100, endpoint=False)
    >>> import matplotlib.pyplot as plt
    >>> plt.plot(x, y, 'go-', xnew, f, '.-', 10, y[0], 'ro')
    >>> plt.legend(['data', 'resampled'], loc='best')
    >>> plt.show()
    """
    x = np.asarray(x)
    Nx = x.shape[axis]

文档理解

len(x) / num * (spacing of x) = 新的采样间隔y
主要是这个公式,了解一个这段话就能理解,这代表的是新的采样间隔,
num代表的是重采样之后的采样个数。将代数整理一下。
len(x) / (spacing of x) =num / (spacing of y)
既然要重采样到统一的频率,那么两者的间隔是一样的。
len(x) / spacing of x = num / spacing of y = N,比例一致

举个例子,如果一开始长度是1250,250hz的一段数据,重采样到200hz,那么新的数据长度就是1000

列算式如下:
1250 / 250 = 1000 / 200 ( = 5 都等于5)

(原理是使用多相滤波和FIR滤波器重新采样。)

代码实现

#注意数据类型的要求,num是int类型
EcgData = wfdb.rdsamp(path, sampfrom=1250 * k, sampto = 1250 + 1250 * k)[0]
signal_resample = signal.resample(EcgData,int((len(EcgData))*(200/250)),axis = 0)
signal_resample = np.array(signal_resample)
#这里读取的长度是1250个数据,原数据采样率是250hz,需要重采样到200hz

猜你喜欢

转载自blog.csdn.net/HJ33_/article/details/120646844