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
| """ 毫米波雷达心率估计:呼吸谐波干扰消除 论文: MDPI Sensors 2026, 26(17), 5587
核心方法: 自适应谐波消除 + 带通滤波 + 心率变异性提取 """
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from scipy import signal as scipy_signal from typing import Tuple, Optional
class FMCWRadarPhaseDemodulator: """ FMCW雷达相位解调器 从雷达中频信号提取胸壁位移相位 原理: FMCW雷达发射线性调频信号 回波与发射信号混频得到中频(IF)信号 IF信号的相位包含目标距离信息 胸壁运动导致相位变化 → 呼吸+心跳 """ def __init__(self, sample_rate: float = 100.0, freq_slope: float = 60e12, center_freq: float = 60e9): self.sample_rate = sample_rate self.freq_slope = freq_slope self.center_freq = center_freq self.c = 3e8 def demodulate(self, if_signal: np.ndarray) -> np.ndarray: """ 相位解调 Args: if_signal: (N,) 中频信号 Returns: phase: (N,) 胸壁位移相位 """ N = len(if_signal) fft_result = np.fft.fft(if_signal) range_resolution = self.c / (2 * self.freq_slope * N / self.sample_rate) chest_bin = int(0.5 / range_resolution) frame_size = 256 hop_size = 128 phases = [] for i in range(0, N - frame_size, hop_size): frame = if_signal[i:i+frame_size] fft_frame = np.fft.fft(frame) phase = np.angle(fft_frame[chest_bin]) phases.append(phase) phases = np.unwrap(phases) wavelength = self.c / self.center_freq displacement = phases * wavelength / (4 * np.pi) return displacement
class RespiratoryHarmonicCanceller: """ 呼吸谐波消除器 论文核心方法: 自适应消除呼吸基频及其谐波 步骤: 1. 检测呼吸基频 2. 生成各次谐波 3. 自适应滤波消除 4. 提取残留心跳信号 """ def __init__(self, num_harmonics: int = 5, adaptive_filter_length: int = 32): self.num_harmonics = num_harmonics self.filter_length = adaptive_filter_length def detect_resp_freq(self, chest_signal: np.ndarray, sample_rate: float = 100.0) -> float: """ 检测呼吸基频 Args: chest_signal: 胸壁位移信号 sample_rate: 采样率 Returns: resp_freq: 呼吸频率 (Hz) """ b, a = scipy_signal.butter(4, [0.1, 0.5], btype='band', fs=sample_rate) resp_filtered = scipy_signal.filtfilt(b, a, chest_signal) freqs = np.fft.rfftfreq(len(resp_filtered), 1/sample_rate) spectrum = np.abs(np.fft.rfft(resp_filtered)) mask = (freqs >= 0.1) & (freqs <= 0.5) peak_idx = np.argmax(spectrum[mask]) resp_freq = freqs[mask][peak_idx] return resp_freq def cancel_harmonics(self, chest_signal: np.ndarray, resp_freq: float, sample_rate: float = 100.0) -> np.ndarray: """ 消除呼吸谐波 Args: chest_signal: 原始胸壁信号 resp_freq: 呼吸基频 sample_rate: 采样率 Returns: cardiac_signal: 消除呼吸后的心跳信号 """ N = len(chest_signal) t = np.arange(N) / sample_rate reference = np.zeros((N, self.num_harmonics * 2)) for h in range(1, self.num_harmonics + 1): reference[:, 2*(h-1)] = np.sin(2 * np.pi * h * resp_freq * t) reference[:, 2*(h-1)+1] = np.cos(2 * np.pi * h * resp_freq * t) weights = np.zeros(self.filter_length * self.num_harmonics * 2) cardiac_signal = chest_signal.copy() for n in range(self.filter_length, N): ref_window = reference[n-self.filter_length:n].flatten() filter_output = np.dot(weights, ref_window) error = chest_signal[n] - filter_output cardiac_signal[n] = error mu = 0.01 weights += mu * error * ref_window b, a = scipy_signal.butter(4, [0.8, 3.0], btype='band', fs=sample_rate) cardiac_filtered = scipy_signal.filtfilt(b, a, cardiac_signal) return cardiac_filtered
class HeartRateEstimator: """ 心率估计器 从消除谐波后的心跳信号估计心率 """ def __init__(self, window_sec: float = 10.0, sample_rate: float = 100.0): self.window_sec = window_sec self.sample_rate = sample_rate self.window_samples = int(window_sec * sample_rate) def estimate(self, cardiac_signal: np.ndarray) -> dict: """ 估计心率 Args: cardiac_signal: 心跳信号 Returns: result: { 'heart_rate': float (BPM), 'confidence': float, 'hrv': float (ms), # 心率变异性 } """ N = len(cardiac_signal) estimates = [] for i in range(0, N - self.window_samples, self.window_samples // 2): window = cardiac_signal[i:i+self.window_samples] freqs = np.fft.rfftfreq(len(window), 1/self.sample_rate) spectrum = np.abs(np.fft.rfft(window)) mask = (freqs >= 0.8) & (freqs <= 2.0) if np.any(mask): peak_idx = np.argmax(spectrum[mask]) hr_hz = freqs[mask][peak_idx] hr_bpm = hr_hz * 60 confidence = spectrum[mask][peak_idx] / (spectrum.sum() + 1e-8) estimates.append((hr_bpm, confidence)) if not estimates: return {'heart_rate': 0, 'confidence': 0, 'hrv': 0} hr_values = [e[0] for e in estimates] conf_values = [e[1] for e in estimates] avg_hr = np.median(hr_values) avg_conf = np.mean(conf_values) peaks, _ = scipy_signal.find_peaks( cardiac_signal, distance=int(0.5 * self.sample_rate), prominence=0.5 * np.std(cardiac_signal) ) if len(peaks) > 2: ibi = np.diff(peaks) / self.sample_rate * 1000 hrv = np.std(ibi) else: hrv = 0 return { 'heart_rate': avg_hr, 'confidence': avg_conf, 'hrv': hrv, }
if __name__ == "__main__": np.random.seed(42) sample_rate = 100.0 duration = 60.0 N = int(duration * sample_rate) t = np.arange(N) / sample_rate resp_freq = 0.25 resp_signal = 8.0 * np.sin(2 * np.pi * resp_freq * t) hr_true = 72 hr_freq = hr_true / 60 cardiac_signal = 1.5 * np.sin(2 * np.pi * hr_freq * t) for h in range(2, 6): resp_signal += (8.0 / h) * np.sin(2 * np.pi * h * resp_freq * t) chest_signal = resp_signal + cardiac_signal + np.random.normal(0, 0.3, N) print("=" * 60) print("毫米波雷达心率估计测试") print(f"真实呼吸频率: {resp_freq * 60:.1f} BPM") print(f"真实心率: {hr_true} BPM") canceller = RespiratoryHarmonicCanceller(num_harmonics=5) detected_resp = canceller.detect_resp_freq(chest_signal, sample_rate) print(f"\n检测到呼吸频率: {detected_resp * 60:.1f} BPM") cardiac_clean = canceller.cancel_harmonics( chest_signal, detected_resp, sample_rate ) estimator = HeartRateEstimator(window_sec=10.0, sample_rate=sample_rate) result = estimator.estimate(cardiac_clean) print(f"\n估计心率: {result['heart_rate']:.1f} BPM") print(f"置信度: {result['confidence']:.3f}") print(f"HRV: {result['hrv']:.1f} ms") print(f"误差: {abs(result['heart_rate'] - hr_true):.1f} BPM") print("\n频谱分析:") freqs = np.fft.rfftfreq(N, 1/sample_rate) orig_spectrum = np.abs(np.fft.rfft(chest_signal)) clean_spectrum = np.abs(np.fft.rfft(cardiac_clean)) for f, label in [(0.25, "呼吸基频"), (0.5, "呼吸2次"), (0.75, "呼吸3次"), (1.2, "心跳基频")]: idx = np.argmin(np.abs(freqs - f)) orig_mag = orig_spectrum[idx] clean_mag = clean_spectrum[idx] reduction = (1 - clean_mag / (orig_mag + 1e-8)) * 100 print(f" {label} ({f:.2f} Hz): {orig_mag:.1f} → " f"{clean_mag:.1f} (降低{reduction:.0f}%)") print("\n✅ 测试通过")
|