File size: 4,919 Bytes
d55b504 |
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 |
% ... (Đoạn code tính toán mag giữ nguyên) ...
clc; clear; close all;
%% 1. CẤU HÌNH ĐƯỜNG DẪN & THAM SỐ
input_folder = "D:\uav_detect\drone-rf\MAVIC3PRO\VTSBW=20"; % Folder chứa file .iq
output_root = "D:\uav_detect\drone-rf\v20"; % Folder tổng chứa kết quả
% --- Global Scale (CỐ ĐỊNH CHO CẢ TẬP) ---
% --- 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, 'pack2_*.iq');
file_list = dir(file_pattern);
function mag_norm = robust_normalize(mag_db)
% prctile(data, p) trả về giá trị phân vị thứ p
min_val = prctile(mag_db(:), 10);
max_val = min_val + 80;
mag_clip = mag_db;
mag_clip(mag_clip < min_val) = min_val;
mag_clip(mag_clip > max_val) = max_val;
mag_norm = (mag_clip - min_val) / (max_val - min_val);
end
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);
%% 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+1);
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);
[num_freq_bins, ~] = size(mag); % Lấy chiều cao ảnh (ví dụ 2048)
% 1. Xử lý BIÊN (Roll-off)
% Thường bỏ khoảng 10% ở mỗi đầu (trên và dưới)
edge_percent = 0.05;
% Tô đen biên dưới (Tần số thấp)
mag_norm = robust_normalize(mag);
% 2. Xử lý GAI DC (DC Spike)
% DC nằm chính giữa trục tần số (khi dùng 'centered')
%dc_center = floor(num_freq_bins / 2) + 1;
%dc_width = 4; % Xóa khoảng 4-5 pixel quanh tâm
% Tô đen vùng tâm
%mag(dc_center-dc_width : dc_center+dc_width, :) = GLOBAL_MIN_DB;
% --- CHUẨN HÓA GLOBAL ---
% Tạo ảnh màu RGB
img_rgb = ind2rgb(gray2ind(mag_norm, 256), cmap);
%[h_img, w_img, ~] = size(img_rgb);
%margin_px = round(h_img * edge_percent);
%img_rgb(1:margin_px+23, :, :) = 0;
% Gán màu đen cho biên trên (Tần số cao)
%img_rgb(end-margin_px+10:end, :, :) = 0;
% 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);
% mag = 20*log10(abs(s) + eps);
%% --- BƯỚC LỌC NHIỄU BIÊN & DC (MASKING) ---
% Mục tiêu: Tô đen những vùng không tin cậy để ảnh đẹp và Model không học sai.
%% --- TIẾP TỤC QUY TRÌNH CHUẨN HÓA NHƯ CŨ ---
% mag(mag < GLOBAL_MIN_DB) = GLOBAL_MIN_DB;
% ... |