data values are changing when I try to put markers, line width etc.
Show older comments
USING MATLAB Version: (R2021a)
this is the original code and it works fine:
% MATLAB script to analyze IEEE 802.11p SISO performance
% Metrics: PER, Throughput, Latency, Energy Efficiency
% Plots results vs. SNR
figure()
% Simulation parameters
SNR_dB = 0:2:30; % SNR range (dB)
packet_size = 300 * 8; % Packet size in bits (300 bytes for BSM)
num_packets = 10000; % Number of packets for simulation
channel_bandwidth = 10e6; % 10 MHz channel
tx_power = 0.1; % Transmit power in watts (20 dBm = 100 mW)
rx_power = 0.05; % Receive power in watts (for idle listening)
contention_window = 32; % CSMA/CA contention window (slots)
slot_time = 13e-6; % Slot time in seconds (802.11p)
difs = 64e-6; % DIFS time in seconds
% IEEE 802.11p MCS parameters
mcs_indices = 0:7;
modulation = {'BPSK', 'BPSK', 'QPSK', 'QPSK', '16QAM', '16QAM', '64QAM', '64QAM'};
coding_rate = [1/2, 3/4, 1/2, 3/4, 1/2, 3/4, 2/3, 3/4];
data_rates = [3, 4.5, 6, 9, 12, 18, 24, 27] * 1e6; % Data rates in bps
bits_per_symbol = [1, 1, 2, 2, 4, 4, 6, 6]; % Bits per symbol for modulation
% Initialize result arrays
PER = zeros(length(mcs_indices), length(SNR_dB));
Throughput = zeros(length(mcs_indices), length(SNR_dB));
Latency = zeros(length(mcs_indices), length(SNR_dB));
Energy_Efficiency = zeros(length(mcs_indices), length(SNR_dB));
% Rayleigh fading channel model
% BER approximation for modulations in Rayleigh fading
for mcs = 1:length(mcs_indices)
M = 2^bits_per_symbol(mcs); % Modulation order
cr = coding_rate(mcs); % Coding rate
R = data_rates(mcs); % Data rate
for snr_idx = 1:length(SNR_dB)
snr_linear = 10^(SNR_dB(snr_idx)/10); % Convert SNR to linear
% Approximate BER for Rayleigh fading (simplified, based on M-ary modulation)
if strcmp(modulation{mcs}, 'BPSK')
Pb = 0.5 * (1 - sqrt(snr_linear / (1 + snr_linear)));
elseif strcmp(modulation{mcs}, 'QPSK')
Pb = 0.5 * (1 - sqrt(snr_linear / (2 + snr_linear)));
elseif strcmp(modulation{mcs}, '16QAM')
Pb = (2/M) * (1 - sqrt(snr_linear * M / (2 * (M-1) + snr_linear * M)));
else % 64QAM
Pb = (2/M) * (1 - sqrt(snr_linear * M / (2 * (M-1) + snr_linear * M)));
end
% Adjust BER for coding rate (approximation)
Pb = Pb / cr; % Simplified: assumes coding reduces effective BER
% Packet Error Rate: PER = 1 - (1 - BER)^packet_size
PER(mcs, snr_idx) = 1 - (1 - Pb)^packet_size;
% Throughput: R * (1 - PER) * (1 - MAC overhead)
mac_overhead = 0.2; % Assume 20% overhead for 802.11p MAC
Throughput(mcs, snr_idx) = R * (1 - PER(mcs, snr_idx)) * (1 - mac_overhead);
% Latency: Transmission time + contention + retransmissions
tx_time = packet_size / R; % Transmission time per packet
avg_retransmissions = 1 / (1 - PER(mcs, snr_idx)); % Expected retransmissions
contention_delay = difs + (contention_window/2) * slot_time; % Average contention
Latency(mcs, snr_idx) = (tx_time + contention_delay) * avg_retransmissions * 1000; % Convert to ms
% Energy Efficiency: Energy per bit = (Tx + Rx power during contention) / Throughput
energy_per_packet = (tx_power * tx_time + rx_power * contention_delay) * avg_retransmissions;
Energy_Efficiency(mcs, snr_idx) = energy_per_packet / (packet_size * (1 - PER(mcs, snr_idx))); % Joules/bit
end
end
% Plotting results
figure('Position', [100, 100, 1200, 800]);
% Plot 1: Packet Error Rate
subplot(2, 2, 1);
semilogy(SNR_dB, PER');
grid on;
title('Packet Error Rate (PER) vs. SNR');
xlabel('SNR (dB)');
ylabel('PER');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
ylim([1e-4, 1]);
set(gca,'FontWeight','bold','FontSize',12);
% Plot 2: Throughput
subplot(2, 2, 2);
plot(SNR_dB, Throughput' / 1e6);
grid on;
title('Throughput vs. SNR');
xlabel('SNR (dB)');
ylabel('Throughput (Mbps)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
set(gca,'FontWeight','bold','FontSize',12);
% Plot 3: Latency
subplot(2, 2, 3);
plot(SNR_dB, Latency');
grid on;
title('Latency vs. SNR');
xlabel('SNR (dB)');
ylabel('Latency (ms)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
ylim([0, 500]);
set(gca,'FontWeight','bold','FontSize',12);
% Plot 4: Energy Efficiency
subplot(2, 2, 4);
semilogy(SNR_dB, Energy_Efficiency' * 1e9);
grid on;
title('Energy Efficiency vs. SNR');
xlabel('SNR (dB)');
ylabel('Energy Efficiency (nJ/bit)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
set(gca,'FontWeight','bold','FontSize',12);
% Adjust layout
sgtitle('IEEE 802.11p SISO Performance Analysis');
BUT IF I WANT TO GET CUSTOMZED LINE WDTH, MARKERS ETC, SUBPLOT 4 CHANGES, (EVEN IF USE LOOP) THIS THE MODIFIED CODE:
% IEEE 802.11p SISO Performance Analysis with Styled Plots (Safe Styling)
figure()
% Simulation parameters
SNR_dB = 0:2:30;
packet_size = 300 * 8;
num_packets = 10000;
channel_bandwidth = 10e6;
tx_power = 0.1;
rx_power = 0.05;
contention_window = 32;
slot_time = 13e-6;
difs = 64e-6;
% MCS parameters
mcs_indices = 0:7;
modulation = {'BPSK', 'BPSK', 'QPSK', 'QPSK', '16QAM', '16QAM', '64QAM', '64QAM'};
coding_rate = [1/2, 3/4, 1/2, 3/4, 1/2, 3/4, 2/3, 3/4];
data_rates = [3, 4.5, 6, 9, 12, 18, 24, 27] * 1e6;
bits_per_symbol = [1, 1, 2, 2, 4, 4, 6, 6];
% Initialize results
PER = zeros(length(mcs_indices), length(SNR_dB));
Throughput = zeros(length(mcs_indices), length(SNR_dB));
Latency = zeros(length(mcs_indices), length(SNR_dB));
Energy_Efficiency = zeros(length(mcs_indices), length(SNR_dB));
% Main simulation loop
for mcs = 1:length(mcs_indices)
M = 2^bits_per_symbol(mcs);
cr = coding_rate(mcs);
R = data_rates(mcs);
for snr_idx = 1:length(SNR_dB)
snr_linear = 10^(SNR_dB(snr_idx)/10);
if strcmp(modulation{mcs}, 'BPSK')
Pb = 0.5 * (1 - sqrt(snr_linear / (1 + snr_linear)));
elseif strcmp(modulation{mcs}, 'QPSK')
Pb = 0.5 * (1 - sqrt(snr_linear / (2 + snr_linear)));
elseif strcmp(modulation{mcs}, '16QAM') || strcmp(modulation{mcs}, '64QAM')
Pb = (2/M) * (1 - sqrt(snr_linear * M / (2*(M-1) + snr_linear*M)));
end
Pb = Pb / cr;
PER(mcs, snr_idx) = 1 - (1 - Pb)^packet_size;
mac_overhead = 0.2;
Throughput(mcs, snr_idx) = R * (1 - PER(mcs, snr_idx)) * (1 - mac_overhead);
tx_time = packet_size / R;
avg_retx = 1 / (1 - PER(mcs, snr_idx));
contention_delay = difs + (contention_window/2) * slot_time;
Latency(mcs, snr_idx) = (tx_time + contention_delay) * avg_retx * 1000;
energy_packet = (tx_power * tx_time + rx_power * contention_delay) * avg_retx;
Energy_Efficiency(mcs, snr_idx) = energy_packet / (packet_size * (1 - PER(mcs, snr_idx)));
end
end
% Plotting results
figure('Position', [100, 100, 1200, 800]);
% --------- Plot 1: Packet Error Rate (PER) ---------
subplot(2, 2, 1);
hold on; grid on;
hPER = gobjects(1, length(mcs_indices)); % preallocate handles
for i = 1:length(mcs_indices)
hPER(i) = semilogy(SNR_dB, PER(i, :));
end
title('Packet Error Rate (PER) vs. SNR');
xlabel('SNR (dB)');
ylabel('PER');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
ylim([1e-4, 1]);
set(gca,'FontWeight','bold','FontSize',12);
% Customize styling separately after plotting:
set(hPER(1), 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', 'o');
set(hPER(2), 'LineWidth', 1.5, 'LineStyle', '--', 'Marker', 's');
set(hPER(3), 'LineWidth', 2.0, 'LineStyle', '-', 'Marker', '^');
set(hPER(4), 'LineWidth', 2.0, 'LineStyle', '--', 'Marker', 'd');
set(hPER(5), 'LineWidth', 2.5, 'LineStyle', '-', 'Marker', 'v');
set(hPER(6), 'LineWidth', 2.5, 'LineStyle', '--', 'Marker', 'x');
set(hPER(7), 'LineWidth', 3.0, 'LineStyle', '-', 'Marker', '+');
set(hPER(8), 'LineWidth', 3.0, 'LineStyle', '--', 'Marker', '*');
% --------- Plot 2: Throughput ---------
subplot(2, 2, 2);
hold on; grid on;
hThrpt = gobjects(1, length(mcs_indices));
for i = 1:length(mcs_indices)
hThrpt(i) = plot(SNR_dB, Throughput(i, :) / 1e6);
end
title('Throughput vs. SNR');
xlabel('SNR (dB)');
ylabel('Throughput (Mbps)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
set(gca,'FontWeight','bold','FontSize',12);
set(hThrpt(1), 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', 'o');
set(hThrpt(2), 'LineWidth', 1.5, 'LineStyle', '--', 'Marker', 's');
set(hThrpt(3), 'LineWidth', 2.0, 'LineStyle', '-', 'Marker', '^');
set(hThrpt(4), 'LineWidth', 2.0, 'LineStyle', '--', 'Marker', 'd');
set(hThrpt(5), 'LineWidth', 2.5, 'LineStyle', '-', 'Marker', 'v');
set(hThrpt(6), 'LineWidth', 2.5, 'LineStyle', '--', 'Marker', 'x');
set(hThrpt(7), 'LineWidth', 3.0, 'LineStyle', '-', 'Marker', '+');
set(hThrpt(8), 'LineWidth', 3.0, 'LineStyle', '--', 'Marker', '*');
% --------- Plot 3: Latency ---------
subplot(2, 2, 3);
hold on; grid on;
hLat = gobjects(1, length(mcs_indices));
for i = 1:length(mcs_indices)
hLat(i) = plot(SNR_dB, Latency(i, :));
end
title('Latency vs. SNR');
xlabel('SNR (dB)');
ylabel('Latency (ms)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
ylim([0, 500]);
set(gca,'FontWeight','bold','FontSize',12);
set(hLat(1), 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', 'o');
set(hLat(2), 'LineWidth', 1.5, 'LineStyle', '--', 'Marker', 's');
set(hLat(3), 'LineWidth', 2.0, 'LineStyle', '-', 'Marker', '^');
set(hLat(4), 'LineWidth', 2.0, 'LineStyle', '--', 'Marker', 'd');
set(hLat(5), 'LineWidth', 2.5, 'LineStyle', '-', 'Marker', 'v');
set(hLat(6), 'LineWidth', 2.5, 'LineStyle', '--', 'Marker', 'x');
set(hLat(7), 'LineWidth', 3.0, 'LineStyle', '-', 'Marker', '+');
set(hLat(8), 'LineWidth', 3.0, 'LineStyle', '--', 'Marker', '*');
% --------- Plot 4: Energy Efficiency ---------
subplot(2, 2, 4);
hold on; grid on;
hEE = gobjects(1, length(mcs_indices));
for i = 1:length(mcs_indices)
hEE(i) = semilogy(SNR_dB, Energy_Efficiency(i, :) * 1e9);
end
title('Energy Efficiency vs. SNR');
xlabel('SNR (dB)');
ylabel('Energy Efficiency (nJ/bit)');
legend(arrayfun(@(x) sprintf('MCS %d: %s, %.2f', x-1, modulation{x}, coding_rate(x)), 1:length(mcs_indices), 'UniformOutput', false));
set(gca,'FontWeight','bold','FontSize',12);
set(hEE(1), 'LineWidth', 1.5, 'LineStyle', '-', 'Marker', 'o');
set(hEE(2), 'LineWidth', 1.5, 'LineStyle', '--', 'Marker', 's');
set(hEE(3), 'LineWidth', 2.0, 'LineStyle', '-', 'Marker', '^');
set(hEE(4), 'LineWidth', 2.0, 'LineStyle', '--', 'Marker', 'd');
set(hEE(5), 'LineWidth', 2.5, 'LineStyle', '-', 'Marker', 'v');
set(hEE(6), 'LineWidth', 2.5, 'LineStyle', '--', 'Marker', 'x');
set(hEE(7), 'LineWidth', 3.0, 'LineStyle', '-', 'Marker', '+');
set(hEE(8), 'LineWidth', 3.0, 'LineStyle', '--', 'Marker', '*');
% Adjust layout
sgtitle('IEEE 802.11p SISO Performance Analysis');
I AM NOT ABLE FIND OUT THE PROBLEM, KINDLY HELP AND GUIDE.
Accepted Answer
More Answers (0)
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


