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
| import numpy as np from typing import Tuple, List from dataclasses import dataclass
@dataclass class FMCWRadarConfig: """FMCW雷达配置""" start_freq: float = 60e9 bandwidth: float = 4e9 chirp_duration: float = 100e-6 sample_rate: float = 10e6 num_chirps: int = 128 num_rx: int = 4 num_tx: int = 3
class FMCWRadarProcessor: """FMCW雷达信号处理器""" def __init__(self, config: FMCWRadarConfig): self.config = config self.range_resolution = 3e8 / (2 * config.bandwidth) self.velocity_resolution = 3e8 / (2 * config.start_freq * config.num_chirps * config.chirp_duration) def generate_chirp_signal(self) -> np.ndarray: """生成线性调频信号""" t = np.linspace(0, self.config.chirp_duration, int(self.config.sample_rate * self.config.chirp_duration)) chirp_rate = self.config.bandwidth / self.config.chirp_duration phase = 2 * np.pi * (self.config.start_freq * t + 0.5 * chirp_rate * t**2) tx_signal = np.exp(1j * phase) return tx_signal def range_fft(self, adc_data: np.ndarray) -> np.ndarray: """ 距离FFT Args: adc_data: (num_chirps, num_samples, num_rx) ADC数据 Returns: range_profile: (num_chirps, num_range_bins, num_rx) 距离剖面 """ range_fft = np.fft.fft(adc_data, axis=1) range_profile = range_fft[:, :adc_data.shape[1]//2, :] return range_profile def doppler_fft(self, range_profile: np.ndarray) -> np.ndarray: """ 多普勒FFT Args: range_profile: (num_chirps, num_range_bins, num_rx) Returns: range_doppler_map: (num_doppler_bins, num_range_bins, num_rx) """ doppler_fft = np.fft.fftshift(np.fft.fft(range_profile, axis=0), axes=0) return np.abs(doppler_fft) def detect_targets(self, range_doppler_map: np.ndarray, threshold: float = 0.1) -> List[dict]: """ 检测目标 Args: range_doppler_map: 距离-多普勒图 threshold: 检测阈值 Returns: targets: 目标列表 """ cfar_mask = self.cfar_detection(range_doppler_map, threshold) targets = [] doppler_bins, range_bins, _ = np.where(cfar_mask) for d, r, rx in zip(doppler_bins, range_bins, [0] * len(range_bins)): distance = r * self.range_resolution velocity = (d - range_doppler_map.shape[0]//2) * self.velocity_resolution targets.append({ 'distance': distance, 'velocity': velocity, 'range_bin': r, 'doppler_bin': d }) return targets def cfar_detection(self, data: np.ndarray, threshold: float) -> np.ndarray: """CFAR恒虚警检测""" from scipy.ndimage import uniform_filter local_mean = uniform_filter(data, size=(16, 16), mode='constant') return data > (local_mean * (1 + threshold))
class VitalSignsExtractor: """生命体征提取器""" def __init__(self): self.hr_freq_range = (0.8, 2.0) self.rr_freq_range = (0.15, 0.5) def extract_chest_displacement(self, range_profile: np.ndarray, target_bin: int) -> np.ndarray: """ 提取胸腔位移 Args: range_profile: (num_chirps, num_range_bins, num_rx) target_bin: 目标距离bin Returns: displacement: (num_frames,) 胸腔位移序列 """ phase = np.angle(range_profile[:, target_bin, 0]) unwrapped_phase = np.unwrap(phase) wavelength = 3e8 / 60e9 displacement = unwrapped_phase * wavelength / (4 * np.pi) from scipy.signal import butter, filtfilt nyq = 0.5 * 10 low = 0.1 / nyq b, a = butter(4, low, btype='high') displacement = filtfilt(b, a, displacement) return displacement def extract_heart_rate(self, displacement: np.ndarray, sample_rate: float = 10) -> Tuple[float, np.ndarray]: """ 提取心率 Args: displacement: 胸腔位移序列 sample_rate: 采样率 Returns: hr: 心率 (bpm) spectrum: 频谱 """ n = len(displacement) freq = np.fft.rfftfreq(n, 1/sample_rate) spectrum = np.abs(np.fft.rfft(displacement)) hr_mask = (freq >= self.hr_freq_range[0]) & (freq <= self.hr_freq_range[1]) hr_spectrum = spectrum[hr_mask] hr_freq = freq[hr_mask] peak_idx = np.argmax(hr_spectrum) hr_freq_peak = hr_freq[peak_idx] hr = hr_freq_peak * 60 return hr, spectrum def extract_respiration_rate(self, displacement: np.ndarray, sample_rate: float = 10) -> Tuple[float, np.ndarray]: """ 提取呼吸率 Args: displacement: 胸腔位移序列 sample_rate: 采样率 Returns: rr: 呼吸率 (bpm) spectrum: 频谱 """ n = len(displacement) freq = np.fft.rfftfreq(n, 1/sample_rate) spectrum = np.abs(np.fft.rfft(displacement)) rr_mask = (freq >= self.rr_freq_range[0]) & (freq <= self.rr_freq_range[1]) rr_spectrum = spectrum[rr_mask] rr_freq = freq[rr_mask] peak_idx = np.argmax(rr_spectrum) rr_freq_peak = rr_freq[peak_idx] rr = rr_freq_peak * 60 return rr, spectrum
if __name__ == "__main__": config = FMCWRadarConfig() processor = FMCWRadarProcessor(config) vital_extractor = VitalSignsExtractor() print(f"距离分辨率: {processor.range_resolution*100:.2f} cm") print(f"速度分辨率: {processor.velocity_resolution:.2f} m/s") num_chirps = 128 num_samples = 256 num_rx = 4 adc_data = np.random.randn(num_chirps, num_samples, num_rx) * 0.01 target_bin = int(1.5 / processor.range_resolution) for i in range(num_chirps): phase_shift = 2 * np.pi * 0.3 * i / num_chirps * 10 adc_data[i, target_bin, :] += np.exp(1j * phase_shift) range_profile = processor.range_fft(adc_data) range_doppler = processor.doppler_fft(range_profile) targets = processor.detect_targets(range_doppler[:, :, 0]) print(f"\n检测到目标数: {len(targets)}") for t in targets: print(f" 距离: {t['distance']:.2f} m, 速度: {t['velocity']:.2f} m/s") displacement = vital_extractor.extract_chest_displacement(range_profile, target_bin) hr, _ = vital_extractor.extract_heart_rate(displacement) rr, _ = vital_extractor.extract_respiration_rate(displacement) print(f"\n心率: {hr:.1f} bpm") print(f"呼吸率: {rr:.1f} bpm")
|