-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata.py
403 lines (317 loc) · 15.1 KB
/
data.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import asyncio
import websockets
import pandas as pd
import json
import numpy as np
import scipy.signal as signal
import time
# Low-pass filter function to remove high-frequency noise
def low_pass_filter(ecg_signal, fs=100, cutoff=1.0):
nyquist = 0.5 * fs
normal_cutoff = cutoff / nyquist
b, a = signal.butter(1, normal_cutoff, btype='low', analog=False)
filtered_ecg = signal.filtfilt(b, a, ecg_signal)
return filtered_ecg
# Bandpass filter and peak detection
def detect_r_peaks(ecg_signal, fs=100):
lowcut = 0.5 # Lower bound of heart rate (0.5 Hz = 30 BPM)
highcut = 5.0 # Upper bound of heart rate (5 Hz = 300 BPM)
nyquist = 0.5 * fs
low = lowcut / nyquist
high = highcut / nyquist
b, a = signal.butter(1, [low, high], btype='band')
filtered_ecg = signal.filtfilt(b, a, ecg_signal)
# Find R-peaks (local maxima) in the filtered ECG signal
peaks, _ = signal.find_peaks(filtered_ecg, distance=fs / 2) # Adjust distance for heart rate
return peaks
# Function to calculate BPM
def calculate_bpm(peaks, sample_rate=100):
rr_intervals = np.diff(peaks) / sample_rate # RR intervals in seconds
bpm = 60 / np.mean(rr_intervals) if len(rr_intervals) > 0 else 0
return bpm
# Convert raw ADC values to voltage
def convert_to_voltage(ecg_signal, adc_resolution=1024, reference_voltage=3.3):
return (ecg_signal / adc_resolution) * reference_voltage
# Process and send data over WebSocket
async def process_and_send_data_one_by_one(csv_file_path, uri):
# Load ECG data from CSV
ecg_data = pd.read_csv(csv_file_path)
# Extract ECG values and timestamps
timestamps = ecg_data['timestamp'].values
ecg_values = ecg_data['ecg_value'].values
# Convert ECG values to voltage
voltages = convert_to_voltage(np.array(ecg_values))
# Sampling frequency estimation
fs = int(1 / (timestamps[1] - timestamps[0])) # Assumes uniform sampling
print(f"Sampling frequency (fs): {fs} Hz")
# Filter and process ECG data
filtered_ecg = low_pass_filter(ecg_values, fs)
peaks = detect_r_peaks(filtered_ecg, fs)
bpm = calculate_bpm(peaks, fs)
print(f"Calculated BPM: {bpm}")
# Use a fixed delay for testing (2 messages per second)
fixed_delay = 0.01 # Adjust this value to control data rate (in seconds)
last_sent_index = 0 # Track the last sent index
# Loop to handle WebSocket reconnections
while True:
try:
async with websockets.connect(uri, ping_interval=None, ping_timeout=4200) as websocket:
print("Connected to WebSocket server.")
last_sent_time = time.time()
for index in range(last_sent_index, len(timestamps)):
timestamp = timestamps[index]
voltage = voltages[index]
current_time = time.time()
print(f"Time since last message: {current_time - last_sent_time:.3f} seconds")
last_sent_time = current_time
message = json.dumps({
"timestamp": timestamp,
"ecg": [voltage], # Send as a list
"bpm": bpm
})
await websocket.send(message)
print(f"Sent: {message}")
last_sent_index = index + 1 # Update the last sent index
await asyncio.sleep(fixed_delay)
break # Exit loop after successfully sending all data
except websockets.exceptions.ConnectionClosedOK:
print("Connection closed by the server. Reconnecting...")
await asyncio.sleep(2) # Wait before reconnecting
except websockets.exceptions.ConnectionClosedError as e:
print(f"Unexpected connection closure: {e}. Retrying...")
await asyncio.sleep(2)
except Exception as e:
print(f"Unexpected error: {e}. Retrying...")
await asyncio.sleep(5)
if __name__ == "__main__":
csv_file_path = r"C:\Users\Neu\Desktop\vs\bita\ecg_data\ecg_log.csv"
#csv_file_path = r"C:\Users\Neu\Downloads\SampleECG.csv" # Path to the uploaded CSV file
websocket_uri = "ws://localhost:8000/ws/3" # Replace with your WebSocket server URI
try:
asyncio.run(process_and_send_data_one_by_one(csv_file_path, websocket_uri))
except KeyboardInterrupt:
print("Shutting down gracefully...")
# import asyncio
# import websockets
# import pandas as pd
# import json
# import numpy as np
# import scipy.signal as signal
# import time
# # Low-pass filter function to remove high-frequency noise
# def low_pass_filter(ecg_signal, fs=100, cutoff=1.0):
# nyquist = 0.5 * fs
# normal_cutoff = cutoff / nyquist
# b, a = signal.butter(1, normal_cutoff, btype='low', analog=False)
# filtered_ecg = signal.filtfilt(b, a, ecg_signal)
# return filtered_ecg
# # Bandpass filter and peak detection
# def detect_r_peaks(ecg_signal, fs=100):
# lowcut = 0.5 # Lower bound of heart rate (0.5 Hz = 30 BPM)
# highcut = 5.0 # Upper bound of heart rate (5 Hz = 300 BPM)
# nyquist = 0.5 * fs
# low = lowcut / nyquist
# high = highcut / nyquist
# b, a = signal.butter(1, [low, high], btype='band')
# filtered_ecg = signal.filtfilt(b, a, ecg_signal)
# # Find R-peaks (local maxima) in the filtered ECG signal
# peaks, _ = signal.find_peaks(filtered_ecg, distance=fs / 2) # Adjust distance for heart rate
# return peaks
# # Function to calculate BPM
# def calculate_bpm(peaks, sample_rate=100):
# rr_intervals = np.diff(peaks) / sample_rate # RR intervals in seconds
# bpm = 60 / np.mean(rr_intervals) if len(rr_intervals) > 0 else 0
# return bpm
# # Convert raw ADC values to voltage
# def convert_to_voltage(ecg_signal, adc_resolution=1024, reference_voltage=3.3):
# return (ecg_signal / (adc_resolution )) * reference_voltage
# # Process and send data over WebSocket
# async def process_and_send_data_one_by_one(csv_file_path, uri):
# # Load ECG data from CSV
# ecg_data = pd.read_csv(csv_file_path)
# # Extract ECG values and timestamps
# timestamps = ecg_data['timestamp'].values
# ecg_values = ecg_data['ecg_value'].values
# # Convert ECG values to voltage
# voltages = convert_to_voltage(np.array(ecg_values))
# # Sampling frequency estimation
# fs = int(1 / (timestamps[1] - timestamps[0])) # Assumes uniform sampling
# print(f"Sampling frequency (fs): {fs} Hz")
# # Filter and process ECG data
# filtered_ecg = low_pass_filter(ecg_values, fs)
# peaks = detect_r_peaks(filtered_ecg, fs)
# bpm = calculate_bpm(peaks, fs)
# print(f"Calculated BPM: {bpm}")
# # Use a fixed delay for testing (2 messages per second)
# fixed_delay = 0.01# Adjust this value to control data rate (in seconds)
# # Loop to handle WebSocket reconnections
# while True:
# try:
# async with websockets.connect(uri, ping_interval=20, ping_timeout=10) as websocket:
# print("Connected to WebSocket server.")
# last_sent_time = time.time()
# for timestamp, voltage in zip(timestamps, voltages):
# current_time = time.time()
# print(f"Time since last message: {current_time - last_sent_time:.3f} seconds")
# last_sent_time = current_time
# # Prepare single data unit, ensure ecg is sent as a list
# message = json.dumps({
# "timestamp": timestamp,
# "ecg": [voltage], # Send as a list
# "bpm": bpm
# })
# # Send the data
# await websocket.send(message)
# print(f"Sent: {message}")
# # Apply fixed delay
# await asyncio.sleep(fixed_delay) # Use fixed delay to control sending rate
# # Exit loop after successfully sending all data
# break
# except websockets.exceptions.ConnectionClosedOK:
# print("Connection closed by the server. Reconnecting...")
# await asyncio.sleep(2) # Wait before reconnecting
# except websockets.exceptions.ConnectionClosedError as e:
# print(f"Unexpected connection closure: {e}. Retrying...")
# await asyncio.sleep(2)
# except Exception as e:
# print(f"Unexpected error: {e}. Retrying...")
# await asyncio.sleep(5)
# if __name__ == "__main__":
# csv_file_path = r"C:\Users\Neu\Desktop\vs\bita\ecg_data\ecg_log 1.csv"
# #csv_file_path = r"C:\Users\Neu\Desktop\vs\bita\ecg_data\ecg_log sorted.csv"
# #csv_file_path = r"C:\Users\Neu\Downloads\SampleECG.csv" # Path to the uploaded CSV file
# websocket_uri = "ws://localhost:8000/ws/3" # Replace with your WebSocket server URI
# try:
# asyncio.run(process_and_send_data_one_by_one(csv_file_path, websocket_uri))
# except KeyboardInterrupt:
# print("Shutting down gracefully...")
# import asyncio
# import websockets
# import serial
# import json
# import numpy as np
# import scipy.signal as signal
# # Low-pass filter function to remove high-frequency noise
# def low_pass_filter(ecg_signal, fs=100, cutoff=1.0):
# nyquist = 0.5 * fs
# normal_cutoff = cutoff / nyquist
# b, a = signal.butter(1, normal_cutoff, btype='low', analog=False)
# filtered_ecg = signal.filtfilt(b, a, ecg_signal)
# return filtered_ecg
# # Bandpass filter and peak detection
# def detect_r_peaks(ecg_signal):
# lowcut = 0.5 # Lower bound of heart rate (0.5 Hz = 30 BPM)
# highcut = 5.0 # Upper bound of heart rate (5 Hz = 300 BPM)
# fs = 100 # Sample rate (100 Hz)
# nyquist = 0.5 * fs
# low = lowcut / nyquist
# high = highcut / nyquist
# b, a = signal.butter(1, [low, high], btype='band')
# filtered_ecg = signal.filtfilt(b, a, ecg_signal)
# # Find R-peaks (local maxima) in the filtered ECG signal
# peaks, _ = signal.find_peaks(filtered_ecg, distance=fs / 2) # Adjust distance for heart rate
# return peaks
# # Function to calculate BPM
# def calculate_bpm(peaks, sample_rate=100):
# rr_intervals = np.diff(peaks) / sample_rate # RR intervals in seconds
# bpm = 60 / np.mean(rr_intervals) if len(rr_intervals) > 0 else 0
# return bpm
# async def send_data_from_arduino():
# # Connect to Arduino over serial
# ser = serial.Serial('COM7', 9600) # Adjust port to your system
# uri = "ws://localhost:8000/ws/3"
# ecg_signal = [] # Buffer for ECG data
# noise_threshold = 100 # Initial noise threshold
# while True:
# try:
# async with websockets.connect(uri, ping_interval=20, ping_timeout=10) as websocket:
# print("Connected to WebSocket server.")
# while True:
# if ser.in_waiting > 0:
# arduino_data = ser.readline().decode('utf-8').strip()
# try:
# data = json.loads(arduino_data)
# ecg_value = data['ecg']
# timestamp = data['timestamp']
# # Validate and filter ECG data
# if isinstance(ecg_value, (int, float)) and ecg_value > 0:
# rolling_std = np.std(ecg_signal[-100:]) if len(ecg_signal) >= 100 else 0
# if ecg_value > noise_threshold + rolling_std:
# ecg_signal.append(ecg_value)
# # Limit buffer size
# if len(ecg_signal) > 1000:
# ecg_signal = ecg_signal[-1000:]
# if len(ecg_signal) > 100: # Ensure enough data for processing
# peaks = detect_r_peaks(np.array(ecg_signal))
# bpm = calculate_bpm(peaks)
# print(f"BPM: {bpm}")
# message = json.dumps({
# "ecg": ecg_signal[-50:], # Send last 50 data points
# "bpm": bpm,
# "timestamp": timestamp
# })
# await websocket.send(message)
# print(f"Sent: {message}")
# else:
# print("Filtered noise.")
# else:
# print("Invalid data received.")
# except json.JSONDecodeError:
# print("JSON Decode Error")
# await asyncio.sleep(0.1)
# except websockets.exceptions.ConnectionClosed as e:
# print(f"Connection closed: {e}. Reconnecting...")
# await asyncio.sleep(2)
# except asyncio.TimeoutError:
# print("Connection timed out. Reconnecting...")
# await asyncio.sleep(2)
# except Exception as e:
# print(f"Unexpected error: {e}. Retrying in 5 seconds...")
# await asyncio.sleep(5)
# # Graceful shutdown handler
# if __name__ == "__main__":
# try:
# asyncio.run(send_data_from_arduino())
# except KeyboardInterrupt:
# print("Shutting down gracefully...")
# import asyncio
# import websockets
# #import bitalino
# import json
# import random
# import time
# import numpy as np
# from biosppy.signals import ecg
# import datetime
# async def send_random_data():
# uri = "ws://localhost:8000/ws/3"
# while True:
# try:
# async with websockets.connect(uri, ping_interval=20, ping_timeout=10) as websocket:
# print("Connected to the WebSocket server.")
# while True:
# ecg_value = random.uniform(-1, 1)
# bpm = random.uniform(60, 100)
# timestamp = datetime.datetime.now().isoformat()
# message = json.dumps({
# "ecg": [ecg_value],
# "timestamp": timestamp,
# "bpm": bpm
# })
# await websocket.send(message)
# print(f"Sent: {message}")
# await asyncio.sleep(1.5)
# except websockets.exceptions.ConnectionClosed as e:
# print(f"Connection closed: {e}. Reconnecting...")
# await asyncio.sleep(2)
# except asyncio.TimeoutError:
# print("Connection timed out. Reconnecting...")
# await asyncio.sleep(2)
# except asyncio.CancelledError:
# print("Task was cancelled. Exiting...")
# break
# except Exception as e:
# print(f"Unexpected error: {e}. Retrying in 5 seconds...")
# await asyncio.sleep(5)
# if __name__ == "__main__":
# asyncio.run(send_random_data())