-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVD_Denosing.py
330 lines (269 loc) · 11.5 KB
/
SVD_Denosing.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
from typing import List
import numpy as np
from scipy import signal
import os
import pandas as pd
from config import RESULTS_DIR, WINDOW_DIR
class SVDDenoising:
def __init__(self):
"""
Initialize SVD denoising algorithm with default parameters.
Attributes
----------
fs_galaxy : int
Galaxy Watch sampling frequency (25 Hz)
target_length_galaxy : int
Target length for Galaxy Watch signal processing (200 samples)
fs_e4 : int
E4 device sampling frequency (64 Hz)
target_length_e4 : int
Target length for E4 signal processing (512 samples)
min_singular_values : int
Minimum number of singular values to retain
energy_threshold : float
Threshold for singular value energy retention (0.0 to 1.0)
motion_threshold : float
Threshold for motion artifact detection (0.0 to 1.0)
lowcut : float
Lower cutoff frequency for bandpass filter (Hz)
highcut : float
Higher cutoff frequency for bandpass filter (Hz)
nfft : int
Number of points for FFT computation
"""
self.fs_galaxy = 25 # Hz
self.target_length_galaxy = 200
self.fs_e4 = 64 # Hz
self.target_length_e4 = 512
self.min_singular_values = 5
self.energy_threshold = 0.7
self.motion_threshold = 0.5
self.lowcut = 0.5 # Hz
self.highcut = 4 # Hz
self.nfft = 1024
def process_dataframe(self, df: pd.DataFrame,
ppg_col: str = 'ppg',
acc_cols: List[str] = ['acc_x', 'acc_y', 'acc_z'],
device_type: str = 'galaxy') -> pd.DataFrame:
"""
Process PPG and accelerometer data from a DataFrame.
Parameters
----------
df : pandas.DataFrame
Input DataFrame containing PPG and accelerometer data
ppg_col : str, default='ppg'
Name of the column containing PPG data
acc_cols : list of str, default=['acc_x', 'acc_y', 'acc_z']
Names of columns containing accelerometer data
device_type : str, default='galaxy'
Type of device ('galaxy' or 'e4')
Returns
-------
pandas.DataFrame
DataFrame with added column for denoised signal
"""
# Validate input columns
if ppg_col not in df.columns:
raise ValueError(f"PPG column '{ppg_col}' not found in DataFrame")
for col in acc_cols:
if col not in df.columns:
raise ValueError(f"Accelerometer column '{col}' not found in DataFrame")
results = []
for _, row in df.iterrows():
try:
ppg = np.array(row[ppg_col])
acc_x = np.array(row[acc_cols[0]])
acc_y = np.array(row[acc_cols[1]])
acc_z = np.array(row[acc_cols[2]])
fs = self.fs_galaxy if device_type.lower() == 'galaxy' else self.fs_e4
denoised = self.denoise(ppg, acc_x, acc_y, acc_z, fs)
results.append({
'denoised_signal': ';'.join(map(str, denoised))
})
except Exception as e:
print(f"Error processing row: {str(e)}")
results.append({
'denoised_signal': None
})
result_df = pd.concat([df, pd.DataFrame(results)], axis=1)
return result_df
def denoise(self, ppg, acc_x, acc_y, acc_z, fs):
"""
Denoise PPG signal using SVD-based method.
Parameters
----------
ppg : numpy.ndarray
Raw PPG signal
acc_x : numpy.ndarray
X-axis acceleration data
acc_y : numpy.ndarray
Y-axis acceleration data
acc_z : numpy.ndarray
Z-axis acceleration data
fs : float
Sampling frequency in Hz
Returns
-------
numpy.ndarray
Denoised PPG signal
"""
try:
ppg_processed, acc_x_processed, acc_y_processed, acc_z_processed = \
self.preprocess_signals(ppg, acc_x, acc_y, acc_z, fs)
if ppg_processed is None:
return self.mean_normalize(ppg)
H_ppg = self.build_hankel_matrix(ppg_processed)
if H_ppg is None:
return self.mean_normalize(ppg)
U, S, Vt = np.linalg.svd(H_ppg, full_matrices=False)
ppg_fft = np.fft.rfft(ppg_processed, n=self.nfft)
acc_ffts = [
np.fft.rfft(acc, n=self.nfft) for acc in
[acc_x_processed, acc_y_processed, acc_z_processed]
]
motion_weight = self.compute_motion_weights(ppg_fft, acc_ffts)
H_reconstructed = self.select_singular_values(S, U, Vt, motion_weight)
L, K = H_reconstructed.shape
N = L + K - 1
reconstructed = np.zeros(N)
count = np.zeros(N)
for i in range(L):
for j in range(K):
n = i + j
reconstructed[n] += H_reconstructed[i, j]
count[n] += 1
reconstructed /= count
denoised = self.mean_normalize(reconstructed)
return self.mean_normalize(denoised)
except Exception as e:
print({str(e)})
return self.mean_normalize(ppg)
def mean_normalize(self, x):
if len(x) == 0:
return x
mean = np.mean(x)
std_dev = np.std(x) + 1e-10
return (x - mean) / std_dev
def preprocess_signals(self, ppg, acc_x, acc_y, acc_z, fs):
try:
target_length = self.target_length_galaxy if fs == self.fs_galaxy else self.target_length_e4
if len(ppg) != target_length:
ppg = signal.resample(ppg, target_length)
if len(acc_x) != target_length:
acc_x = signal.resample(acc_x, target_length)
if len(acc_y) != target_length:
acc_y = signal.resample(acc_y, target_length)
if len(acc_z) != target_length:
acc_z = signal.resample(acc_z, target_length)
nyq = 0.5 * fs
low = self.lowcut / nyq
high = self.highcut / nyq
b, a = signal.butter(4, [low, high], btype='band')
ppg_filtered = signal.filtfilt(b, a, ppg)
acc_x_filtered = signal.filtfilt(b, a, acc_x)
acc_y_filtered = signal.filtfilt(b, a, acc_y)
acc_z_filtered = signal.filtfilt(b, a, acc_z)
ppg_processed = self.mean_normalize(ppg_filtered)
acc_x_processed = self.mean_normalize(acc_x_filtered)
acc_y_processed = self.mean_normalize(acc_y_filtered)
acc_z_processed = self.mean_normalize(acc_z_filtered)
return ppg_processed, acc_x_processed, acc_y_processed, acc_z_processed
except Exception as e:
print(f" {str(e)}")
return None, None, None, None
def compute_motion_weights(self, ppg_fft, acc_ffts):
try:
ppg_magnitude = np.abs(ppg_fft)
weights = []
for acc_fft in acc_ffts:
acc_magnitude = np.abs(acc_fft)
min_len = min(len(ppg_magnitude), len(acc_magnitude))
corr = np.corrcoef(ppg_magnitude[:min_len], acc_magnitude[:min_len])[0, 1]
if np.isnan(corr):
corr = 0
weight = 1 - min(abs(corr), self.motion_threshold)
weights.append(weight)
return np.mean(weights)
except Exception as e:
print(f"{str(e)}")
return 0.5
def select_singular_values(self, S, U, Vt, motion_weight):
try:
energy_ratio = np.cumsum(S ** 2) / np.sum(S ** 2)
adjusted_threshold = self.energy_threshold * (1 + 0.1 * motion_weight)
k = max(self.min_singular_values,
np.searchsorted(energy_ratio, adjusted_threshold) + 1)
k = min(k, len(S))
reconstructed = np.zeros_like(U @ np.diag(S) @ Vt)
for i in range(k):
adjusted_s = S[i] * (1 + 0.2 * motion_weight)
reconstructed += adjusted_s * np.outer(U[:, i], Vt[i, :])
return reconstructed
except Exception as e:
print(f"{str(e)}")
return U @ np.diag(S) @ Vt
def build_hankel_matrix(self, signal):
try:
N = len(signal)
L = N // 2
K = N - L + 1
H = np.zeros((L, K))
for i in range(L):
H[i, :] = signal[i:i + K]
return H
except Exception as e:
print( {str(e)})
return None
def process_dataset():
denoiser = SVDDenoising()
files = [f for f in os.listdir(WINDOW_DIR) if f.endswith('GD.csv')]
for filename in files:
print( {filename})
try:
df = pd.read_csv(os.path.join(WINDOW_DIR, filename))
results = {
'denoisedGalaxy': [],
'denoisedE4': []
}
for i, row in df.iterrows():
try:
if pd.notna(row['galaxyPPG']) and pd.notna(row['galaxyACC']):
ppg = np.array([float(x) for x in row['galaxyPPG'].split(';') if x.strip()])
acc = np.array([float(x) for x in row['galaxyACC'].split(';') if x.strip()]).reshape(-1, 3)
denoised = denoiser.denoise(
ppg, acc[:, 0], acc[:, 1], acc[:, 2], denoiser.fs_galaxy)
results['denoisedGalaxy'].append(';'.join(map(str, denoised)))
else:
results['denoisedGalaxy'].append(None)
if pd.notna(row['e4BVP']) and pd.notna(row['e4ACC']):
bvp = np.array([float(x) for x in row['e4BVP'].split(';') if x.strip()])
acc = np.array([float(x) for x in row['e4ACC'].split(';') if x.strip()]).reshape(-1, 3)
acc_resampled = np.array([
signal.resample(acc[:, i], len(bvp))
for i in range(3)
]).T
denoised = denoiser.denoise(
bvp,
acc_resampled[:, 0],
acc_resampled[:, 1],
acc_resampled[:, 2],
denoiser.fs_e4
)
results['denoisedE4'].append(';'.join(map(str, denoised)))
else:
results['denoisedE4'].append(None)
except Exception as e:
print( {str(e)})
results['denoisedGalaxy'].append(None)
results['denoisedE4'].append(None)
for col, values in results.items():
df[col] = values
output_dir = os.path.join(RESULTS_DIR, 'SVD')
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}_denoised.csv")
df.to_csv(output_file, index=False)
except Exception as e:
print( {str(e)})
continue
if __name__ == "__main__":
process_dataset()