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
| import numpy as np from scipy import signal
class FMCWRadarVitalSigns: """ FMCW雷达生命体征检测 原理: 1. 发射线性调频信号(Chirp) 2. 接收目标反射信号 3. 混频得到中频(IF)信号 4. 分析相位变化提取生命体征 """ def __init__(self, config: dict): self.fc = 60e9 self.bw = 4e9 self.chirp_duration = 100e-6 self.num_chirps = 128 self.num_samples = 256 self.fft_size = 512 def process_if_signal(self, if_data: np.ndarray) -> dict: """ 处理中频信号 Args: if_data: (num_chirps, num_samples) ADC数据 Returns: vital_signs: { 'respiration_rate': 呼吸率(次/分钟), 'heart_rate': 心率(次/分钟), 'signal_quality': 信号质量评分 } """ range_fft = self._range_fft(if_data) target_range = self._detect_target(range_fft) phase_signal = self._extract_phase(if_data, target_range) phase_unwrapped = np.unwrap(phase_signal) respiration, heart_rate = self._extract_vital_signs(phase_unwrapped) return { 'respiration_rate': respiration, 'heart_rate': heart_rate, 'signal_quality': self._estimate_signal_quality(phase_unwrapped) } def _range_fft(self, if_data: np.ndarray) -> np.ndarray: """距离FFT""" return np.fft.fft(if_data, n=self.fft_size, axis=1) def _detect_target(self, range_fft: np.ndarray) -> int: """检测目标距离bin""" magnitude = np.abs(range_fft).mean(axis=0) target_bin = np.argmax(magnitude[1:50]) + 1 return target_bin def _extract_phase( self, if_data: np.ndarray, target_bin: int ) -> np.ndarray: """ 提取相位时间序列 生命体征导致目标微动,反映为相位变化 """ range_fft = np.fft.fft(if_data, n=self.fft_size, axis=1) phase = np.angle(range_fft[:, target_bin]) return phase def _extract_vital_signs( self, phase_signal: np.ndarray ) -> Tuple[float, float]: """ 提取呼吸和心率 Args: phase_signal: 相位时间序列 Returns: respiration_rate: 呼吸率(次/分钟) heart_rate: 心率(次/分钟) """ t = np.arange(len(phase_signal)) * self.chirp_duration phase_detrend = signal.detrend(phase_signal) respiration_band = [0.1, 0.5] heart_band = [0.8, 2.0] fs = 1.0 / self.chirp_duration b_resp, a_resp = signal.butter( 4, [f/min(fs/2, 1) for f in respiration_band], btype='band' ) respiration_signal = signal.filtfilt(b_resp, a_resp, phase_detrend) b_heart, a_heart = signal.butter( 4, [f/min(fs/2, 1) for f in heart_band], btype='band' ) heart_signal = signal.filtfilt(b_heart, a_heart, phase_detrend) resp_freq, resp_psd = signal.welch( respiration_signal, fs=fs, nperseg=len(respiration_signal) ) heart_freq, heart_psd = signal.welch( heart_signal, fs=fs, nperseg=len(heart_signal) ) resp_peak_idx = np.argmax(resp_psd) heart_peak_idx = np.argmax(heart_psd) respiration_rate = resp_freq[resp_peak_idx] * 60 heart_rate = heart_freq[heart_peak_idx] * 60 return respiration_rate, heart_rate def _estimate_signal_quality(self, phase_signal: np.ndarray) -> float: """估计信号质量""" signal_power = np.var(phase_signal) noise_power = np.var(np.diff(phase_signal, n=2)) snr = signal_power / (noise_power + 1e-10) return min(1.0, snr / 10)
if __name__ == "__main__": config = {} radar = FMCWRadarVitalSigns(config) np.random.seed(42) if_data = np.random.randn(128, 256) * 0.1 t = np.arange(128) * 100e-6 respiration = 0.5 * np.sin(2 * np.pi * 0.25 * t) heartbeat = 0.05 * np.sin(2 * np.pi * 1.2 * t) phase_modulation = respiration + heartbeat if_data[:, 50] += 10 * np.exp(1j * phase_modulation * 10) result = radar.process_if_signal(if_data) print(f"呼吸率: {result['respiration_rate']:.1f} 次/分钟") print(f"心率: {result['heart_rate']:.1f} 次/分钟") print(f"信号质量: {result['signal_quality']:.2f}")
|