code / processs.m
Bannh's picture
Upload 5 files
d55b504 verified
clc; clear; close all;
function iq_new = iq_augment(iq_raw, type, param)
% iq_raw: Tín hiệu gốc 1xN
% type: 'awgn', 'phase', 'fading', 'burst'
% param: Tham số điều chỉnh độ mạnh
iq_new = iq_raw;
% Lấy biên độ đỉnh của tín hiệu (làm mốc tham chiếu)
% Dùng quantile 99.9% để tránh nhiễu đột biến (spike) làm sai lệch
peak_amp = quantile(abs(iq_raw), 0.999);
avg_amp = mean(abs(iq_raw));
switch type
case 'awgn'
%% 1. NHIỄU TRẮNG (Additive Noise) - Fix lại logic
% param: Tỷ lệ nhiễu so với đỉnh tín hiệu (0.0 -> 1.0)
% Ví dụ: 0.05 (nhẹ), 0.2 (mạnh)
% Tạo nhiễu phức có phân bố chuẩn
noise = (randn(size(iq_raw)) + 1j*randn(size(iq_raw)));
% Chuẩn hóa nhiễu về biên độ 1
noise = noise / std(noise);
% Cộng vào: Noise level tính theo Peak của tín hiệu
% Nếu param = 0.1, nghĩa là nhiễu cao bằng 10% tín hiệu mạnh nhất
iq_new = iq_raw + (peak_amp * param) * noise;
case 'phase'
%% 2. NHIỄU PHA (Phase Noise/Jitter)
% param: Độ lệch pha tối đa (Radian). Ví dụ: 0.5 (nhòe nhẹ), 1.5 (nhòe mạnh)
% Tạo nhiễu pha ngẫu nhiên
theta_noise = param * randn(size(iq_raw));
% Nhân xoay pha
iq_new = iq_raw .* exp(1j * theta_noise);
case 'fading'
%% 3. FADING (Multipath) - Làm tín hiệu đứt đoạn
% param: Tốc độ biến thiên (Hz). Ví dụ: 50
len = length(iq_raw);
% Tạo bộ lọc thông thấp để làm mượt envelope fading
% (Mô phỏng sự thay đổi chậm của kênh truyền)
filter_len = round(len / param);
if filter_len < 3, filter_len = 3; end
% Tạo nhiễu Gaussian ngẫu nhiên
fading_process = randn(1, len) + 1j*randn(1, len);
% Lọc làm trơn (Moving Average)
env = movmean(abs(fading_process), filter_len);
% Chuẩn hóa envelope về [0, 1] để nó cắt tín hiệu
env = (env - min(env)) / (max(env) - min(env));
% Nhân chồng lên tín hiệu
iq_new = iq_raw .* env;
case 'burst'
%% 4. NHIỄU XUNG (Interference) - Vạch sọc dọc
% param: Xác suất xuất hiện (0.01 -> 0.05)
% Tạo mặt nạ ngẫu nhiên
mask = rand(size(iq_raw)) < param;
% Tạo nhiễu xung cường độ CỰC MẠNH (bằng 80% đỉnh tín hiệu)
burst_val = (randn(size(iq_raw)) + 1j*randn(size(iq_raw))) * (peak_amp * 0.8);
iq_new = iq_raw + mask .* burst_val;
end
end
%% 1. CẤU HÌNH ĐƯỜNG DẪN & THAM SỐ
input_folder = "D:\uav_detect\drone-rf\DAUTELEVONANO"; % Folder chứa file .iq
output_root = "D:\uav_detect\drone-rf\DAUTELEVONANO\test"; % Folder tổng chứa kết quả
% --- Global Scale (CỐ ĐỊNH CHO CẢ TẬP) ---
GLOBAL_MIN_DB = -120; % Mức sàn nhiễu (Nền Xanh)
GLOBAL_MAX_DB = 50; % Mức tín hiệu (Đỏ)
% --- Tham số STFT ---
fs = 100e6;
duration_ms = 30;
overlap_time = 0.5;
nfft = 1024;
window = hamming(3072);
spec_overlap = nfft/2;
cmap = colormap(jet(256));
aug_types = {'original', 'awgn_light', 'awgn_heavy', 'phase_noise', 'fading'};
% Tính toán kích thước cắt
samples_per_image = round(fs * (duration_ms / 1000));
step_size = round(samples_per_image * (1 - overlap_time));
bytes_back = (samples_per_image - step_size) * 8;
%% 2. QUÉT FILE TRONG FOLDER
% Tìm tất cả file .iq trong folder drone-rf
file_pattern = fullfile(input_folder, 'pack1_*.iq');
file_list = dir(file_pattern);
if isempty(file_list)
error('Không tìm thấy file .iq nào trong folder "%s"', input_folder);
end
% Tạo folder output gốc nếu chưa có
if ~exist(output_root, 'dir')
mkdir(output_root);
end
fprintf('Tìm thấy %d file trong "%s".\n', length(file_list), input_folder);
fprintf('Scale áp dụng: [%d dB, %d dB]\n', GLOBAL_MIN_DB, GLOBAL_MAX_DB);
%% 3. BẮT ĐẦU XỬ LÝ HÀNG LOẠT
% Mẹo: Nếu máy mạnh, đổi "for" thành "parfor" để chạy song song (nhanh gấp 4-8 lần)
for k = 1:length(file_list)
% Lấy tên file và đường dẫn đầy đủ
current_filename = file_list(k).name;
full_path = fullfile(file_list(k).folder, current_filename);
% Tạo folder con: dataset_spectrograms/spectrogram01, 02...
sub_folder_name = sprintf('spectrogram%02d', k);
output_dir = fullfile(output_root, sub_folder_name);
if ~exist(output_dir, 'dir')
mkdir(output_dir);
end
fprintf('--> [%d/%d] Đang xử lý: %s >>> Lưu vào: %s\n', ...
k, length(file_list), current_filename, sub_folder_name);
% Mở file
fid = fopen(full_path, 'r');
if fid == -1
warning('Lỗi mở file %s', full_path);
continue;
end
fseek(fid, 0, 'eof'); file_size = ftell(fid); fseek(fid, 0, 'bof');
img_count = 0;
% --- VÒNG LẶP CẮT ẢNH ---
while ~feof(fid)
img_count = img_count + 1;
raw_data = fread(fid, [2, samples_per_image], 'float32');
if size(raw_data, 2) < samples_per_image
break;
end
% Xử lý tín hiệu
iq_chunk = complex(raw_data(1,:), raw_data(2,:));
% ...
aug_types = {'awgn_light', 'awgn_heavy', 'phase', 'fading', 'burst'};
for a = 2
type_key = aug_types{a};
switch type_key
case 'awgn_light'
% Cộng nhiễu bằng 5% đỉnh tín hiệu (Thấy nền nhiễu dâng lên nhẹ)
iq_processed = iq_augment(iq_chunk, 'awgn', 0.05);
case 'awgn_heavy'
% Cộng nhiễu bằng 20% đỉnh tín hiệu (Nền nhiễu dâng cao, Drone mờ đi)
iq_processed = iq_augment(iq_chunk, 'awgn', 5);
case 'phase'
% Lệch pha 0.8 rad (Khoảng 45 độ) -> Vạch tín hiệu sẽ bị toe toét
iq_processed = iq_augment(iq_chunk, 'phase', 0.8);
case 'fading'
% Fading nhanh (biến thiên 200 lần trong khung hình)
iq_processed = iq_augment(iq_chunk, 'fading', 200);
case 'burst'
% 3% thời gian bị nhiễu xung chèn vào
iq_processed = iq_augment(iq_chunk, 'burst', 0.03);
end
% ... (Tiếp tục tạo Spectrogram như cũ) ...
[s, ~, ~] = spectrogram(iq_chunk, window, spec_overlap, nfft, fs, 'centered');
mag = 20*log10(abs(s) + eps);
% --- CHUẨN HÓA GLOBAL ---
mag(mag < GLOBAL_MIN_DB) = GLOBAL_MIN_DB;
mag(mag > GLOBAL_MAX_DB) = GLOBAL_MAX_DB;
mag_norm = (mag - GLOBAL_MIN_DB) / (GLOBAL_MAX_DB - GLOBAL_MIN_DB);
% Tạo ảnh màu RGB
img_rgb = ind2rgb(gray2ind(mag_norm, 256), cmap);
% Lưu file (Quality 95 để giữ chi tiết cho SAHI)
fname = sprintf('seq%02d_%05d_%s.jpg', k, img_count, type_key);
imwrite(img_rgb, fullfile(output_dir, fname), 'Quality', 95);
end
% Lùi đầu đọc (Overlap)
fseek(fid, -bytes_back, 'cof');
end
fclose(fid);
end
fprintf('\n=== HOÀN TẤT! KIỂM TRA FOLDER "%s" ===\n', output_root);