unable to plot semilogy properly
3 次查看(过去 30 天)
显示 更早的评论
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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1633211/image.png)
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
0 个评论
采纳的回答
Star Strider
2024-3-2
编辑:Star Strider
2024-3-2
% 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
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
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 个评论
Star Strider
2024-3-2
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.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 QAM 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!