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 import torch import torch.nn as nn from scipy import signal as sig
class RadarGestureRecognition: """mmWave 雷达手势识别系统""" def __init__(self, n_classes=8): self.n_classes = n_classes self.n_rx = 4 self.n_chirp = 64 self.n_adc = 256 self.frame_rate = 30 self.range_fft_size = 256 self.doppler_fft_size = 64 self.angle_fft_size = 64 self.model = RadarGestureNet(n_classes=n_classes) def process_raw_data(self, adc_data): """ 处理IWR1443原始ADC数据 Args: adc_data: (n_chirp, n_rx, n_adc) raw ADC Returns: range_doppler_map: (n_rx, range_bins, doppler_bins) range_angle_map: (doppler_bins, range_bins, angle_bins) """ range_fft = np.fft.fft(adc_data, n=self.range_fft_size, axis=-1) range_fft = np.abs(range_fft) doppler_fft = np.fft.fftshift( np.fft.fft(range_fft, n=self.doppler_fft_size, axis=0), axes=0 ) angle_fft = np.fft.fftshift( np.fft.fft(doppler_fft, n=self.angle_fft_size, axis=1), axes=1 ) range_doppler = np.log1p(np.abs(doppler_fft)) range_angle = np.log1p(np.abs(angle_fft)) return range_doppler, range_angle def extract_micro_doppler(self, range_doppler, n_frames=30): """ 提取micro-Doppler特征(时频图) 手势的微多普勒特征包含: - 手部运动速度 - 运动方向变化 - 运动持续时间 """ center_bin = range_doppler.shape[-1] // 2 max_velocity = np.max(np.abs(range_doppler[:, :, center_bin]), axis=0) f, t, sxx = sig.stft( max_velocity, fs=self.frame_rate, nperseg=8, noverlap=4 ) return sxx
class RadarGestureNet(nn.Module): """mmWave雷达手势分类网络""" def __init__(self, n_classes=8, input_channels=4): super().__init__() self.features = nn.Sequential( nn.Conv2d(input_channels, 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.MaxPool2d(2), nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d(1), ) self.temporal = nn.LSTM( input_size=256, hidden_size=128, num_layers=2, batch_first=True ) self.classifier = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Dropout(0.3), nn.Linear(64, n_classes) ) def forward(self, x): """ Args: x: (B, T, C, H, W) T帧的Range-Doppler图 Returns: logits: (B, n_classes) """ B, T, C, H, W = x.shape x = x.view(B * T, C, H, W) spatial = self.features(x) spatial = spatial.view(B, T, -1) temporal_out, _ = self.temporal(spatial) last = temporal_out[:, -1, :] return self.classifier(last)
CABIN_GESTURES = { 0: 'swipe_left', 1: 'swipe_right', 2: 'swipe_up', 3: 'swipe_down', 4: 'circle_cw', 5: 'circle_ccw', 6: 'push', 7: 'pull', }
if __name__ == "__main__": system = RadarGestureRecognition(n_classes=8) adc = np.random.randn(64, 4, 256) + 1j * np.random.randn(64, 4, 256) rd, ra = system.process_raw_data(adc) print(f"Range-Doppler: {rd.shape}") print(f"Range-Angle: {ra.shape}") B, T = 2, 10 input_data = torch.randn(B, T, 4, 64, 64) output = system.model(input_data) print(f"\n输入: {input_data.shape}") print(f"输出: {output.shape} (8类手势)") print(f"参数量: {sum(p.numel() for p in system.model.parameters())/1e6:.2f}M") print(f"\n座舱手势集:") for idx, name in CABIN_GESTURES.items(): print(f" {idx}: {name}")
|