-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_emp.py
51 lines (39 loc) · 1.5 KB
/
pre_emp.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
import numpy as np
from keras.layers import Lambda,Input,Subtract,Dense
from keras.models import Model
import keras.backend as K
def keras_pre_emp(sig_len,pre_emp_c =0.97):
in0 = Input(shape=(sig_len,))
x1= Lambda (lambda x: x[:,1:])(in0)
x2= Lambda (lambda x: x[:,:-1])(in0)
x3=Lambda(lambda x: 0.97*x)(x2)
x4= Subtract()([x1,x3])
model =Model(in0,x4)
return model
" The following functions come together"
def create_pre_emp_mat(signal_len, pre_emp_c=0.97):
vector_dim =signal_len-1
a = np.zeros((vector_dim, vector_dim), float)
np.fill_diagonal(a, pre_emp_c)
b = np.identity(vector_dim)
b = np.append(np.zeros((vector_dim, 1), float), b, axis=1)
b1 = np.append(a, np.zeros((vector_dim, 1), float), axis=1)
c = b - b1
weight_mat = np.transpose(c)
return weight_mat
def create_pre_emp(signal_len, pre_emp_c=0.97):
in0 = Input(shape=(signal_len,))
w_mat=create_pre_emp_mat(signal_len, deriv_coeff=0.97)
x00 = Dense(signal_len - 1, use_bias=False, trainable=False, weights=[w_mat], activation="linear")(in0)
model= Model(in0,x00)
return model
if __name__=="__main__":
xx = np.random.rand(2000)
print ("linear process no keras")
zz1=xx[1:]-0.97*xx[:-1]
"keras with dense layer"
model =create_pre_emp(2000)
zz= model.predict(np.expand_dims(xx,axis=0))
"keras_no dense layer"
model =keras_pre_emp(2000)
zz2 = model.predict(np.expand_dims(xx, axis=0))