想要计算存储在平坦CSV文件(百万行+)中的X,Y,Z加速度计数据的主频率,次要主频率,例如
我正在尝试使用scipy虽然知道numpy - 要么会这样做。我已经将我的X,Y,Z转换为SMV格式(单幅度矢量),并希望对此应用傅立叶变换,然后使用fftfreq获取频率 - 击败我的位是n和时间步长。我有我想要查看的采样率,赫兹和滚动窗口的大小(10行数据),但不太确定如何将其应用于下面的脚本:
#The three-dimension data collected (X,Y,Z) were transformed into a
#single-dimensional Signal Magnitude Vector SMV (aka The Resultant)
#SMV = x2 + Y2 + Z2
X2 = X['X']*X['X']
Y2 = X['Y']*X['Y']
Z2 = X['Z']*X['Z']
#print X['X'].head(2) #Confirmed worked
#print X2.head(2) #Confirmed worked
combine = [X2,Y2,Z2, Y]
parent = pd.concat(combine, axis=1)
parent['ADD'] = parent.sum(axis=1) #Sum X2,Y2,Z2
sqr = np.sqrt(parent['ADD']) #Square Root of Sum Above
sqr.name = 'SMV'
combine2 = [sqr, Y] #Reduce Dataset to SMV and Class
parent2 = pd.concat(combine2, axis=1)
print parent2.head(4)
"************************* Begin Fourier ****************************"
from scipy import fftpack
X = fftpack.fft(sqr)
f_s = 80 #80 Hertz
samp = 1024 #samples per segment divided by 12.8 secs signal length
n = X.size
timestep = 10
freqs = fftpack.fftfreq(n, d=timestep)
答案 0 :(得分:2)
首先,您需要将数据加载到一个numpy数组中(抱歉,我没有完全按照您的方法):
def load_data():
csvlist = []
times = []
with open('freq.csv') as f:
csvfile = csv.reader(f, delimiter=',')
for i, row in enumerate(csvfile):
timestamp = datetime.datetime.strptime(row[0],"%Y-%m-%d %H:%M:%S.%f")
times.append(timestamp)
csvlist.append(row[1:])
timestep = times[1]-times[0]
csvarr = numpy.array(csvlist, dtype=numpy.float32)
return timestep, csvarr
这可能是更好的方法吗? 然后你需要计算幅度:
rms = numpy.sqrt(numpy.sum(data**2, axis=1))
然后是傅里叶分析:
def fourier(timestep, data):
N = len(data)//2
freq = fftpack.fftfreq(len(data), d=timestep)[:N]
fft = fftpack.fft(data)[:N]
amp = numpy.abs(fft)/N
order = numpy.argsort(amp)[::-1]
return freq[order]
从此返回的是按重要性递减顺序的频率列表。