根据以上情况和要求,更改以下代码:classdef TDMPerformanceAnalyzer < TDMSystem
properties (Access = private)
stabilityHistory % 稳定性分析历史
powerConsumption % 功率消耗
efficiencyHistory % 能效历史
qosScore % QoS分数
end
properties (Constant)
MODULATION_METHODS = {‘BPSK’, ‘QPSK’, ‘16-QAM’};
MODULATION_EFFICIENCY = [1, 2, 4]; % 频谱效率(bps/Hz)
MODULATION_COMPLEXITY = [1, 1.5, 2.5]; % 实现复杂度(相对值)
COLORS = {‘#0072BD’, ‘#D95319’, ‘#EDB120’, ‘#7E2F8E’, ‘#77AC30’, ‘#4DBEEE’, ‘#A2142F’};
MARKERS = {‘o’, ‘s’, ‘^’, ‘d’, ‘p’, ‘h’, ‘>’};
LINE_STYLES = {‘-’, ‘–’, ‘-.’, ‘:’};
end
methods function obj = TDMPerformanceAnalyzer(params) % 构造函数 obj@TDMSystem(params); obj = obj.initializePerformanceData(); % 修复:正确调用私有方法 end function runPerformanceAnalysis(obj) % 运行TDM系统性能分析 fprintf('初始化TDM系统性能分析...\n'); obj = obj.generateSignals(); % 初始时隙分配 obj = obj.allocateSlots(); % 动态性能分析 fprintf('执行动态性能分析...\n'); obj = obj.dynamicPerformanceAnalysis(); % 稳定性分析 fprintf('执行稳定性分析...\n'); obj = obj.stabilityAnalysis(); % 能效分析 fprintf('执行能效分析...\n'); obj = obj.energyEfficiencyAnalysis(); % 系统整体性能评估 fprintf('执行系统整体性能评估...\n'); obj.overallPerformanceEvaluation(); end function obj = dynamicPerformanceAnalysis(obj) % 动态性能分析 iterations = obj.params.iterations; snr_current = obj.params.snrDb; snr_min = 5; snr_decrease_rate = 2; for iter = 1:iterations fprintf('动态分析迭代 #%d (SNR = %.1f dB)...\n', iter, snr_current); % 更新SNR snr_current = max(snr_min, obj.params.snrDb - (iter-1) * snr_decrease_rate); obj.params.snrDb = snr_current; % 系统处理流程 obj = obj.processSystem(); % 评估性能 obj = obj.evaluatePerformance(); % 自适应时隙分配 obj = obj.adaptiveSlotAllocation(); % 计算QoS分数 qos_targets = [0.01, 0.05, 0.1]; % 默认目标误码率 if isfield(obj.params, 'qosTargets') && length(obj.params.qosTargets) >= obj.params.numSignals qos_targets = obj.params.qosTargets(1:obj.params.numSignals); else qos_targets = repmat(qos_targets(1), 1, obj.params.numSignals); end qos = (1 - obj.ber ./ qos_targets')' * 100; qos(qos < 0) = 0; % 确保分数不为负 if isempty(obj.qosScore) obj.qosScore = qos; else obj.qosScore = [obj.qosScore; qos]; end % 可视化当前迭代结果 obj.visualizeDynamicPerformance(iter, snr_current); end end function obj = stabilityAnalysis(obj) % 稳定性分析 clock_drift_max = 0.01; iterations = obj.params.iterations; clock_drift = linspace(0, clock_drift_max, iterations); for iter = 1:iterations fprintf('稳定性分析迭代 #%d (时钟漂移 = %.6f)...\n', iter, clock_drift(iter)); % 更新时钟漂移 obj.params.clockDrift = [0, clock_drift(iter), -clock_drift(iter)]; % 系统处理流程 obj = obj.processSystem(); % 评估性能 obj = obj.evaluatePerformance(); % 记录稳定性分析结果 obj.stabilityHistory = [obj.stabilityHistory; obj.ber']; end % 可视化稳定性分析结果 obj.visualizeStabilityAnalysis(iterations, clock_drift); end function obj = energyEfficiencyAnalysis(obj) % 能效分析 iterations = obj.params.iterations; snr_current = obj.params.snrDb; snr_min = 5; snr_decrease_rate = 2; power_scaling = 0.1; obj.powerConsumption = zeros(iterations, length(obj.MODULATION_METHODS)); obj.efficiencyHistory = zeros(iterations, length(obj.MODULATION_METHODS)); for iter = 1:iterations fprintf('能效分析迭代 #%d (SNR = %.1f dB)...\n', iter, snr_current); % 更新SNR snr_current = max(snr_min, obj.params.snrDb - (iter-1) * snr_decrease_rate); for mod_idx = 1:length(obj.MODULATION_METHODS) % 计算功率消耗 power = obj.calculatePowerConsumption(snr_current, snr_min, ... obj.MODULATION_EFFICIENCY(mod_idx), ... obj.MODULATION_COMPLEXITY(mod_idx), power_scaling); obj.powerConsumption(iter, mod_idx) = power; % 计算能效 efficiency = obj.MODULATION_EFFICIENCY(mod_idx) / power; obj.efficiencyHistory(iter, mod_idx) = efficiency; end end % 可视化能效分析结果 obj.visualizeEnergyEfficiency(iterations); end function visualizeDynamicPerformance(obj, iter, snr) % 可视化动态性能分析结果 fig = figure('Name', ['动态性能分析迭代 #', num2str(iter)], 'Position', [100, 100, 1200, 800]); t = 0:1/obj.params.fs:obj.params.duration-1/obj.params.fs; % 优化布局 gs = tiledlayout(2, 2); gs.Padding = 'compact'; gs.TileSpacing = 'compact'; % 1. 时隙分配 nexttile; b = bar(obj.allocatedSlots); b.FaceColor = obj.COLORS{1}; b.EdgeColor = 'none'; b.LineWidth = 1.5; title(['第' num2str(iter) '次时隙分配 (SNR = ' num2str(snr) 'dB)'], 'FontSize', 12, 'FontWeight', 'bold'); xlabel('信号编号', 'FontSize', 10); ylabel('分配时隙数', 'FontSize', 10); grid on; grid minor; ylim([0, max(obj.allocatedSlots)*1.2]); set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 添加数据标签 for i = 1:length(obj.allocatedSlots) text(i, obj.allocatedSlots(i) + max(obj.allocatedSlots)*0.03, ... num2str(obj.allocatedSlots(i)), ... 'HorizontalAlignment', 'center', 'FontSize', 9, 'FontWeight', 'bold'); end % 2. TDM信号 nexttile; plot(t, obj.tdmSignal, 'Color', obj.COLORS{2}, 'LineWidth', 1.5); title(['第' num2str(iter) '次迭代生成的TDM信号'], 'FontSize', 12, 'FontWeight', 'bold'); xlabel('时间 (s)', 'FontSize', 10); ylabel('幅度', 'FontSize', 10); grid on; grid minor; ylim([-2, 2]); xlim([0, min(0.1, obj.params.duration)]); % 只显示前0.1秒或更短的信号 set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 3. 误码率 nexttile; plot(1:obj.params.numSignals, obj.ber, 'o-', 'LineWidth', 1.5, 'MarkerSize', 8, ... 'MarkerFaceColor', obj.COLORS{3}, 'Color', obj.COLORS{3}); title('估计误码率 (BER)', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('信号编号', 'FontSize', 10); ylabel('误码率', 'FontSize', 10); grid on; grid minor; set(gca, 'YScale', 'log', 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); ylim([1e-6, 1]); % 4. QoS分数 nexttile; if ~isempty(obj.qosScore) && size(obj.qosScore, 2) >= obj.params.numSignals % 确保显示最新的QoS分数 latest_qos = obj.qosScore(end, 1:obj.params.numSignals); % 检查是否有NaN或Inf值 valid_indices = ~isnan(latest_qos) & ~isinf(latest_qos); if any(valid_indices) % 只显示有效值 b = bar(1:obj.params.numSignals, latest_qos); b.FaceColor = obj.COLORS{4}; b.EdgeColor = 'none'; b.LineWidth = 1.5; title('服务质量 (QoS) 分数', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('信号编号', 'FontSize', 10); ylabel('QoS分数 (%)', 'FontSize', 10); grid on; grid minor; ylim([0, 110]); set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 添加数据标签 for i = 1:obj.params.numSignals if valid_indices(i) text(i, latest_qos(i) + 3, sprintf('%.1f%%', latest_qos(i)), ... 'HorizontalAlignment', 'center', 'FontSize', 9, 'FontWeight', 'bold'); end end else % 如果没有有效值,显示提示 text(0.5, 0.5, '无有效QoS数据', 'HorizontalAlignment', 'center', 'FontSize', 12); title('服务质量 (QoS) 分数'); axis off; end else % 如果数据为空或维度不匹配,显示提示 text(0.5, 0.5, '未计算QoS分数', 'HorizontalAlignment', 'center', 'FontSize', 12); title('服务质量 (QoS) 分数'); axis off; end % 添加水印 annotation(fig, 'textbox', [0.7, 0.01, 0.25, 0.05], 'String', ['迭代: ' num2str(iter)], ... 'EdgeColor', 'none', 'HorizontalAlignment', 'right', 'FontSize', 8); % 优化图形外观 set(fig, 'Color', 'white'); set(gcf, 'PaperPositionMode', 'auto'); end function visualizeStabilityAnalysis(obj, iterations, clock_drift) % 可视化稳定性分析结果 fig = figure('Name', '稳定性分析结果', 'Position', [100, 100, 1200, 800]); % 优化布局 gs = tiledlayout(2, 1); gs.Padding = 'compact'; gs.TileSpacing = 'compact'; % 1. 时钟偏差 nexttile; p = plot(1:iterations, clock_drift, '-o', 'LineWidth', 1.5, 'MarkerSize', 8, ... 'MarkerFaceColor', obj.COLORS{1}, 'Color', obj.COLORS{1}); title('时钟偏差模拟', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('时钟偏差(秒)', 'FontSize', 10); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 2. 误码率随时钟偏差变化 nexttile; for i = 1:obj.params.numSignals color_idx = mod(i-1, length(obj.COLORS)) + 1; line_idx = mod(i-1, length(obj.LINE_STYLES)) + 1; marker_idx = mod(i-1, length(obj.MARKERS)) + 1; plot(1:iterations, obj.stabilityHistory(:, i), ... 'LineWidth', 1.5, 'Color', obj.COLORS{color_idx}, ... 'Marker', obj.MARKERS{marker_idx}, 'MarkerSize', 6, ... 'LineStyle', obj.LINE_STYLES{line_idx}); hold on; end hold off; title('误码率随时钟偏差变化', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('误码率 (BER)', 'FontSize', 10); legend(arrayfun(@(x) ['信号' num2str(x)], 1:obj.params.numSignals, 'UniformOutput', false), ... 'Location', 'best', 'FontSize', 9); grid on; grid minor; set(gca, 'YScale', 'log', 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); ylim([1e-6, 1]); % 添加水印 annotation(fig, 'textbox', [0.7, 0.01, 0.25, 0.05], ... 'String', ['时钟漂移范围: 0 - ' num2str(clock_drift(end))], ... 'EdgeColor', 'none', 'HorizontalAlignment', 'right', 'FontSize', 8); % 优化图形外观 set(fig, 'Color', 'white'); set(gcf, 'PaperPositionMode', 'auto'); end function visualizeEnergyEfficiency(obj, iterations) % 可视化能效分析结果 fig = figure('Name', '能效分析结果', 'Position', [100, 100, 1200, 800]); % 优化布局 gs = tiledlayout(2, 1); gs.Padding = 'compact'; gs.TileSpacing = 'compact'; % 1. 功率消耗 nexttile; for i = 1:length(obj.MODULATION_METHODS) color_idx = mod(i-1, length(obj.COLORS)) + 1; line_idx = mod(i-1, length(obj.LINE_STYLES)) + 1; marker_idx = mod(i-1, length(obj.MARKERS)) + 1; plot(1:iterations, obj.powerConsumption(:, i), ... 'LineWidth', 1.5, 'Color', obj.COLORS{color_idx}, ... 'Marker', obj.MARKERS{marker_idx}, 'MarkerSize', 6, ... 'LineStyle', obj.LINE_STYLES{line_idx}); hold on; end hold off; title('功率消耗随迭代变化', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('功率消耗', 'FontSize', 10); legend(obj.MODULATION_METHODS, 'Location', 'best', 'FontSize', 9); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 2. 能效 nexttile; for i = 1:length(obj.MODULATION_METHODS) color_idx = mod(i-1, length(obj.COLORS)) + 1; line_idx = mod(i-1, length(obj.LINE_STYLES)) + 1; marker_idx = mod(i-1, length(obj.MARKERS)) + 1; plot(1:iterations, obj.efficiencyHistory(:, i), ... 'LineWidth', 1.5, 'Color', obj.COLORS{color_idx}, ... 'Marker', obj.MARKERS{marker_idx}, 'MarkerSize', 6, ... 'LineStyle', obj.LINE_STYLES{line_idx}); hold on; end hold off; title('能效随迭代变化', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('能效 (bps/Hz/W)', 'FontSize', 10); legend(obj.MODULATION_METHODS, 'Location', 'best', 'FontSize', 9); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 添加水印 annotation(fig, 'textbox', [0.7, 0.01, 0.25, 0.05], ... 'String', ['调制方式: ' strjoin(obj.MODULATION_METHODS, ', ')], ... 'EdgeColor', 'none', 'HorizontalAlignment', 'right', 'FontSize', 8); % 优化图形外观 set(fig, 'Color', 'white'); set(gcf, 'PaperPositionMode', 'auto'); end function overallPerformanceEvaluation(obj) % 系统整体性能评估 fig = figure('Name', '系统整体性能评估', 'Position', [100, 100, 1200, 1000]); % 计算平均能效 avg_efficiency = mean(obj.efficiencyHistory, 2); % 优化布局 gs = tiledlayout(4, 1); gs.Padding = 'compact'; gs.TileSpacing = 'compact'; % 1. SNR变化 nexttile; snr_values = obj.params.snrDb - (0:obj.params.iterations-1) * 2; snr_values = max(5, snr_values); p = plot(1:obj.params.iterations, snr_values, '-o', 'LineWidth', 1.5, 'MarkerSize', 8, ... 'MarkerFaceColor', obj.COLORS{1}, 'Color', obj.COLORS{1}); title('信噪比 (SNR) 变化', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('SNR (dB)', 'FontSize', 10); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 2. QoS分数 nexttile; if ~isempty(obj.qosScore) for i = 1:obj.params.numSignals color_idx = mod(i-1, length(obj.COLORS)) + 1; line_idx = mod(i-1, length(obj.LINE_STYLES)) + 1; marker_idx = mod(i-1, length(obj.MARKERS)) + 1; plot(1:size(obj.qosScore, 1), obj.qosScore(:, i), ... 'LineWidth', 1.5, 'Color', obj.COLORS{color_idx}, ... 'Marker', obj.MARKERS{marker_idx}, 'MarkerSize', 6, ... 'LineStyle', obj.LINE_STYLES{line_idx}); hold on; end hold off; title('系统服务质量 (QoS) 分数', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('QoS分数', 'FontSize', 10); legend(arrayfun(@(x) ['信号' num2str(x)], 1:obj.params.numSignals, 'UniformOutput', false), ... 'Location', 'best', 'FontSize', 9); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); else text(0.5, 0.5, '未计算QoS分数', 'HorizontalAlignment', 'center', 'FontSize', 12); title('系统服务质量 (QoS) 分数'); axis off; end % 3. 平均能效 nexttile; p = plot(1:obj.params.iterations, avg_efficiency, '-o', 'LineWidth', 1.5, 'MarkerSize', 8, ... 'MarkerFaceColor', obj.COLORS{3}, 'Color', obj.COLORS{3}); title('系统平均能效', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('平均能效 (bps/Hz/W)', 'FontSize', 10); grid on; grid minor; set(gca, 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); % 4. 稳定性分析结果 nexttile; if ~isempty(obj.stabilityHistory) for i = 1:obj.params.numSignals color_idx = mod(i-1, length(obj.COLORS)) + 1; line_idx = mod(i-1, length(obj.LINE_STYLES)) + 1; marker_idx = mod(i-1, length(obj.MARKERS)) + 1; plot(1:size(obj.stabilityHistory, 1), obj.stabilityHistory(:, i), ... 'LineWidth', 1.5, 'Color', obj.COLORS{color_idx}, ... 'Marker', obj.MARKERS{marker_idx}, 'MarkerSize', 6, ... 'LineStyle', obj.LINE_STYLES{line_idx}); hold on; end hold off; title('系统稳定性分析结果', 'FontSize', 12, 'FontWeight', 'bold'); xlabel('迭代次数', 'FontSize', 10); ylabel('误码率 (BER)', 'FontSize', 10); legend(arrayfun(@(x) ['信号' num2str(x)], 1:obj.params.numSignals, 'UniformOutput', false), ... 'Location', 'best', 'FontSize', 9); grid on; grid minor; set(gca, 'YScale', 'log', 'FontSize', 10, 'Box', 'on', 'LineWidth', 1.2); ylim([1e-6, 1]); else text(0.5, 0.5, '未执行稳定性分析', 'HorizontalAlignment', 'center', 'FontSize', 12); title('系统稳定性分析结果'); axis off; end % 打印系统性能评估结果 fprintf('===== 系统整体性能评估 =====\n'); fprintf('总迭代次数: %d\n', obj.params.iterations); fprintf('初始信噪比: %.1f dB\n', obj.params.snrDb); fprintf('最终信噪比: %.1f dB\n', snr_values(end)); fprintf('\n'); % 确保avg_qos的大小与numSignals匹配 if ~isempty(obj.qosScore) avg_qos = mean(obj.qosScore, 1); % 检查avg_qos的长度是否与numSignals一致 if length(avg_qos) < obj.params.numSignals % 如果不一致,填充默认值或调整 avg_qos = [avg_qos, zeros(1, obj.params.numSignals - length(avg_qos))]; end fprintf('平均服务质量 (QoS):\n'); for i = 1:obj.params.numSignals fprintf(' 信号%d: %.1f%%\n', i, avg_qos(i)); end fprintf('\n'); end fprintf('能效分析结果:\n'); for mod_idx = 1:length(obj.MODULATION_METHODS) avg_mod_efficiency = mean(obj.efficiencyHistory(:, mod_idx)); fprintf(' %s: 平均能效 = %.3f bps/Hz/W\n', obj.MODULATION_METHODS{mod_idx}, avg_mod_efficiency); end % 保存评估结果 if isprop(obj.params, 'saveResults') && obj.params.saveResults saveResults(obj, snr_values, avg_qos, avg_efficiency); end % 优化图形外观 set(fig, 'Color', 'white'); set(gcf, 'PaperPositionMode', 'auto'); end function obj = adaptiveSlotAllocation(obj) % 自适应时隙分配 % 根据当前误码率调整各信号的时隙分配 % 基本分配比例 baseAllocation = ones(1, obj.params.numSignals) / obj.params.numSignals; % 如果有误码率数据,根据误码率调整分配 if ~isempty(obj.ber) % 误码率倒数作为优先级指标(误码率越高,优先级越高) priority = 1 ./ (obj.ber + 1e-10); % 避免除零错误 % 归一化优先级 priority = priority / sum(priority); % 结合基本分配和优先级 allocation = 0.7 * baseAllocation + 0.3 * priority'; % 确保总分配时隙数不变 allocation = allocation * sum(obj.allocatedSlots); % 转换为整数 obj.allocatedSlots = round(allocation); % 处理舍入误差 diff = sum(obj.allocatedSlots) - sum(allocation); if diff ~= 0 % 找到最大差异的信号进行调整 [~, idx] = max(abs(obj.allocatedSlots - allocation)); obj.allocatedSlots(idx) = obj.allocatedSlots(idx) - diff; end end fprintf('时隙分配调整为: [%s]\n', num2str(obj.allocatedSlots)); end end methods (Access = private) function obj = initializePerformanceData(obj) % 初始化性能数据存储 obj.stabilityHistory = []; obj.powerConsumption = []; obj.efficiencyHistory = []; obj.qosScore = []; end function obj = processSystem(obj) % 系统处理流程 obj = obj.multiplex(); obj = obj.transmit(); obj = obj.demultiplex(); obj = obj.synchronize(); end function power = calculatePowerConsumption(obj, snr, snr_min, efficiency, complexity, scaling) % 功率消耗模型:与SNR、调制效率和复杂度相关 snr_factor = 1 + (snr - snr_min) / (20 - snr_min); efficiency_factor = 1 / efficiency; complexity_factor = complexity; power = scaling * snr_factor * efficiency_factor * complexity_factor; end end methods (Static) function saveResults(obj, snr_values, avg_qos, avg_efficiency) % 保存分析结果到文件 timestamp = datestr(now, 'yyyymmdd_HHMMSS'); filename = ['TDM_Analysis_Results_' timestamp '.mat']; % 保存所有性能数据 save(filename, 'obj', 'snr_values', 'avg_qos', 'avg_efficiency'); fprintf('分析结果已保存到: %s\n', filename); end end
end