这似乎是一个非常直接的问题,但我想不出解决方案。假设我有一个y包含 8000 个样本的正弦函数:
import numpy as np
Fs = 8000
f = 1
npts = 8000
x = np.arange(npts)
y = np.sin(2 * np.pi * f * x / Fs)
Run Code Online (Sandbox Code Playgroud)
我想将这个函数下采样到 6000 个样本,所以我尝试了这个回答类似问题的方法......
import math
from scipy import nanmean
#number of samples I want to downsample to
npts2 = 6000
#calculating the number of NaN values to pad to the array
n = math.ceil(float(y.size)/npts2)
pad_size = n*npts2 - len(y)
padded = np.append(y, np.zeros(int(pad_size))*np.NaN)
#downsampling the reshaped …Run Code Online (Sandbox Code Playgroud) 在numpy中我有成千上万的val.我想通过平均相邻值来减小其大小.例如:
a = [2,3,4,8,9,10]
#average down to 2 values here
a = [3,9]
#it averaged 2,3,4 and 8,9,10 together
Run Code Online (Sandbox Code Playgroud)
所以,基本上,我在数组中有n个元素,我想告诉它平均下降到X个值,并且它的平均值如上所述.
有没有办法用numpy做到这一点(已经将它用于其他事情,所以我想坚持下去).
我找到了以下方法来对 python 中的信号进行下采样。我想在 asample_rate为 100.21 时使用此方法,但我认为目前它仅适用于 2 的整数幂。是否可以将频率 100.21 Hz 的信号下采样至 8 Hz?
def interpolateDataTo8Hz(data,sample_rate,startTime):
# Downsample
idx_range = range(0,len(data))
data = data.iloc[idx_range[0::int(sample_rate)/8]]
# Set the index to be 8Hz
data.index = pd.DatetimeIndex(start=startTime,periods = len(data),freq='125L')
# Interpolate all empty values
data = interpolateEmptyValues(data)
return data
def interpolateEmptyValues(data):
cols = data.columns.values
for c in cols:
data[c] = data[c].interpolate()
return data
Run Code Online (Sandbox Code Playgroud)