unable to plot semilogy properly

3 views (last 30 days)
hello everyone, I have a code that simulates QAM modulation below:
% QPSK
M = 8;
k = log2(M); % Bits per symbol
EbNoVec = (5:15); % Eb/No values (dB)
numSymPerFrame = 1000; % Number of PSK symbols per frame
berEst = zeros(size(EbNoVec));
for n = 1:length(EbNoVec)
% Convert Eb/No to SNR
snrdB = EbNoVec(n) + 10*log10(k);
% Reset the error and bit counters
numErrs = 0;
numBits = 0;
while numErrs < 200 && numBits < 1e7
% Generate binary data and convert to symbols
data_in = randi([0 1],numSymPerFrame*k,1);
%----------------------------------------------------------------
% Your modulator here:
modSig = pskmod(data_in,M,InputType='bit'); % PSK Modulation
% Pass through AWGN channel:
rxSig = awgn(modSig, snrdB); % Additive White Gaussian Noise with
% increasing snrdB
% Your demodulator here:
data_out = pskdemod(rxSig,M,OutputType='bit'); % PSK Demodulation
%----------------------------------------------------------------
% Calculate the number of bit errors
nErrors = biterr(data_in,data_out);
numErrs = numErrs + nErrors;
numBits = numBits + numSymPerFrame*k;
end
% Estimate the BER
berEst(n) = numErrs/numBits;
fprintf("snrdB: %.6f berEst: %.6f " + ...
" \n \t numErrs: %d numBits: %d\n\n", snrdB, berEst(n), numErrs, numBits);
end
fitEbN0 = EbNoVec(1):0.25:EbNoVec(end); % Interpolation values
berfit(EbNoVec,berEst,fitEbN0);
hold on;
for n=1:length(EbNoVec)
semilogy([EbNoVec(n) EbNoVec(n)],'g-+');
end
hold off;
the code runs well, but unfortunately the semilogy looks like this :
the 'g-+' does not show up on the plot...do you know how to resolve this issue?? any response regarding this problem is really appreciated..thx

Accepted Answer

Star Strider
Star Strider on 2 Mar 2024
Edited: Star Strider on 2 Mar 2024
The easiest way to solve that is to plot each curve on its own y-axis using the yyaxis function —
% QPSK
M = 8;
k = log2(M); % Bits per symbol
EbNoVec = (5:15); % Eb/No values (dB)
numSymPerFrame = 1000; % Number of PSK symbols per frame
berEst = zeros(size(EbNoVec));
for n = 1:length(EbNoVec)
% Convert Eb/No to SNR
snrdB = EbNoVec(n) + 10*log10(k);
% Reset the error and bit counters
numErrs = 0;
numBits = 0;
while numErrs < 200 && numBits < 1e7
% Generate binary data and convert to symbols
data_in = randi([0 1],numSymPerFrame*k,1);
%----------------------------------------------------------------
% Your modulator here:
modSig = pskmod(data_in,M,InputType='bit'); % PSK Modulation
% Pass through AWGN channel:
rxSig = awgn(modSig, snrdB); % Additive White Gaussian Noise with
% increasing snrdB
% Your demodulator here:
data_out = pskdemod(rxSig,M,OutputType='bit'); % PSK Demodulation
%----------------------------------------------------------------
% Calculate the number of bit errors
nErrors = biterr(data_in,data_out);
numErrs = numErrs + nErrors;
numBits = numBits + numSymPerFrame*k;
end
% Estimate the BER
berEst(n) = numErrs/numBits;
fprintf("snrdB: %.6f berEst: %.6f " + ...
" \n \t numErrs: %d numBits: %d\n\n", snrdB, berEst(n), numErrs, numBits);
end
snrdB: 9.771213 berEst: 0.030222 numErrs: 272 numBits: 9000 snrdB: 10.771213 berEst: 0.019667 numErrs: 236 numBits: 12000 snrdB: 11.771213 berEst: 0.012611 numErrs: 227 numBits: 18000 snrdB: 12.771213 berEst: 0.006867 numErrs: 206 numBits: 30000 snrdB: 13.771213 berEst: 0.002806 numErrs: 202 numBits: 72000 snrdB: 14.771213 berEst: 0.000893 numErrs: 201 numBits: 225000 snrdB: 15.771213 berEst: 0.000333 numErrs: 200 numBits: 600000 snrdB: 16.771213 berEst: 0.000063 numErrs: 200 numBits: 3198000 snrdB: 17.771213 berEst: 0.000008 numErrs: 84 numBits: 10002000 snrdB: 18.771213 berEst: 0.000001 numErrs: 11 numBits: 10002000 snrdB: 19.771213 berEst: 0.000000 numErrs: 2 numBits: 10002000
fitEbN0 = EbNoVec(1):0.25:EbNoVec(end); % Interpolation values
figure
yyaxis left
berfit(EbNoVec,berEst,fitEbN0);
yyaxis right
semilogy(EbNoVec, EbNoVec,'g-+')
figure
berfit(EbNoVec,berEst,fitEbN0);
hlgd = findobj(gcf, 'Type','legend');
hlgd.AutoUpdate = 'on';
hold on;
semilogy(EbNoVec, EbNoVec,'g-+', 'DisplayName','EbNoVec')
hold off;
ylim([min([berEst EbNoVec]) max([berEst EbNoVec])] .* [0.1 10])
get(hlgd) % Show Changed Legend Properties
AutoUpdate: on BeingDeleted: off Box: on BusyAction: 'queue' ButtonDownFcn: @bdowncb Children: [0×0 GraphicsPlaceholder] Color: [1 1 1] ContextMenu: [1×1 ContextMenu] CreateFcn: '' DeleteFcn: '' Direction: 'normal' EdgeColor: [0.1500 0.1500 0.1500] FontAngle: 'normal' FontName: 'Helvetica' FontSize: 9 FontWeight: 'normal' HandleVisibility: 'on' HitTest: on Interpreter: 'tex' Interruptible: off ItemHitFcn: @defaultItemHitCallback Layout: [0×0 matlab.ui.layout.LayoutOptions] LineWidth: 0.5000 Location: 'southwest' NumColumns: 1 NumColumnsMode: 'auto' Orientation: 'vertical' Parent: [1×1 Figure] PickableParts: 'visible' Position: [0.1490 0.1363 0.2413 0.1155] Selected: off SelectionHighlight: on String: {'Empirical BER' 'Exp Fit' 'EbNoVec'} Tag: 'legend' TextColor: [0 0 0] Title: [1×1 Text] Type: 'legend' Units: 'normalized' UserData: [] Visible: on
To plot them on the same axes without using yyaxis, set the ylim limits to accommodate both sets of data. (There must be something about the berfit function that does not allow this to be the default.)
EDIT — (2 Mar 2024 at 14:47)
Added second plot and explanation.
EDIT — (2 Mar 2024 at 15:47)
Changed legend.
.
  4 Comments
Abdul Hamzah
Abdul Hamzah on 2 Mar 2024
Edited: Abdul Hamzah on 2 Mar 2024
and how can I change the legend of the plot that is made from the this line here ? berfit(EbNoVec,berEst,fitEbN0);
Btw the plot made by berfit is the right one (if that is what you meant by the last plot)..but do you know how to change its legend?
Star Strider
Star Strider on 2 Mar 2024
Yes.
First, I got its handle with:
hlgd = findobj(gcf, 'Type','legend');
then I changed it to turn 'AutoUpdate' to 'on' and that added ‘EbNoVec’ to it, which is what I wanted to do. (You can change it to not do that by turning 'AutoUpdate' to 'off'.)
I am not certain what else you want to change, however that should also serve as an example of how to change its properties.
The:
get(hlgd)
function call displays the legend properties. Comment that line (or delete it) to prevent the properties from displaying.
.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!