You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pandas as pd
import numpy as np
def generate_adsb_test_data(normal_count=120, attack_count=80):
"""
生成ADS-B测试数据
normal_count: 正常数据条数
attack_count: 攻击数据条数
"""
data = []
# ====================== 1. 生成正常飞行数据 ======================
# 模拟几架真实飞机的ICAO地址
normal_icaos = ["a1b2c3", "d4e5f6", "789abc", "def012"]
base_time = 1656292550 # 起始时间戳
for i in range(normal_count):
icao = np.random.choice(normal_icaos)
time = base_time + i * 2 # 每2秒一条数据
# 正常的经纬度:缓慢变化
lat = 40.0 + np.random.normal(0, 0.01) + (i * 0.001)
lon = -90.0 + np.random.normal(0, 0.01) + (i * 0.001)
# 正常的航向0-360度缓慢变化
heading = 180 + np.random.normal(0, 5)
# 正常的速度200-300节
velocity = 250 + np.random.normal(0, 10)
# 正常的高度10000米左右
baroaltitude = 10000 + np.random.normal(0, 50)
geoaltitude = baroaltitude + 100 + np.random.normal(0, 20)
label = 0 # 正常
data.append([icao, time, lat, lon, heading, velocity, baroaltitude, geoaltitude, label])
# ====================== 2. 生成攻击数据 ======================
attack_icaos = ["fake01", "ghost02", "attack03"]
for i in range(attack_count):
icao = np.random.choice(attack_icaos)
time = base_time + (normal_count + i) * 2
# 随机选择一种攻击类型
attack_type = np.random.choice(["speed", "altitude", "position", "ghost"])
if attack_type == "speed":
# 攻击1速度异常突然变得极快或极慢
lat = 40.0 + np.random.normal(0, 0.01)
lon = -90.0 + np.random.normal(0, 0.01)
heading = 180
velocity = np.random.choice([50, 500]) # 要么极慢,要么极快
baroaltitude = 10000
geoaltitude = 10100
elif attack_type == "altitude":
# 攻击2高度异常突然跳到0或极高
lat = 40.0 + np.random.normal(0, 0.01)
lon = -90.0 + np.random.normal(0, 0.01)
heading = 180
velocity = 250
baroaltitude = np.random.choice([0, 20000]) # 要么0要么2万米
geoaltitude = baroaltitude
elif attack_type == "position":
# 攻击3位置异常经纬度突然跳变
lat = np.random.uniform(-90, 90) # 随机全球位置
lon = np.random.uniform(-180, 180)
heading = 180
velocity = 250
baroaltitude = 10000
geoaltitude = 10100
else:
# 攻击4幽灵飞机所有参数都不合理
lat = 0.0
lon = 0.0
heading = 999 # 不合理的航向
velocity = 1000 # 超音速
baroaltitude = 50000 # 不可能的高度
geoaltitude = 50000
label = 1 # 攻击
data.append([icao, time, lat, lon, heading, velocity, baroaltitude, geoaltitude, label])
# ====================== 3. 打乱并保存 ======================
df = pd.DataFrame(data, columns=[
"icao24", "time", "lat", "lon", "heading",
"velocity", "baroaltitude", "geoaltitude", "label"
])
# 打乱顺序,让正常和攻击混在一起
df = df.sample(frac=1).reset_index(drop=True)
# 保存为CSV
output_file = "test_adsb_data.csv"
df.to_csv(output_file, index=False)
print(f"✅ 测试数据生成成功!")
print(f" 文件:{output_file}")
print(f" 总数据:{len(df)}")
print(f" 正常数据:{len(df[df['label']==0])}")
print(f" 攻击数据:{len(df[df['label']==1])}")
print(f"\n现在你可以上传 {output_file} 到你的系统测试了!")
if __name__ == "__main__":
generate_adsb_test_data(normal_count=120, attack_count=80)