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
| class VitalSignEstimator: """ 从放大后的雷达信号提取28个特征,用ML估计心率/呼吸率 特征类别: 1. FFT频域特征(峰值频率、功率比、谱质心等) 2. 时域特征(过零率、方差、包络等) 3. 时频联合特征(STFT峰值轨迹等) """ def __init__(self, fs: float = 20.0): self.fs = fs def extract_features(self, signal: np.ndarray) -> np.ndarray: """ 提取28个特征 Args: signal: (R, T) 放大后的信号 Returns: features: (28,) 特征向量 """ features = [] best_bin = np.argmax(np.var(signal, axis=1)) sig = signal[best_bin] from scipy.fft import fft spectrum = np.abs(fft(sig)) freqs = np.fft.fftfreq(len(sig), 1/self.fs) pos_mask = freqs > 0 spectrum_pos = spectrum[pos_mask] freqs_pos = freqs[pos_mask] hr_mask = (freqs_pos >= 0.8) & (freqs_pos <= 4.0) hr_power = np.sum(spectrum_pos[hr_mask]) rr_mask = (freqs_pos >= 0.1) & (freqs_pos <= 0.5) rr_power = np.sum(spectrum_pos[rr_mask]) hr_peak_freq = freqs_pos[hr_mask][np.argmax(spectrum_pos[hr_mask])] if np.any(hr_mask) else 0 rr_peak_freq = freqs_pos[rr_mask][np.argmax(spectrum_pos[rr_mask])] if np.any(rr_mask) else 0 total_power = np.sum(spectrum_pos) + 1e-8 hr_ratio = hr_power / total_power rr_ratio = rr_power / total_power centroid = np.sum(freqs_pos * spectrum_pos) / total_power bandwidth = np.sqrt(np.sum(((freqs_pos - centroid)**2) * spectrum_pos) / total_power) gm = np.exp(np.mean(np.log(spectrum_pos + 1e-10))) am = np.mean(spectrum_pos) flatness = gm / (am + 1e-10) cumsum = np.cumsum(spectrum_pos) roll85 = freqs_pos[np.searchsorted(cumsum, 0.85 * cumsum[-1])] features.extend([hr_power, rr_power, hr_peak_freq, rr_peak_freq, hr_ratio, rr_ratio, centroid, bandwidth, flatness, roll85, np.max(spectrum_pos), np.mean(spectrum_pos)]) from scipy.signal import find_peaks, hilbert zcr = np.mean(np.diff(np.sign(sig)) != 0) variance = np.var(sig) from scipy.stats import skew, kurtosis sk = skew(sig) kt = kurtosis(sig) analytic = hilbert(sig) envelope = np.abs(analytic) env_mean = np.mean(envelope) env_std = np.std(envelope) env_max = np.max(envelope) peaks, _ = find_peaks(sig, distance=int(self.fs * 0.3)) peak_count = len(peaks) if peak_count > 1: mean_interval = np.mean(np.diff(peaks)) / self.fs hr_from_peaks = 60 / mean_interval else: hr_from_peaks = 0 features.extend([zcr, variance, sk, kt, env_mean, env_std, env_max, peak_count, hr_from_peaks, np.mean(np.diff(peaks)) / self.fs if peak_count > 1 else 0]) from scipy.signal import stft f, t, Sxx = stft(sig, fs=self.fs, nperseg=64) Sxx_norm = Sxx / (np.sum(Sxx) + 1e-10) tf_entropy = -np.sum(Sxx_norm * np.log2(Sxx_norm + 1e-10)) peak_freqs = f[np.argmax(Sxx, axis=0)] freq_stability = 1 / (np.std(peak_freqs) + 1e-8) freq_mean = np.mean(peak_freqs) sparsity = np.sum(np.abs(Sxx))**2 / (np.sum(Sxx**2) + 1e-10) max_energy_ratio = np.max(Sxx) / (np.sum(Sxx) + 1e-10) freq_var_rate = np.std(np.diff(peak_freqs)) if len(peak_freqs) > 2 else 0 features.extend([tf_entropy, freq_stability, freq_mean, sparsity, max_energy_ratio, freq_var_rate]) return np.array(features) def estimate(self, signal: np.ndarray, model_hr=None, model_rr=None) -> dict: """ 估计心率/呼吸率 Args: signal: (R, T) 放大后的信号 model_hr: 预训练心率模型 (RandomForest) model_rr: 预训练呼吸率模型 Returns: {'heart_rate': bpm, 'respiration_rate': brpm} """ features = self.extract_features(signal).reshape(1, -1) if model_hr is not None: hr = model_hr.predict(features)[0] else: best_bin = np.argmax(np.var(signal, axis=1)) from scipy.fft import fft spec = np.abs(fft(signal[best_bin])) freqs = np.fft.fftfreq(len(signal[best_bin]), 1/self.fs) hr_mask = (freqs >= 0.8) & (freqs <= 4.0) hr = freqs[hr_mask][np.argmax(spec[hr_mask])] * 60 if np.any(hr_mask) else 0 if model_rr is not None: rr = model_rr.predict(features)[0] else: best_bin = np.argmax(np.var(signal, axis=1)) spec = np.abs(fft(signal[best_bin])) freqs = np.fft.fftfreq(len(signal[best_bin]), 1/self.fs) rr_mask = (freqs >= 0.1) & (freqs <= 0.5) rr = freqs[rr_mask][np.argmax(spec[rr_mask])] * 60 if np.any(rr_mask) else 0 return {'heart_rate': hr, 'respiration_rate': rr}
|