admin 管理员组

文章数量: 1184232

一、准备工作

电脑发出数据的波形图绘制在我的另一篇博客有详细介绍:

根据Wireshark捕获数据包时间和长度绘制电脑发射信号波形-CSDN博客

路由器发送给电脑数据的波形图绘制也在我的另一篇博客有详细介绍:

根据Wireshark捕获数据包时间和长度绘制路由器发送给电脑数据的信号波形-CSDN博客

这里额外介绍了下怎么便捷保存MATLAB变量,可以用来保存绘制的电脑发射信号波形数据和路由器发射信号波形数据:
MATLAB变量便捷存储方法-CSDN博客

二、比较波形绘制

本篇文章把两个波形图绘制在一起,进行比较。程序:

%zhouzhichao
%2025年8月20日
%观察路由器、电脑互相发送的信号
clc
close all
clear
load("D:\无线通信网络认知\通信学报\5G信号\Wireshark波形绘制\direction_192_168_1_103.mat")
y_plot_1 = y_plot;
t_plot_1 = t_plot;
load("D:\无线通信网络认知\通信学报\5G信号\Wireshark波形绘制\source_192_168_1_103.mat")
y_plot_2 = y_plot;
t_plot_2 = t_plot;

%% 绘图
figure; 
stairs(t_plot_1, y_plot_1, 'LineWidth', 1.5);
hold on
y_plot_2=y_plot_2+1;
stairs(t_plot_2, y_plot_2, 'LineWidth', 1.5);

ylim([-0.2 2.2]);
xlabel('Time (s)');
ylabel('Signal Wave');
title('Waveform from Wireshark Packets');
grid on;

set(gca, 'FontName', 'Times New Roman')
set(gca, 'FontSize', 15)

save("D:\无线通信网络认知\通信学报\5G信号\Wireshark波形绘制\save_test.mat","t_plot","y_plot")

source=192.168.1.103的波形和direction=192.168.1.103的波形,其中上面是我的电脑发出的信号波形,下面是我的电脑收到的信号波形(路由器发送):

后面又采集了一次:

三、波形多尺度观察

绿色框里电脑和路由器的波形表现出明显的相互性:

但是黄色框里电脑和路由器的波形显现得比较独立:

最前面我的电脑(红色)发了好多波形,路由器没响应;最后这个其实响应还比较明显,路由器(蓝色)发了很多数据,电脑收到后逐个响应。

放大波形看看:

[3.1, 3.3]

[3.19, 3.194]

xlim([3.19184,3.19187])

四、搭上载波

[3.19184,3.19187]这段信号时长3.19187-3.19184=0.00003s=0.03ms=30ns

2.4GHz下设置24GHz的采样率的话

0.00003s*24GHz=0.00003*24*10^9=720000采样点=72万个采样点

试试能不能仿真出来:

4.1 截取信号

首先把y_plot_1,t_plot_1在[3.19184,3.19187]进行截取:

%% 截取信号
% 定义时间区间
t_start = 3.19184;
t_end = 3.19187;

% 找到在这个时间区间内的索引
indices = t_plot_1 >= (t_start-0.1) & t_plot_1 <= (t_end+0.1);

% 截取数据
t_plot_1_segment = t_plot_1(indices);
y_plot_1_segment = y_plot_1(indices);

% 绘制截取后的波形
figure;
stairs(t_plot_1_segment, y_plot_1_segment, 'LineWidth', 1.5);
xlim([t_start, t_end]);
ylim([-0.2, 2.2]);
xlabel('Time (s)');
ylabel('Signal Wave');
title('Segmented Waveform from Wireshark Packets');
set(gca, 'FontName', 'Times New Roman');
set(gca, 'FontSize', 15);

再绘制波形:

MATLAB直接仿真2.4GHz的载波有些太困难了。

降低采样率:

% 定义载波频率和采样率
fc = 2.4e9;       % 2.4 GHz 载波频率
Fs = 5e9;        % 采样率 24 GHz

4.2 时间轴递增

K>> fprintf('%.8f\n', t_plot_1_segment(1));
3.10319956
K>> fprintf('%.8f\n', t_plot_1_segment(2));
3.10319956
K>> fprintf('%.8f\n', t_plot_1_segment(3));
3.10320100
K>> fprintf('%.8f\n', t_plot_1_segment(4));
3.10320100

把t轴上一样的数值第一次出现时-0.00001:

% 初始偏移量
offset = 0.00001;

% 初始化一个新数组,用于存储调整后的值
t_plot_1_segment_adjusted = t_plot_1_segment;

% 遍历 `t_plot_1_segment` 数组
for i = 2:length(t_plot_1_segment)
    % 如果当前值与前一个值相同,调整当前值
    if t_plot_1_segment_adjusted(i) == t_plot_1_segment_adjusted(i-1)
        t_plot_1_segment_adjusted(i-1) = t_plot_1_segment_adjusted(i-1) - offset;
    end
end

4.3 插值

插值思路:

对y_plot_1_segment和t_plot_1_segment_adjusted进行插值。

首先检查y_plot_1_segment中的两个挨在一起的1,中间补充1,补充1的数量根据两个挨在一起的1对应的t_plot_1_segment_adjusted中的数值差,和采样率Fs计算;

然后,检查y_plot_1_segment中的两个挨在一起的0,中间补充0,补充0的数量根据两个挨在一起的0对应的t_plot_1_segment_adjusted中的数值差,和采样率Fs计算;

最后,检查y_plot_1_segment中的两个挨在一起的0和1,0和1之间补充0,补充0的数量根据两个挨在一起的0对应的t_plot_1_segment_adjusted中的数值差,和采样率Fs计算。

内存还是不够,降低中心频率和采样率:

% 定义载波频率和采样率
fc = 1.2e9;       % 2.4 GHz 载波频率
Fs = 39;        % 采样率 24 GHz

4.4 电磁波

本来还想在调制载波之后优化为电磁波图:

(27 封私信) 用什么软件能画出这样的图? - 知乎


x = (0:2:300)/50;
y = -sin(x*pi); z = cos(x*pi);
[m,M] = cellfun(@(t)deal(min(t),max(t)),{x,y,z});
[yc,zc,xc] = cylinder(1,500); xc = xc*M(1);
figure('render','painter','color','w'), view(44,14), axis image ij off, hold on
line(x,y,z,'linewidth',2,'color','r')
line(xc',yc',zc','color',[1 0.7 0.7])
h = surface(xc,yc,zc,'facecolor','none','edgecolor','r','edgealpha',0.02);
quiver3(x,0*x,0*z,0*x,y,z,0,'k','linewidth',1,'maxhead',0.06);
quiver3([m(1) 0 0],[0 m(2) 0],[0 0 m(3)],...
    [1.2*(M(1)-m(1)) 0 0],[0 1.3*(M(2)-m(2)) 0],[0 0 1.3*(M(3)-m(3))],0,...
    'linewidth',2,'color','k','maxhead',0.08)

现在连载波也仿真不出。

五、主要程序

5.1 Wireshark EXCEL表格转换MAT文件程序

%zhouzhichao
%2025年10月21日
%把Wireshark捕获的数据绘制为波形图

clc
clear
close all

% 读取 Excel 文件
% data = readtable('Oct 21 - Source AP.xlsx');
data = readtable('Oct 21 - Source STA.xlsx');
% 查看前几行
head(data)

% 单独取出时间和长度
time = data.Time;
length = data.Length;
% 基本清洗
mask = ~isnan(time) & ~isnan(length);
time   = time(mask);
length = length(mask);

%% 参数:每字节耗时
% t_per_byte = 1/866.7e6;     
t_per_byte = 1/585e6;   
%% 计算每个包的起止时间
t_end   = time(:);
t_start = t_end - length(:) .* t_per_byte;

% 若有负起点,截到 0(可按需要注释)
t_start = max(t_start, 0);

% 以起点升序排序
intervals = sortrows([t_start t_end], 1);

%% (可选)合并重叠或紧邻的区间,减少锯齿段数量
% “紧邻”的阈值(例如 < 1 ns 认为相连)
touch_eps = 1e-9;  % 1 ns
merged = [];
for k = 1:size(intervals,1)
    s = intervals(k,1); e = intervals(k,2);
    if isempty(merged)
        merged = [s e];
    else
        if s <= merged(end,2) + touch_eps
            % 重叠/相邻:向后扩展
            merged(end,2) = max(merged(end,2), e);
        else
            merged = [merged; s e];
        end
    end
end

%% 生成阶梯波形的 (t, y) 点列
t_plot = [];
y_plot = [];
for k = 1:size(merged,1)
    s = merged(k,1); e = merged(k,2);
    % 对于每个区间 [s, e],追加四个点: (s,0)->(s,1)->(e,1)->(e,0)
    t_plot = [t_plot; s; s; e; e];
    y_plot = [y_plot; 0; 1; 1; 0];
end

% 若希望在图上从 0 持续到首个 s 前也显示 0,可以在最前面加一个点
if ~isempty(t_plot)
    t_plot = [min(0, t_plot(1)); t_plot];
    y_plot = [0; y_plot];
end

%% 绘图
figure; 
idx = find(t_plot==0);
t_plot(idx)=[];
y_plot(idx)=[];
stairs(t_plot, y_plot, 'LineWidth', 1.5);
ylim([-0.2 1.2]);
% xlim([])
xlabel('Time (s)');
ylabel('Signal Wave');
title('Waveform from Wireshark Packets');
grid on;

set(gca, 'FontName', 'Times New Roman')
set(gca, 'FontSize', 15)

% save("D:\无线通信网络认知\通信学报\5G信号\监听模式\数据正式采集\2025年10月21日\Source AP.mat","t_plot","y_plot")
save("D:\无线通信网络认知\通信学报\5G信号\监听模式\数据正式采集\2025年10月21日\Source STA.mat","t_plot","y_plot")

5.2 MAT文件绘制波形程序

%zhouzhichao
%2025年10月20日
%观察路由器、电脑互相发送的信号
clc
close all
clear
load("D:\无线通信网络认知\通信学报\5G信号\监听模式\数据正式采集\2025年10月21日\Source AP.mat")
y_plot_1 = y_plot;
t_plot_1 = t_plot;
load("D:\无线通信网络认知\通信学报\5G信号\监听模式\数据正式采集\2025年10月21日\Source STA.mat")
y_plot_2 = y_plot;
t_plot_2 = t_plot;

%% 单图双波形
figure('Position', [100, 100, 600, 370.8]); % [左, 下, 宽, 高]
a = 2.5;
t_plot_1 = t_plot_1 - a;
stairs(t_plot_1, y_plot_1, 'LineWidth', 1.5);
hold on
y_plot_2=y_plot_2+1.2;
t_plot_2 = t_plot_2 - a;
stairs(t_plot_2, y_plot_2, 'LineWidth', 1.5);

% xlim([0,3])

ylim([-0.2 2.4]);

set(gca, 'YTick', [])
set(gca, 'FontName', 'Times New Roman')
set(gca, 'FontSize', 15)

xlabel('时间/秒', 'FontName', 'SimSun');
ylabel('数据包存在状态', 'FontName', 'SimSun');
set(gca, 'LineWidth', 1);

box off     % 取消边框
ax1 = axes('Position',get(gca,'Position'),'XAxisLocation','top',...
    'YAxisLocation','right','Color','none','XColor','k','YColor','k');  % 设置坐标区
set(ax1,'XTick', [],'YTick', []);   % 去掉xy轴刻度
hold off
set(gca, 'LineWidth', 1);


%% USRP波形
samp_rate = 3.84e6;

file_path = 'E:\25年10月19日采集信号\2.437G signal wifi-open.txt';

fid = fopen(file_path, 'r');

data = fread(fid, 'float32');
 
fclose(fid);
 
real_part = data(1:2:end);  % 取奇数位置的数据作为实部
imag_part = data(2:2:end);  % 取偶数位置的数据作为虚部
 
complex_data = real_part + 1i * imag_part;
 
n = length(complex_data); % 数据长度

figure('Position', [100, 100, 600, 300]); % [左, 下, 宽, 高]
t_all= linspace(0,size(real_part,1)/samp_rate,size(real_part,1));
indice = 1:round(10*samp_rate);
t_all=t_all(indice);
complex_data=complex_data(indice);
plot(t_all,complex_data);

set(gca, 'FontName', 'Times New Roman')
xlabel('时间/秒', 'FontName', 'SimSun');
ylabel('信号幅度', 'FontName', 'SimSun');
xlim([0,3])
ylim([-0.3,0.3])

yticks(-0.3:0.1:0.3);

set(gca, 'FontSize', 15)
set(gca, 'LineWidth', 1);

box off     % 取消边框
ax1 = axes('Position',get(gca,'Position'),'XAxisLocation','top',...
    'YAxisLocation','right','Color','none','XColor','k','YColor','k');  % 设置坐标区
set(ax1,'XTick', [],'YTick', []);   % 去掉xy轴刻度
hold off

set(gca, 'LineWidth', 1);

%% 用Wireshark的数值划出USRP的波形

figure('Position', [100, 100, 600, 300]); % [左, 下, 宽, 高]
% emit_wave = complex_data;
% receive_wave = complex_data;

show_wave = complex_data;
y_plot = y_plot_1;
t_plot = t_plot_1;
% y_plot_2 = y_plot_2 -1.2;
% y_plot = y_plot_2;
% t_plot = t_plot_2;

for i=1:length(t_plot)-1 
    value_1 = y_plot(i);
    point_1 = t_plot(i);
    value_2 = y_plot(i+1);
    point_2 = t_plot(i+1);
    if value_1==0 && value_2==0
        idx = find(t_all > point_1 & t_all < point_2);
        if ~isempty(idx)
            show_wave(idx)=0;
%             disp()
        end
    end
end

plot(t_all,show_wave);
set(gca, 'FontName', 'Times New Roman')
xlabel('时间/秒', 'FontName', 'SimSun');
ylabel('信号幅度', 'FontName', 'SimSun');
xlim([0,3])
% ylim([-0.3,0.3])
ylim([-0.1,0.1])
% yticks(-0.3:0.1:0.3);
yticks(-0.1:0.05:0.1);
set(gca, 'FontSize', 15)
set(gca, 'LineWidth', 1);

box off     % 取消边框
ax1 = axes('Position',get(gca,'Position'),'XAxisLocation','top',...
    'YAxisLocation','right','Color','none','XColor','k','YColor','k');  % 设置坐标区
set(ax1,'XTick', [],'YTick', []);   % 去掉xy轴刻度
hold off

set(gca, 'LineWidth', 1);

本文标签: 波形 路由器 通信 数据 电脑