/
utils_stress_detector.py
90 lines (67 loc) · 2.15 KB
/
utils_stress_detector.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
"""
Utils for Python Stress Detector
Created on 10 Jul 2018
@author: MaxMouse
"""
from scipy.io import wavfile
import emd
import os
import sys, getopt
import matplotlib.pyplot as plt
def plot_data(the_data):
plt.plot(the_data)
plt.show()
def get_audio_data_from_file_absolute_path(input_file):
return wavfile.read(input_file, True)
def extract_emd(dat1):
return emd.emd(dat1, extrapolation=None, nimfs=8, shifting_distance=0.2)
def extract_data(myemd, imf_count):
the_data = []
for freq in myemd:
the_data.append(freq[imf_count - 1])
return the_data
def get_zero_crossings(the_emd):
count_zeros = 0
imf_count = len(the_emd[0])
if imf_count > 3:
imf_count = imf_count - 1
the_data = extract_data(the_emd, imf_count)
for i in range(len(the_data)-1):
if the_data[i] > 0 and the_data[i+1] < 0:
count_zeros = count_zeros + 1
elif the_data[i] < 0 and the_data[i+1] > 0:
count_zeros = count_zeros + 1
return count_zeros
def get_stress_tremor_average_from_data(the_data, rate1):
the_emd = extract_emd(the_data)
#plot_data(the_emd)
count_zeros = get_zero_crossings(the_emd)
audio_time_length = len(the_data)/float(rate1)
stress_tremor_avg = count_zeros - 1
stress_tremor_avg = stress_tremor_avg/audio_time_length
return stress_tremor_avg
def is_under_stress(stress_tremor_avg):
under_stress = False
if stress_tremor_avg > 12:
under_stress = True
elif stress_tremor_avg < 8:
under_stress = True
return under_stress
def get_input_file_from_argv(argv):
input_file = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile="])
except getopt.GetoptError:
print('python3 StressDetectorDesktop.py -i <inputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python3 StressDetectorDesktop.py -i <inputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
input_file = arg
if not os.path.isfile(input_file):
# file does NOT exist
print('File does NOT exist, will exit')
sys.exit(2)
return input_file