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
| import numpy as np from scipy.signal import butter, filtfilt, find_peaks
class VitalSignsExtractor: """ 生命体征提取器 从雷达相位信号中提取呼吸和心跳 """ def __init__(self, fs: float = 100): """ Args: fs: 采样率(Hz) """ self.fs = fs def extract_phase(self, s_if: np.ndarray) -> np.ndarray: """ 从中频信号提取相位 相位 = arctan(Im/Re) """ spectrum = np.fft.fft(s_if) max_idx = np.argmax(np.abs(spectrum[:len(spectrum)//2])) phase = np.angle(spectrum[max_idx]) return phase def bandpass_filter( self, signal: np.ndarray, lowcut: float, highcut: float, order: int = 4 ) -> np.ndarray: """ 带通滤波 Args: lowcut: 低频截止(Hz) highcut: 高频截止(Hz) """ nyq = self.fs / 2 low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') filtered = filtfilt(b, a, signal) return filtered def extract_respiration(self, phase_signal: np.ndarray) -> np.ndarray: """ 提取呼吸信号 频率范围: 0.1-0.7 Hz (6-42次/分) """ return self.bandpass_filter(phase_signal, 0.1, 0.7) def extract_heartbeat(self, phase_signal: np.ndarray) -> np.ndarray: """ 提取心跳信号 频率范围: 0.8-6 Hz (48-360次/分) """ return self.bandpass_filter(phase_signal, 0.8, 6.0) def calculate_rate(self, signal: np.ndarray) -> float: """ 计算频率(次/分钟) """ peaks, _ = find_peaks(signal, distance=self.fs*0.5) if len(peaks) < 2: return 0.0 avg_interval = np.mean(np.diff(peaks)) / self.fs rate = 60 / avg_interval return rate def process(self, phase_signal: np.ndarray) -> dict: """ 完整处理流程 Returns: { 'respiration_signal': 呼吸信号, 'heartbeat_signal': 心跳信号, 'respiration_rate': 呼吸率, 'heart_rate': 心率 } """ resp_sig = self.extract_respiration(phase_signal) heart_sig = self.extract_heartbeat(phase_signal) resp_rate = self.calculate_rate(resp_sig) heart_rate = self.calculate_rate(heart_sig) return { 'respiration_signal': resp_sig, 'heartbeat_signal': heart_sig, 'respiration_rate': resp_rate, 'heart_rate': heart_rate }
if __name__ == "__main__": np.random.seed(42) fs = 100 t = np.linspace(0, 60, 60*fs) phase = 2 * np.sin(2 * np.pi * 0.25 * t) + \ 0.5 * np.sin(2 * np.pi * 1.2 * t) + \ 0.1 * np.random.randn(len(t)) extractor = VitalSignsExtractor(fs=fs) result = extractor.process(phase) print(f"检测到呼吸率: {result['respiration_rate']:.1f} 次/分") print(f"检测到心率: {result['heart_rate']:.1f} 次/分")
|