-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModel.py
196 lines (153 loc) · 5.31 KB
/
DataModel.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Model to preict Knee Health Based on EMG, pressure and sound data
# import flask as fl
import requests as rq
# import json as js
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import spectrogram
# import pywt
# from keras.models import Sequential
# from keras.layers import Dense
# from keras.optimizers import Adam
# print("hello")
IP = "192.168.151.251"
# get data through GET request from OM2M server
url_sound = "http://"+IP+"/~/in-cse/in-name/AE-TEST/User-Patterns/Microphone/la"
# url_pressure = "http://192.168.137.1:5089/~/in-cse/in-name/AE-TEST/Peizo_Sensor/"
url_health = "http://"+IP+"/~/in-cse/in-name/AE-TEST/User-Patterns/Health_Sensor/la"
url_flex = "http://"+IP+"/~/in-cse/in-name/AE-TEST/User-Patterns/Flex_Sensor/la"
# url_emg = "http://192.168.137.1:5089/~/in-cse/in-name/AE-TEST/EMG_Sensor/"
payload = {}
headers = {
'X-M2M-Origin': 'admin:admin',
'Accept': 'application/json'
}
sound_data = rq.request("GET", url_sound, headers=headers, data=payload)
print(sound_data.status_code)
# print(sound_data)
# pressure_data = rq.request("GET", url_pressure, headers=headers, data=payload)
health_data = rq.request("GET", url_health, headers=headers, data=payload)
print(health_data.status_code)
# print(health_data)
# emg_data = rq.request("GET", url_emg, headers=headers, data=payload)
flex_data = rq.request("GET", url_flex, headers=headers, data=payload)
print(flex_data.status_code)
# print(flex_data)
sound_data = sound_data.json()['m2m:cin']['con'] # extract data from json file
# pressure_data = pressure_data.json()['m2m:cin']['con'] # extract data from json file
health_data = health_data.json()['m2m:cin']['con'] # extract data from json file
# emg_data = emg_data.json()['m2m:cin']['con'] # extract data from json file
flex_data = flex_data.json()['m2m:cin']['con']
print(sound_data)
# print(pressure_data)
print(health_data)
# print(emg_data)
print(flex_data)
# extract data from json file
# wavelet transform for emg data
# wavelet = 'db4'
# level = 4
# coeffs = pywt.wavedec(emg_data, wavelet, level=level)
# make a spectrogram for the sound data
sample_rate = 20 # Hz
nfft = 256 # number of samples per window
noverlap = 128 # number of samples that overlap between windows
window = "hamming"
freq, time, Sxx = spectrogram(sound_data, fs=sample_rate, nfft=nfft, noverlap=noverlap, window=window)
# display graphs for all datas extracted
# Raw Sound Data Graph
plt.figure(figsize=(10, 6))
plt.plot(sound_data)
plt.xlabel('Time (s)')
plt.ylabel('Voltage (mV)')
plt.title('Raw Sound Data')
plt.colorbar(label='mV')
plt.show()
# Processed Sound Data Graph
plt.figure(figsize=(10, 6))
plt.pcolormesh(time, freq, 10 * np.log10(Sxx), shading='auto')
plt.colorbar(label='dB')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.title('Spectrogram of Voltage Data')
plt.show()
'''
# make a graph for the pressure data
threshold = 3000 # threshold for pressure sensor
digital_pressure = [max(pressure_data) if pressure > threshold else 0 for pressure in pressure_data] # digital pressure data
plt.figure(figsize=(10, 6))
plt.plot(digital_pressure)
plt.plot(pressure_data)
plt.legend(['Digital Threshold Reading', 'Analog Pressure'])
plt.xlabel('Time (s)')
plt.ylabel('Pressure (mV)')
plt.title('Pressure Data')
plt.colorbar(label='mV')
plt.show()
# make a graph for the Raw-EMG data
plt.figure(figsize=(10, 6))
plt.plot(emg_data)
plt.xlabel('Time (s)')
plt.ylabel('Voltage (mV)')
plt.title('EMG Data')
plt.colorbar(label='mV')
plt.show()
# make a graph for the Processed-EMG data
for i in range(1, level+2):
plt.subplot(level+2, 1, i+1)
plt.plot(coeffs[i-1])
plt.title(f'Detail {i}' if i < level+1 else 'Approximation')
plt.tight_layout()
plt.show()
'''
# make a graph for the flex sensor data
plt.figure(figsize=(10, 6))
plt.plot(flex_data)
plt.xlabel('Time (s)')
plt.ylabel('Voltage (mV)')
plt.title('Flex Sensor Data')
plt.colorbar(label='mV')
plt.show()
# make health data graph
heart_rate = health_data[2]
blood_oxygen = health_data[3]
sys_pressure = health_data[0]
dia_pressure = health_data[1]
plt.figure(figsize=(10, 6))
plt.plot(heart_rate)
plt.xlabel('Time (s)')
plt.ylabel('Heart Rate (bpm)')
plt.title('Heart Rate')
plt.colorbar(label='bpm')
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(blood_oxygen)
plt.xlabel('Time (s)')
plt.ylabel('Blood Oxygen (%)')
plt.title('Blood Oxygen')
plt.colorbar(label='%')
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(sys_pressure)
plt.plot(dia_pressure)
plt.legend(['Systolic', 'Diastolic'])
plt.xlabel('Time (s)')
plt.ylabel('Blood Pressure (mmHg)')
plt.title('Blood Pressure')
plt.colorbar(label='mmHg')
plt.show()
# neural network that takes in (EMG, pressure, sound) and predicts the knee health in score of 100
# Generating random input data
# You can replace this with your actual input data
# np.random.seed(42)
# X = np.random.rand(100, 3) # 100 samples, 3 features
# # Generating random output data within the range of 0-100
# y = np.random.uniform(0, 100, 100)
# # Creating a neural network model
# model = Sequential()
# model.add(Dense(8, input_dim=3, activation='relu')) # 8 neurons in the hidden layer
# model.add(Dense(1, activation='linear')) # Output layer with linear activation
# # Compiling the model
# model.compile(loss='mean_squared_error', optimizer=Adam(learning_rate=0.001))
# # Training the model
# model.fit(X, y, epochs=50, batch_size=10, verbose=1)