forked from kiloser/kalman_rako
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acfilter_test.py
81 lines (73 loc) · 1.93 KB
/
acfilter_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
#this file is used to verify the butterworth filter implented on accelerator.
#the feature is sucessfully realized on stm32,so this file is not useful anymore.
#but data still can be ploted for eassy
import matplotlib.pyplot as plt
import scipy.fftpack
import numpy as np
import scipy
from scipy import signal
plt.rc('font',size=14)
def simple_low_pass(acceldata):
udata=0
lastdata=0
dt=0.001
fcut=10
a=(2*np.pi*fcut*dt)/(2*np.pi*fcut*dt+1)
data=[]
for tmp in acceldata:
if udata==0:
data.append(tmp)
udata=tmp
lastdata=tmp
else:
udata=(1-a)*udata+a*lastdata
lastdata=tmp-udata
data.append(lastdata)
return data
fd=open('data\\raw_accel2.txt','r')
accelxdata=[]
accelydata=[]
accelzdata=[]
for line in fd.readlines():
data=line.strip('\n').split('\t')
data=[float(ii) for ii in data]
accelxdata.extend([data[0]])
accelydata.extend([data[1]])
accelzdata.extend([data[2]])
fd.close()
# Number of samplepoints
N = len(accelxdata)
# sample spacing
T = 1.0 / 100.0
x = np.linspace(0.0, N*T, N)
y = [ii for ii in accelxdata]
fig1=plt.figure(1)
ax=fig1.add_subplot(121)
ax.set_ylim(-2,0)
ax.set_ylabel('Accel x')
ax.set_xlabel('Time')
ax.plot(x,y)#plt raw data
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
fig2=plt.figure(2)
ax2=fig2.add_subplot(121)
ax2.set_ylabel('Amplitude')
ax2.set_xlabel('Frequency')
ax2.plot(xf[1:], 2.0/N * np.abs(yf[:N//2][1:]))
nyq = 0.5 * 100
low = 10 / nyq
b, a = signal.butter(2, low, btype='low')
y = signal.lfilter(b, a, y)
ax1=fig1.add_subplot(122)
ax1.set_ylim(-2,0)
ax1.set_ylabel('Accel x')
ax1.set_xlabel('Time')
ax1.plot(x,y)#plt filtered data
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
ax2=fig2.add_subplot(122)
ax2.set_ylabel('Amplitude')
ax2.set_xlabel('Frequency')
ax2.plot(xf[1:], 2.0/N * np.abs(yf[:N//2][1:]))
plt.show()