-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ilia.shumailov
committed
Oct 23, 2017
1 parent
2dcff8f
commit 1312d9d
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import numpy as np | ||
import nolds | ||
|
||
def IEMG(data): | ||
return np.sum(np.square(data.astype(np.float))) | ||
|
||
def MAV(data): | ||
return np.mean(np.absolute(data.astype(np.float))) | ||
|
||
def MMAV1(data): | ||
N = (data.shape[0]) | ||
s = 0.0 | ||
for i in range(N): | ||
if 0.25*N <= i <= 0.75*N: | ||
w = 1 | ||
else: | ||
w = 0.5 | ||
s += w * abs(float(data[i])) | ||
return s/N | ||
|
||
def MMAV2(data): | ||
N = (data.shape[0]) | ||
s = 0.0 | ||
for i in range(N): | ||
if 0.25*N <= i <= 0.75*N: | ||
w = 1 | ||
elif 0.25*N > i: | ||
w = 4*i/N | ||
else: | ||
w = 4*(i-N)/N | ||
s += w * abs(float(data[i])) | ||
return s/N | ||
|
||
def VAR(data): | ||
return np.var([x**2 for x in data]) | ||
|
||
def RMS(data): | ||
return np.sqrt(np.mean(np.square(data))) | ||
|
||
def WL(data): | ||
data = data.astype(np.float) | ||
N = float(data.shape[0]) | ||
return sum([ abs(data[i+1]-data[i]) for i in range(int(N)-1)]) | ||
|
||
def ZC(data): | ||
mdata = data.copy().astype(np.float) - 0.5 | ||
return (np.diff(np.sign(mdata)) != 0).sum() | ||
|
||
def SSC(data): | ||
mdata = data.copy().astype(np.float) - 0.5 | ||
return sum(1 for i in range(1, len(mdata)-1) if mdata[i-1]*mdata[i]<0 and mdata[i]*mdata[i+1]<0) | ||
|
||
def WAMP(data, threashold=0.2): | ||
mdata = data.copy().astype(np.float) - 0.5 | ||
return sum(1 for i in range(1, len(mdata)) if abs(mdata[i-1]*mdata[i])>= threashold) | ||
|
||
def STDDEV(data): | ||
data = data.astype(np.float) | ||
N = float(len(data)) | ||
mean = np.mean(data) | ||
|
||
summation = sum([(x - mean)**2 for x in data]) | ||
qq = summation*(1/(N-1)) | ||
return np.sqrt(qq) | ||
|
||
def SSI(data): | ||
return np.sum(np.square(data)) | ||
|
||
def absval_temp(data): | ||
|
||
N = float(data.shape[0]) | ||
|
||
summation = sum([ abs(data[i+1]-data[i]) for i in range(int(N)-1)]) | ||
|
||
return (1/(N-1))*summation | ||
|
||
def mean_absval_second_diff(data): | ||
N = float(data.shape[0]) | ||
|
||
summation = sum([ abs(data[i+2]-data[i]) for i in range(int(N)-2)]) | ||
|
||
return (1/(N-2))*summation | ||
|
||
def mean_absval_third_diff(data): | ||
N = float(data.shape[0]) | ||
|
||
summation = sum([ abs(data[i+3]-data[i]) for i in range(int(N)-3)]) | ||
|
||
return (1/(N-3))*summation | ||
|
||
def mean_absval_fourth_diff(data): | ||
N = float(data.shape[0]) | ||
|
||
summation = sum([ abs(data[i+4]-data[i]) for i in range(int(N)-4)]) | ||
|
||
return (1/(N-4))*summation | ||
|
||
def getmean(data): | ||
return np.mean(data.astype(np.float)) | ||
|
||
def ENT(data): | ||
return nolds.sampen(data.astype(np.float)) | ||
|
||
def get_features_from_stream( | ||
strea, | ||
fncs = [MAV, MMAV1, MMAV2, SSI, VAR, RMS, WL, ZC, SSC, WAMP, STDDEV] | ||
): | ||
|
||
return a([ f(strea) for f in fncs]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# The library with audio processing tools | ||
|
||
from collections import defaultdict | ||
import audio_features as fea | ||
import file_processing as iop | ||
import scikits.audiolab | ||
|
||
def split_into_channels(sig, num_chan=2): | ||
outputs = defaultdict(list) | ||
|
||
for i in xrange(0, len(sig), num_chan): | ||
for j in xrange(num_chan): | ||
outputs[j].append(sig[i+j]) | ||
|
||
return [a(outputs[x]) for x in outputs] | ||
|
||
def play_signal(signal, fs = 44100): | ||
scikits.audiolab.play(signal, fs=fs) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# The utilities used to operate on files | ||
|
||
import numpy as np | ||
|
||
|
||
def load_16bit_pcm(path, lower_endian = True): | ||
# Can easily abstract it away. Maybe do it later | ||
endianness = '<' if lower_endian else '>' | ||
return np.memmap(path, dtype='{}h'.format(endianness), mode='r') | ||
|
||
def write_16bit_pcm(data, path, mx = 32767, lower_endian = True): | ||
outf = open(path, "w") | ||
|
||
endianness = '<' if lower_endian else '>' | ||
for dt in data: | ||
outf.write(struct.path('{}h'.format(endianness), int(dt*mx))) | ||
|
||
outf.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
from collections import defaultdict | ||
|
||
def kfold(X, y, num_k, clfs_names): | ||
|
||
d = defaultdict(list) | ||
for sm_c, name in clfs_names: | ||
|
||
scores = [] | ||
kf = KFold(n_splits=num_splits, shuffle=True) | ||
|
||
for train_index, test_index in kf.split(X): | ||
X_train, X_test = a(X[train_index]), a(X[test_index]) | ||
y_train, y_test = a(y[train_index]), a(y[test_index]) | ||
|
||
cur = sm_c() | ||
cur = cur.fit(X_train, y_train) | ||
y_res = cur.predict(X_test) | ||
|
||
cur_acc = f1_score(y_test, y_res, average="macro") | ||
scores.append(cur_acc) | ||
|
||
scores = np.array(scores) | ||
d[name].append("KFOLD: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) | ||
|
||
return d | ||
|
||
|