#if __name__ == '__main__' :
from scipy.signal import tf2ss
import numpy as np
previousSpnds =0
previousFiltered =0
count = 0
DEBUG = False
k = 1 #1.415*10**-20/60 #A.cm2.min.n-1
tau_p = 23.68/60 #min
tau_d = 315.0/60 #min
T = 1 #sampling period is 1 min
A,B,C,D = tf2ss([k*tau_p, k], [tau_d, 1])
# if DEBUG:
# print "A: ",A
# print "B: ",B
# print "C: ",C
# print "D: ",D
ass=(2+A*T)/(2-A*T)
cbss=(C*B*T-A*D*T-2*D)/(2-A*T)
dss=(C*B*T-A*D*T+2*D)/(2-A*T)
[docs]def RealTimeFilter(currentSpnds, isInitial=False):
'''
Real time implementation of Vanadium TF filter.
Cobalt data is passed to this function one row at a time.
Parameters
----------
currentSpnds : list
Row of SPND( Co ) values for a time instant.
isInitial : bool
Optional Flag. Default is False.
Returns
--------
filteredSpnds : list
Filtered sensor data after passing via Vanadium TF (based upon previous
data and current data)
'''
global previousSpnds
global previousFiltered
global count
global a,cb,d
## y_k=a*y_{k-1} + cb*u_{k-1} + d*u_{k}
if isInitial:
previousSpnds = np.zeros(shape=(1,len(currentSpnds)))
previousFiltered = np.zeros(shape=(1,len(currentSpnds)))
#previousFiltered = np.zeros(shape=(1,5))
for spndj in range(len(currentSpnds)):
if currentSpnds[spndj]<0 or np.isnan(currentSpnds[spndj]):
currentSpnds[spndj]=0
else:
for spndj in range(len(currentSpnds)):
if currentSpnds[spndj]<0 or np.isnan(currentSpnds[spndj]):
currentSpnds[spndj]=previousSpnds[spndj]
# print "VV : ", previousFiltered.shape
# print type(a)
# print type(previousFiltered)
# print a
i1 = ass*previousFiltered
i2 = cbss*previousSpnds
i3 = dss*currentSpnds
#print "Instant : ", count
#print "shape i1", i1.shape
#print "shape i2", i2.shape
#print "shape i3", i3.shape
#print "i3",i3
filteredSpnds = i1 + i2 + i3
#filteredSpnds = a*previousFiltered + cb*previousSpnds + d*currentSpnds
count += 1
previousFiltered = np.copy(filteredSpnds)
previousSpnds = currentSpnds
return filteredSpnds
[docs]def foo():
'''
Super function foo. Makes us all insane.
Parameters
----------
None : None
Doesn't take input
'''
global ass,cbss,dss
if DEBUG:
print "a :", ass
print "cb:", cbss
print "d :", dss
mat = [[6, -9, 12, 15, 18],\
[ 5, -8, 11, -14, 20],\
[ 4, 7, 10, 16, 19],\
[ 3, 9, 12, 15, -18],\
[ 5, 8, 11, 14, 17],\
[ 11, 16, 5, 14, -07]]
mat = np.array(mat)
b = RealTimeFilter(mat[0,:], isInitial=True)
print "b isInitial",b
for i in range(1,6):
b = RealTimeFilter(mat[i,:])
print "b not isInitial",b
if __name__ == '__main__':
foo()