无线传感器网络(WSN)覆盖仿真(MATLAB 实现)

MATLAB 代码,涵盖 随机部署、网格部署、覆盖率计算、盲区检测、PSO 优化覆盖 等核心功能


一、基础覆盖模型(圆形感知模型)

1.1 主脚本 wsn_coverage_main.m

matlab 复制代码
%% 无线传感器网络覆盖仿真
clear; clc; close all;

%% ===== 1. 网络参数 =====
area = [0 100; 0 100];   % 监测区域 [x_min x_max; y_min y_max]
N = 50;                  % 传感器节点数
R = 12;                  % 感知半径(米)
commR = 20;              % 通信半径(可选)

%% ===== 2. 节点部署 =====
% 方法1:随机部署
nodes = random_deployment(area, N);

% 方法2:网格部署(对比用)
% nodes = grid_deployment(area, sqrt(N));

fprintf('传感器节点数: %d\n', N);
fprintf('感知半径: %.1f m\n', R);
fprintf('监测区域: %.1f × %.1f m²\n', area(2)-area(1), area(4)-area(3));

%% ===== 3. 覆盖率计算 =====
resolution = 1;  % 网格分辨率(米)
[coverage_map, coverage_rate] = calculate_coverage(nodes, R, area, resolution);

fprintf('网络覆盖率: %.2f%%\n', coverage_rate*100);

%% ===== 4. 盲区检测 =====
blind_regions = detect_blind_regions(coverage_map, area, resolution);
fprintf('盲区数量: %d\n', numel(blind_regions));

%% ===== 5. 可视化 =====
visualize_coverage(nodes, R, area, coverage_map, blind_regions);

%% ===== 6. 覆盖优化(PSO)=====
fprintf('\n=== 开始PSO优化覆盖 ===\n');
optimized_nodes = pso_coverage_optimization(nodes, R, area, resolution);
[opt_coverage_map, opt_rate] = calculate_coverage(optimized_nodes, R, area, resolution);
fprintf('优化后覆盖率: %.2f%%\n', opt_rate*100);

% 对比可视化
figure('Color','w','Position',[100 100 1200 500]);
subplot(1,2,1); visualize_coverage(nodes, R, area, coverage_map, blind_regions); title('随机部署');
subplot(1,2,2); visualize_coverage(optimized_nodes, R, area, opt_coverage_map, []); title('PSO优化部署');

二、核心函数

2.1 节点部署函数

matlab 复制代码
function nodes = random_deployment(area, N)
% 随机部署传感器节点
x_min = area(1); x_max = area(2);
y_min = area(3); y_max = area(4);

nodes = zeros(N, 2);
nodes(:,1) = x_min + (x_max-x_min)*rand(N,1);
nodes(:,2) = y_min + (y_max-y_min)*rand(N,1);
end

function nodes = grid_deployment(area, grid_size)
% 网格部署
x = area(1):grid_size:area(2);
y = area(3):grid_size:area(4);
[X, Y] = meshgrid(x, y);
nodes = [X(:), Y(:)];
end

2.2 覆盖率计算(核心)

matlab 复制代码
function [coverage_map, coverage_rate] = calculate_coverage(nodes, R, area, res)
% 计算网络覆盖率
x_min = area(1); x_max = area(2);
y_min = area(3); y_max = area(4);

% 创建网格
x = x_min:res:x_max;
y = y_min:res:y_max;
[X, Y] = meshgrid(x, y);
coverage_map = zeros(size(X));

% 遍历每个网格点
for i = 1:size(X,1)
    for j = 1:size(X,2)
        point = [X(i,j), Y(i,j)];
        % 判断是否在任意节点的感知范围内
        for k = 1:size(nodes,1)
            if norm(point - nodes(k,:)) <= R
                coverage_map(i,j) = 1;
                break;
            end
        end
    end
end

% 计算覆盖率
total_points = numel(coverage_map);
covered_points = sum(coverage_map(:));
coverage_rate = covered_points / total_points;
end

2.3 盲区检测

matlab 复制代码
function blind_regions = detect_blind_regions(coverage_map, area, res)
% 检测盲区(连通区域)
blind_regions = {};
[labeled_map, num_regions] = bwlabel(coverage_map == 0, 4);  % 4连通

for i = 1:num_regions
    [r, c] = find(labeled_map == i);
    if ~isempty(r)
        region = [r, c];
        % 转换为实际坐标
        region_coords = [region(:,2)*res + area(1)-res, ...
                        region(:,1)*res + area(3)-res];
        blind_regions{end+1} = region_coords;
    end
end
end

2.4 可视化函数

matlab 复制代码
function visualize_coverage(nodes, R, area, coverage_map, blind_regions)
% 可视化覆盖效果
hold on; grid on; box on; axis equal;
xlim([area(1) area(2)]);
ylim([area(3) area(4)]);

% 绘制覆盖区域
if ~isempty(coverage_map)
    [X, Y] = meshgrid(area(1):1:area(2), area(3):1:area(4));
    contourf(X, Y, coverage_map, [0.5 0.5], 'FaceColor', [0.8 0.9 1], 'EdgeColor', 'none');
end

% 绘制传感器节点
plot(nodes(:,1), nodes(:,2), 'ro', 'MarkerSize', 6, 'MarkerFaceColor', 'r');

% 绘制感知范围
theta = linspace(0, 2*pi, 100);
for i = 1:size(nodes,1)
    x_circle = nodes(i,1) + R*cos(theta);
    y_circle = nodes(i,2) + R*sin(theta);
    plot(x_circle, y_circle, 'b-', 'LineWidth', 0.3, 'Color', [0.2 0.6 1]);
end

% 绘制盲区
if ~isempty(blind_regions)
    for i = 1:numel(blind_regions)
        region = blind_regions{i};
        plot(region(:,1), region(:,2), 'k.', 'MarkerSize', 2);
    end
end

xlabel('X (m)'); ylabel('Y (m)');
title('无线传感器网络覆盖图');
legend('覆盖区域', '传感器节点', '感知范围', '盲区', 'Location', 'northwest');
end

三、PSO 优化覆盖(高级功能)

3.1 PSO 优化主函数

matlab 复制代码
function optimized_nodes = pso_coverage_optimization(init_nodes, R, area, res)
% PSO优化传感器节点位置
N = size(init_nodes,1);
max_iter = 50;
w = 0.8; c1 = 1.5; c2 = 1.5;  % PSO参数

% 初始化粒子群
particles = init_nodes;
velocities = zeros(N, 2);
p_best = particles;
p_best_fitness = inf(N,1);

% 计算全局最优
[g_best, g_best_fitness] = global_best(particles, R, area, res);

for iter = 1:max_iter
    for i = 1:N
        % 更新速度
        r1 = rand(); r2 = rand();
        velocities(i,:) = w*velocities(i,:) + ...
                         c1*r1*(p_best(i,:) - particles(i,:)) + ...
                         c2*r2*(g_best - particles(i,:));
        
        % 限制速度
        vel_max = 5;
        velocities(i,:) = max(min(velocities(i,:), vel_max), -vel_max);
        
        % 更新位置
        particles(i,:) = particles(i,:) + velocities(i,:);
        
        % 边界处理
        particles(i,:) = max(particles(i,:), [area(1), area(3)]);
        particles(i,:) = min(particles(i,:), [area(2), area(4)]);
        
        % 计算适应度(覆盖率)
        [~, fitness] = calculate_coverage(particles, R, area, res);
        
        % 更新个体最优
        if fitness < p_best_fitness(i)
            p_best_fitness(i) = fitness;
            p_best(i,:) = particles(i,:);
        end
    end
    
    % 更新全局最优
    [new_g_best, new_g_fitness] = global_best(particles, R, area, res);
    if new_g_fitness < g_best_fitness
        g_best = new_g_best;
        g_best_fitness = new_g_fitness;
    end
    
    fprintf('  PSO迭代 %d/%d, 覆盖率=%.2f%%\n', iter, max_iter, (1-g_best_fitness)*100);
end

optimized_nodes = p_best;
end

function [g_best, g_best_fitness] = global_best(particles, R, area, res)
g_best_fitness = inf;
g_best = particles(1,:);

for i = 1:size(particles,1)
    [~, fitness] = calculate_coverage(particles(i,:), R, area, res);
    if fitness < g_best_fitness
        g_best_fitness = fitness;
        g_best = particles(i,:);
    end
end
end

四、三维覆盖扩展(可选)

matlab 复制代码
%% 三维WSN覆盖
area_3d = [0 100; 0 100; 0 30];  % [x; y; z]
nodes_3d = random_deployment_3d(area_3d, N);
R_3d = 15;

% 三维覆盖率计算(类似二维,增加z轴)
[coverage_map_3d, rate_3d] = calculate_coverage_3d(nodes_3d, R_3d, area_3d, 2);

fprintf('三维网络覆盖率: %.2f%%\n', rate_3d*100);

参考代码 基于MATLAB无线传感器网络覆盖 www.youwenfan.com/contentcsw/82015.html

五、运行效果与参数调优

5.1 关键参数建议

参数 建议值 说明
N 30~100 节点数,太少覆盖不足,太多浪费
R 10~20 感知半径,与节点成本相关
resolution 0.5~2 网格分辨率,越小越精确但越慢
commR 1.5×R 通信半径,保证网络连通性

5.2 预期结果

  1. 随机部署:覆盖率约 70%~85%,存在盲区
  2. PSO优化后:覆盖率提升至 90%~95%,盲区显著减少
  3. 三维覆盖:体积覆盖率约 60%~80%(三维更难覆盖)

六、工程应用建议

  1. 连通性检查:添加通信半径,确保网络连通
  2. 能量模型:考虑节点能耗,优化生存时间
  3. 动态覆盖:模拟节点移动或休眠调度
  4. 真实地形:导入DEM高程数据,考虑地形遮挡