Help - How to plot a graph with multiple colors?
Mostra commenti meno recenti
I'm trying to plot a graph where I can observe the points of the equation (in the sequence of bits 0 and 1). Each set will have a marker and a different color.
I am using the following code:
L = 1e4; %number of bits
SNRdB = -10:2:20;
SNR = 10.^(SNRdB/10);
alpha = 0.3;
cor = 'rbmkg'; % indicate bits an and bn
ident= '*xv+>'; % indicate bits dI and dQ
for count=1:length(SNRdB)
an = 2*randi([ 0 1 ],1,L)-1;
bn = 2*randi([ 0 1 ],1,L)-1;
dI = 2*randi([ 0 1 ],1,L)-1;
dQ = 2*randi([ 0 1 ],1,L)-1;
sn = an .* ( sqrt(1-alpha) + sqrt(alpha) .* dI ) + 1i .* bn .* ( sqrt(1-alpha) + sqrt(alpha) .* dQ );
end
k=1;
str=[];
figure;
for k1 = 1:4
for k2 = 1:4
str =[ str; num2str(max(an(k1),0)) num2str(max(bn(k1),0)) num2str(max(dI(k2),0)) num2str(max(dQ(k2),0))];
scatter(real(sn(k)),imag(sn(k)),[cor(k1) ident(k2)]);
hold on
k = k+1;
end
end
title('symbols')
legend(str);
axis([-2 3 -2 2])
I'm having as answer the following graph:

I don't know where I'm going wrong.
I would like to obtain a graph with all the combinations and respective colors as follows:

Risposte (2)
Walter Roberson
il 31 Mag 2021
for k1 = 1:4
for k2 = 1:4
str =[ str; num2str(max(an(k1),0)) num2str(max(bn(k1),0)) num2str(max(dI(k2),0)) num2str(max(dQ(k2),0))];
scatter(real(sn(k)),imag(sn(k)),[cor(k1) ident(k2)]);
hold on
k = k+1;
end
end
You are only plotting from the first 4 * 4 = 16 elements of sn, but the first 16 elements of sn might happen to not represent all of the bit combinations. Your sn is length 10000
You can instead do something like
U = uniquetol([real(sn).', imag(sn).'], 'byrows', true);
Ui = complex(U(:,1), U(:,2));
now Ui will be the (16) unique complex values, and you can plot them. However, you will need to figure out which one corresponds to which pattern of bits.
6 Commenti
adriane duarte
il 31 Mag 2021
Modificato: adriane duarte
il 31 Mag 2021
You need to decide whether you are plotting an example of what the constellations would look like, with all of the bits marked for all positions, or if instead you are only wanting to plot the entries that are actually used in the data. In a sufficiently large random sample you can be pretty sure that all of the entries are used at some point, but if you were plotting for an actual message, not all of the entries might be used, and it might be of importance to see which ones were actually used.
If you want to plot an example of what the full constellation would look like, then do not generate any random data: instead, generate all of the possible distinct data points, in an order that you know. For example,
alpha = 0.3;
[an, bn, dI, dQ] = ndgrid([-1 1]);
sn = an .* ( sqrt(1-alpha) + sqrt(alpha) .* dI ) + 1i .* bn .* ( sqrt(1-alpha) + sqrt(alpha) .* dQ );
X = real(sn(:));
Y = imag(sn(:));
scatter(X, Y)
bits = ["0", nan, "1"];
t = bits(an+2) + bits(bn+2) + bits(dI+2) + bits(dQ+2);
text(X, Y, t(:))
You would need to do a bit more work to get the markers right, taking into account that there is no documented method of having different markers for a single scatter plot.
adriane duarte
il 31 Mag 2021
Walter Roberson
il 31 Mag 2021
U = uniquetol([real(sn).', imag(sn).'], 'byrows', true);
Ui = complex(U(:,1), U(:,2));
Ui is now the unique patterns actually used.
[An, Bn, DI, DQ] = ndgrid([-1 1]);
Sn = An .* ( sqrt(1-alpha) + sqrt(alpha) .* DI ) + 1i .* Bn .* ( sqrt(1-alpha) + sqrt(alpha) .* DQ );
Sn is now a 2 x 2 x 2 x 2 of the possible patterns.
[wasfound, idx] = ismembertol(Ui, Sn(:));
[aidx, bidx, diidx, dqidx] = ind2sub(size(Sn), idx(wasfound));
bits = ["0", nan, "1"];
t = bits(aidx+2) + bits(bidx+2) + bits(diidx+2) + bits(dqidx+2);
foundu = Ui(wasfound);
scatter(real(foundu), imag(foundu));
text(real(foundu), imag(foundu), t);
and now all of the unique entries that matched close enough to the nominal patterns are labeled.
Now... remember though that in practice that due to noise in transmission, that the received bits will not generally match the nominal patterns to within round-off error. The way you decide which pattern was being represented differs with different encoding schemes. One of the more common ways is to find the distance between received bits and nominal bits. That would look like
%nominal patterns
[An, Bn, DI, DQ] = ndgrid([-1 1]);
Sn = An .* ( sqrt(1-alpha) + sqrt(alpha) .* DI ) + 1i .* Bn .* ( sqrt(1-alpha) + sqrt(alpha) .* DQ );
%actual patterns are as in variable named sn
[~, whichpattern] = min(squareform(pdist2(sn(:), Sn(:))), [], 2);
corrected_sn = Sn(whichpattern);
upat = unique(whichpattern);
rsn = Sn(upat);
[aidx, bidx, diidx, dqidx] = ind2sub(size(Sn), upat);
bits = ["0", "1"];
t = bits(aidx+2) + bits(bidx+2) + bits(diidx+2) + bits(dqidx+2);
scatter(real(rsn), imag(rsn))
text(real(rsn), imag(rsn), t);
corrected_sn is (if all went well) the received sn corrected to match the nearest nominal sn, and whichpattern is an index into the nominal sn. If you wanted to move from corrected sn to bit pattern for all of the bits then you could follow the same technique I used for creating the text for the unique patterns,
dbits = [0, 1];
[aidx, bidx, diidx, dqidx] = ind2sub(size(Sn), whichpattern);
codes = [dbits(aidx+2), dbits(bidx+2), dbits(diidx+2), dbits(dqidx+2)];
bitstream = reshape(codes.', 1, []);
adriane duarte
il 1 Giu 2021
Walter Roberson
il 1 Giu 2021
[wasfound, idx] = ismembertol([real(Ui(:)), imag(Ui(:))], [real(Sn(:)), imag(Sn(:))], 'byrows', true);
Sulaymon Eshkabilov
il 31 Mag 2021
Hi,
Here are corrected part of your script:
...
for k1 = 1:4
for k2 = 1:4
str =[ str; num2str(max(an(k1),0)) num2str(max(bn(k1),0)) num2str(max(dI(k2),0)) num2str(max(dQ(k2),0))];
scatter(real(sn(k1)),imag(sn(k2)),[cor(k1) ident(k2)]);
hold on
end
end
...
Good luck.
1 Commento
adriane duarte
il 31 Mag 2021
Categorie
Scopri di più su 5G Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
