drn / processs.m
Bannh's picture
Upload 3 files
e56645a verified
clc; clear; close all;
%% 1. CẤU HÌNH ĐƯỜNG DẪN & THAM SỐ
input_folder = "E:\uav_detect\drone-rf\DAUTELEVONANO"; % Folder chứa file .iq
output_root = "E:\uav_detect\drone-rf\DAUTELEVONANO"; % 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));
% 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,:));
[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.jpg', k, img_count);
imwrite(img_rgb, fullfile(output_dir, fname), 'Quality', 95);
% 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);