Untitled
unknown
python
8 months ago
2.8 kB
13
Indexable
import os
import numpy as np
import pandas as pd
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tqdm import tqdm # For progress tracking
"""
Mr. Doo, assume this is the folder that contains only those 10 aircraft signals
data/
│── aircraft_1/
│ ├── signal_1.iq
│ ├── signal_2.iq
│── aircraft_2/
│ ├── signal_3.iq
│ ├── signal_4.iq
│── aircraft_3/
│ ├── signal_5.iq
| ...
|__ aircraft_10/
|── signal_x.iq
...
"""
def read_iq_file(filename):
""" Reads IQ binary file and normalizes I and Q values. """
with open(filename, "rb") as f:
data = np.frombuffer(f.read(), dtype=np.uint8)
I = data[0::2].astype(np.float32)
Q = data[1::2].astype(np.float32)
# Normalize values (-1 to 1)
I = (I - 128) / 128.0
Q = (Q - 128) / 128.0
return I, Q
def compute_featues(I, Q):
"""
Shared previously
"""
pass
# === Function to Process All IQ Files ===
def process_iq_folder(dataset_path):
""" Scans a folder structure <class_name>/file.iq and extracts features. """
data = []
labels = []
for class_name in tqdm(os.listdir(dataset_path), desc="Processing Classes"):
class_path = os.path.join(dataset_path, class_name)
if not os.path.isdir(class_path):
continue # Skip non-directory files
for file_name in os.listdir(class_path):
if file_name.endswith(".iq"):
file_path = os.path.join(class_path, file_name)
try:
I, Q = read_iq_file(file_path)
features = compute_features(I, Q)
data.append(features)
labels.append(class_name)
except Exception as e:
print(f"Error processing {file_path}: {e}")
return pd.DataFrame(data), labels
# === Run Feature Extraction ===
dataset_path = "data" # Path to the dataset folder
X, y = process_iq_folder(dataset_path)
# === Encode Labels ===
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y) # Converts class names into numbers
# === Train-Test Split ===
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)
# === Train XGBoost Classifier ===
xgb_classifier = xgb.XGBClassifier(
objective="multi:softmax",
num_class=len(np.unique(y_encoded)),
eval_metric="mlogloss",
max_depth=6,
learning_rate=0.1,
n_estimators=100,
use_label_encoder=False
)
xgb_classifier.fit(X_train, y_train)
# === Model Evaluation ===
y_pred = xgb_classifier.predict(X_test)
accuracy = np.mean(y_pred == y_test)
print(f"Model Accuracy: {accuracy:.4f}")
Editor is loading...
Leave a Comment