-
Notifications
You must be signed in to change notification settings - Fork 21
/
eig.py
28 lines (22 loc) · 842 Bytes
/
eig.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
#!/usr/bin/env python3
''' general eigenvalue decomposition'''
import numpy as np
from scipy import linalg
__author__ = "Michael Hersche and Tino Rellstab"
__email__ = "herschmi@ethz.ch,tinor@ethz.ch"
def gevd(x1,x2,no_pairs):
'''Solve generalized eigenvalue decomposition
Keyword arguments:
x1 -- numpy array of size [NO_channels, NO_samples]
x2 -- numpy array of size [NO_channels, NO_samples]
no_pairs -- number of pairs of eigenvectors to be returned
Return: numpy array of 2*No_pairs eigenvectors
'''
ev,vr= linalg.eig(x1,x2,right=True)
evAbs = np.abs(ev)
sort_indices = np.argsort(evAbs)
chosen_indices = np.zeros(2*no_pairs).astype(int)
chosen_indices[0:no_pairs] = sort_indices[0:no_pairs]
chosen_indices[no_pairs:2*no_pairs] = sort_indices[-no_pairs:]
w = vr[:,chosen_indices] # ignore nan entries
return w