-
Notifications
You must be signed in to change notification settings - Fork 0
/
csd_est_funcs.py
95 lines (65 loc) · 2.35 KB
/
csd_est_funcs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#Sergey Gratiy's CSD function
import numpy as np
import scipy.linalg as la
from math import *
import pylab as plt
def inverse_tikhonov(A,SNR):
'''
calcuate matrix W which where CSD_est=W*LFP
R = resolution matrix R=W*A
'''
At=A.T
AAt = np.dot(A,At)
nz = A.shape[0]
np.mean(np.diag(AAt),axis=0)
AAt_reg = AAt+1/SNR**2*np.mean(np.diag(AAt),axis=0)*np.eye(nz)
#print AAt_reg[0]
invAAt_reg = la.inv(AAt_reg)
W = np.dot(At,invAAt_reg)
R = np.dot(W,A)
return [W,R]
def forward_operator(zs,b,sigma):
'''
caclulate matrix A where LFP=A*CSD
'''
K = np.zeros((len(zs),len(zs)))
dz = zs[1]-zs[0]
for iz,z in enumerate(zs):
for izp, zp in enumerate(zs):
K[iz,izp] = sqrt(((z-zp)/b)**2 + 1) - abs((z-zp)/b)
A = b*dz/(2*sigma)*K
return [A,K]
def show_operators(A,A0,W,W0,R,R0):
fig100 = plt.figure(100)
plt.subplot(2,3,1);
VspanA = np.array([-1, 1])*abs(A).max()
plt.imshow(A, vmin = VspanA[0], vmax = VspanA[1],cmap='jet'); plt.colorbar();
plt.title('forward operator')
plt.subplot(2,3,4);
VspanA0 = np.array([-1, 1])*abs(A0).max()
plt.imshow(A0, vmin = VspanA0[0], vmax = VspanA0[1],cmap='jet'); plt.colorbar();
plt.title('forward operator 0')
plt.subplot(2,3,2);
VspanW = np.array([-1, 1])*abs(W).max()
plt.imshow(W, vmin = VspanW[0], vmax = VspanW[1],cmap='jet'); plt.colorbar();
plt.title('Inverse operator')
plt.subplot(2,3,3);
VspanR = np.array([-1, 1])*abs(R).max()
plt.imshow(R, vmin = VspanR[0], vmax = VspanR[1],cmap='jet'); plt.colorbar();
plt.title('Resolution matrix')
plt.subplot(2,3,5);
VspanW0 = np.array([-1, 1])*abs(W0).max()
plt.imshow(W0, vmin = VspanW0[0], vmax = VspanW0[1],cmap='jet'); plt.colorbar();
plt.title('Inverse operator 0')
plt.subplot(2,3,6);
VspanR0 = np.array([-1, 1])*abs(R0).max()
plt.imshow(R0, vmin = VspanR0[0], vmax = VspanR0[1],cmap='jet'); plt.colorbar();
plt.title('Resolution matrix 0')
fig101 = plt.figure(101)
nw=W.shape[0]
plt.subplot(2,1,1);
plt.plot(W[:,[0,nw/2,nw-1]],'.-')
plt.title('Inverse operator for different channels')
plt.subplot(2,1,2);
plt.plot(W0[:,[0,nw/2,nw-1]],'.-')
return [fig100,fig101]