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
| import torch import torch.nn as nn import numpy as np from scipy import signal as sig
class Radar4DCabinPerception(nn.Module): """4D雷达座舱感知多任务模型""" def __init__(self, n_range=128, n_doppler=64, n_azimuth=32, n_elevation=16): super().__init__() self.spatial_encoder = nn.Sequential( nn.Conv3d(1, 32, 3, padding=1), nn.BatchNorm3d(32), nn.ReLU(), nn.MaxPool3d(2), nn.Conv3d(32, 64, 3, padding=1), nn.BatchNorm3d(64), nn.ReLU(), nn.MaxPool3d(2), nn.Conv3d(64, 128, 3, padding=1), nn.BatchNorm3d(128), nn.ReLU(), nn.AdaptiveAvgPool3d(1), ) self.velocity_encoder = nn.Sequential( nn.Conv2d(1, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1), ) self.fusion = nn.Sequential( nn.Linear(128 * 2, 256), nn.ReLU(), nn.Dropout(0.2), ) self.occupant_head = nn.Linear(256, 5) self.cpd_head = nn.Linear(256, 2) self.breathing_head = nn.Linear(256, 1) self.posture_head = nn.Linear(256, 5) self.gesture_head = nn.Linear(256, 8) def forward(self, spatial_data, velocity_data): """ Args: spatial_data: (B, 1, E, A, R) 空间RDA velocity_data: (B, 1, R, D) Range-Doppler Returns: dict of task outputs """ spatial_feat = self.spatial_encoder(spatial_data).flatten(1) velocity_feat = self.velocity_encoder(velocity_data).flatten(1) fused = self.fusion(torch.cat([spatial_feat, velocity_feat], dim=1)) return { 'occupant_count': self.occupant_head(fused), 'cpd_presence': self.cpd_head(fused), 'breathing_rate': self.breathing_head(fused), 'posture': self.posture_head(fused), 'gesture': self.gesture_head(fused), }
class VitalSignsExtractor: """4D雷达微多普勒生命体征提取""" def __init__(self, fs=20, range_bin=None): self.fs = fs self.range_bin = range_bin def extract_breathing(self, range_doppler_sequence): """ 从Range-Doppler序列提取呼吸 Args: range_doppler_sequence: (T, R, D) 时序R-D图 Returns: breathing_rate: 呼吸频率 (Hz) breathing_signal: 呼吸波形 """ if self.range_bin is None: energy = np.sum(np.abs(range_doppler_sequence)**2, axis=2) self.range_bin = np.argmax(np.sum(energy, axis=0)) target = range_doppler_sequence[:, self.range_bin, :] low_doppler = target[:, :8] phase = np.angle(np.sum(low_doppler, axis=1)) phase = np.unwrap(phase) b, a = sig.butter(4, [0.1, 0.5], btype='band', fs=self.fs) breathing_signal = sig.filtfilt(b, a, phase) freqs = np.fft.rfftfreq(len(breathing_signal), 1/self.fs) spectrum = np.abs(np.fft.rfft(breathing_signal)) breathing_rate = freqs[np.argmax(spectrum)] return breathing_rate, breathing_signal def extract_heartbeat(self, range_doppler_sequence): """提取心跳(0.8-2.0 Hz)""" if self.range_bin is None: energy = np.sum(np.abs(range_doppler_sequence)**2, axis=2) self.range_bin = np.argmax(np.sum(energy, axis=0)) target = range_doppler_sequence[:, self.range_bin, :] low_doppler = target[:, :8] phase = np.unwrap(np.angle(np.sum(low_doppler, axis=1))) b_breath, a_breath = sig.butter(4, [0.1, 0.5], btype='band', fs=self.fs) breathing = sig.filtfilt(b_breath, a_breath, phase) residual = phase - sig.filtfilt( sig.butter(4, [0.1, 0.5], btype='band', fs=self.fs)[0], sig.butter(4, [0.1, 0.5], btype='band', fs=self.fs)[1], phase ) b_heart, a_heart = sig.butter(4, [0.8, 2.0], btype='band', fs=self.fs) heart_signal = sig.filtfilt(b_heart, a_heart, residual) freqs = np.fft.rfftfreq(len(heart_signal), 1/self.fs) spectrum = np.abs(np.fft.rfft(heart_signal)) heart_rate = freqs[np.argmax(spectrum)] return heart_rate, heart_signal
if __name__ == "__main__": model = Radar4DCabinPerception() spatial = torch.randn(2, 1, 16, 32, 128) velocity = torch.randn(2, 1, 128, 64) outputs = model(spatial, velocity) print("=== 4D雷达座舱感知多任务 ===") for task, out in outputs.items(): print(f" {task}: {out.shape}") params = sum(p.numel() for p in model.parameters()) print(f"\n参数量: {params/1e6:.2f}M") print("\n=== 生命体征提取 ===") rd_seq = np.random.randn(200, 128, 64) + 1j * np.random.randn(200, 128, 64) t = np.arange(200) / 20 breathing_mod = np.sin(2 * np.pi * 0.25 * t) rd_seq[:, 50, 0] += breathing_mod extractor = VitalSignsExtractor(fs=20) br_rate, br_signal = extractor.extract_breathing(rd_seq) print(f" 呼吸频率: {br_rate:.2f} Hz ({br_rate*60:.0f} breaths/min)")
|