bit error rate calculation 15,11 hamming code graph - matlab

Below, in my code is supposes to find the simulation BER. But I am getting error on this code where nErrors = biterr(dataIn,dataDec2) this line gives matrix dimensions mismatch.
Is there anyone who can help me in this??
close all;
clear all;
M = 2; % Modulation order
k = log2(M); % Bits per symbol
EbNoVec = -4:2:0; % Eb/No values (dB)
No = -10;
numSymPerFrame = 100; % Number of PSK symbols per frame
berEst2 = zeros(size(EbNoVec));
G2=[1 1 0 0 1 0 0 0 0 0 0 0 0 0 0;
0 1 1 0 0 1 0 0 0 0 0 0 0 0 0;
0 0 1 1 0 0 1 0 0 0 0 0 0 0 0;
1 0 1 0 0 0 0 1 0 0 0 0 0 0 0;
1 0 0 1 0 0 0 0 1 0 0 0 0 0 0;
0 1 0 1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 0 0 0 0 1 0 0 0 0;
0 1 1 1 0 0 0 0 0 0 0 1 0 0 0;
1 0 1 1 0 0 0 0 0 0 0 0 1 0 0;
1 1 0 1 0 0 0 0 0 0 0 0 0 1 0;
1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 ];
H2= gen2par(G2);
decoding2 = syndtable(H2);
Pt2 = zeros(size(EbNoVec));
for n = 1:length(EbNoVec)
% Convert Eb/No to SNR
snrdB = EbNoVec(n) + 10*log10(15/11);
% Reset the error and bit counters
numErrs = 0;
numBits = 0;
Pt2(n)= 10^((snrdB-10)/10);
while numErrs < 100
% Generate binary data and convert to symbols
dataIn = randi([0 1],numSymPerFrame,k)
dataSym = bi2de(dataIn)
dataEnc2 = encode(dataIn,15,11,'linear/binary',G2)
% PSK modulation
txSig = pskmod(dataEnc2,M);
% Pass through AWGN channel
rxSig = awgn(txSig,snrdB,'measured');
% Demodulate the noisy signal
rxSym = pskdemod(rxSig,M);
% Convert received symbols to bits
dataOut = de2bi(rxSym,k);
dataDec2 =decode(rxSym,15,11,'linear/binary',G2, decoding2);
% Calculate the number of bit errors
nErrors = biterr(dataIn,dataDec2);
% Increment the error and bit counters
numErrs = numErrs + nErrors;
numBits = numBits + numSymPerFrame*k;
end
% Estimate the BER
berEst2(n) = numErrs/numBits;
end
berTheory2 = berawgn(EbNoVec,'psk',M,'nondiff');
hold on
semilogy(EbNoVec,berEst1,'r','LineWidth',2);

I debugged your code with a breakpoint on Line 46 [nErrors = biterr(dataIn,dataDec2)]. It seems the dataIn array is 100 x 1 and dataDec2 is a 110 x 1 array. 'biterr' function calculates the number of places where the two vectors of equal length differ.
This will give you a better explanation.
https://www.mathworks.com/help/comm/ref/biterr.html

Related

How do I plot the the rows of a matrix as points on a graph?

I am trying to plot a truss bridge with the lines of the bridge showing which forces are present with different colors showing compression and tension. The lines of the bridge are connected by nodes. I based the line width off of the magnitude of the force divided by 1000.
A = [-0.5 1 0 0 0 0 0 0 0 0.5 0 0 0 0 0;
-sqrt(3)/2 0 0 0 0 0 0 0 0 -sqrt(3)/2 0 0 0 0 0;
0 -1 1 0 0 0 0 0 0 0 -0.5 0.5 0 0 0;
0 0 0 0 0 0 0 0 0 0 -sqrt(3)/2 -sqrt(3)/2 0 0 0;
0 0 -1 1 0 0 0 0 0 0 0 0 -0.5 0.5 0;
0 0 0 0 0 0 0 0 0 0 0 0 -sqrt(3)/2 -sqrt(3)/2 0;
0 0 0 -1 0.5 0 0 0 0 0 0 0 0 0 -0.5;
0 0 0 0 -sqrt(3)/2 0 0 0 0 0 0 0 0 0 -sqrt(3)/2;
0 0 0 0 -0.5 -1 0 0 0 0 0 0 0 0 -0.5;
0 0 0 0 0 1 -1 0 0 0 0 0 0 -0.5 0.5;
0 0 0 0 0 0 0 0 0 0 0 0 0 sqrt(3)/2 sqrt(3)/2;
0 0 0 0 0 0 1 -1 0 0 0 -0.5 0.5 0 0;
0 0 0 0 0 0 0 0 0 0 0 sqrt(3)/2 sqrt(3)/2 0 0;
0 0 0 0 0 0 0 1 -1 -0.5 0.5 0 0 0 0;
0 0 0 0 0 0 0 0 0 sqrt(3)/2 sqrt(3)/2 0 0 0 0];
w7 = 800;
w8 = 900;
w9 = 13000;
W = [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; w7; 0; w8; 0; w9];
x = A\W;
nodes = [0 0;
0.5 sqrt(3)/2;
1.5 sqrt(3)/2;
2.5 sqrt(3)/2;
3.5 sqrt(3)/2;
4 0;
3 0;
2 0;
1 0];
beams = [1 2;
2 3;
3 4;
4 5;
5 6;
6 7;
7 8;
8 9;
1 9;
2 9;
3 9;
3 8;
4 8;
4 7;
5 7];
clf; % clear the figure window
set(gcf,'position',[20 50 600 250],'paperpositionmode','auto')
hold on
% Code to plot goes here!
axis equal; % make aspect ratio 1:1
axis([-.5 4.5 -.5 1.5]);
for jj = 1:15
if x(jj,1) > 0
plot(nodes(beams(jj,1:2),1),nodes(beams(jj,1:2),2),'-g','LineWidth',abs(x(jj,1))/1000);
else
plot(nodes(beams(jj,1:2),1),nodes(beams(jj,1:2),2),'-r','LineWidth',abs(x(jj,1))/1000);
end
end
plot(nodes(1:9,1:2),'.k','MarkerSize',80);
print(gcf,'-dpng','truss_bridge_beams.png');
I got the lines to be plotted the way I wanted, but I want to plot the nodes as dots at the row vectors I specified in the nodes matrix. However, when I tried to do that, the dots were scattered across the graph. Can someone help me fix this?
It’s because plot(Y) plots columns of Y against the row index instead of against each other.
plot(nodes(1:9,1), nodes(1:9,2))
should fix the problem.

Store and plot two structures in MATLAB

I am working with structures that two of which are shown below; A and B. I need to store A and B in a way (possibly not using tables) that allows MATLAB to plot them similar to the images.
In my viewpoint, generating a sparse matrix and store the circles in it may be a solution. Here is a function which could generate a circle array in a sparse matrix with given structure & color. What you need is design a table which denotes the color of each item (0 means there a nothing in that position).
Function CircleArrays.m
function [Imgout] = CircleArrays(DataIn,r,s)
%%Draw a circle array with given colors
% Parameters:
% DataIn ---- structures & color
% r ---- the radius of each circle
% s ---- the sparsity of the data
% Demo:
% S=rand(10);
% r=20;
% sparsity=1;
% Img=CircleArrays(S,r,sparsity);
% figure()
% colormap(hot)
% imagesc(1-Img)
% axis equal
% axis off
%Define a pre-allocate sparse matrix
[m,n]=size(DataIn);
Imgout=spalloc(m*(2*r+1),n*(2*r+1),floor(n*(2*r+1)*m*(2*r+1)*s));
%Create the Mask
I = 1:(2*r+1); % Define the x and y coordinates of the basic mask
x = (I - r)-1;
y = (r - I)+1;
[X,Y] = meshgrid(x,y); % Create the mask
M = (X.^2 + Y.^2 <= r^2);
[xx,yy]=ind2sub(size(M),find(M == true));
%%Add circles into the matrix
for ii=1:m
for jj=1:n
if (DataIn(ii,jj) == 0)
continue;
else
MidX=(ii-1)*(2*r+1)+r+1;
MidY=(jj-1)*(2*r+1)+r+1;
Imgout(sub2ind(size(Imgout),MidX+xx-r-1,MidY+yy-r-1))=DataIn(ii,jj);
end
end
end
end
DEMO:
clc; clear;
%%Structures & configs
% define structures & color
S=[0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 .6 0.6 .6 .6 .6 .6 .6 .6 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0;];
%set the radius of each circle and the sparsity of the data
r=20;
sparsity=0.25;
%generate the Structures
Img=CircleArrays(S,r,sparsity);
%%Draw the picture
figure()
colormap(hot)
imagesc(1-Img)
axis equal
axis off
output by DEMO code:
output by DEMO code in the function:
Notes: I wrote this function based on one of my old answers, so the circle mask maybe not 100% perfect, you can change it to whatever you like.

How to create a criss cross diagonal matrix

How to create a matrix like this
A = [a 0 0 0 0 b;
0 a 0 0 b 0;
0 0 a b 0 0;
0 0 b a 0 0;
0 b 0 0 a 0;
b 0 0 0 0 a]
Perhaps something like this
N = 6; % Size of matrix
a = 1; % Example value
b = 2; % Example value
A = a*eye(N) + b*fliplr(eye(N));
A =
1 0 0 0 0 2
0 1 0 0 2 0
0 0 1 2 0 0
0 0 2 1 0 0
0 2 0 0 1 0
2 0 0 0 0 1

Matlab gfrank() function returning the wrong rank

The function gfrank(A,p) in matlab is supposed to return the rank of a matrix A in GF(p) where p is prime.
But it seems that there is something wrong with this function. as it returns impossible values for some matrices.
For example for the 10*9 matrix:
0 1 1 1 1 0 1 0 0
1 0 0 1 0 1 0 1 1
0 1 1 0 1 1 1 0 0
0 1 1 0 1 1 1 0 1
0 1 0 0 0 0 0 0 1
0 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 1 1
0 1 0 1 0 1 0 1 0
0 1 1 1 1 0 0 1 1
in GF(29), the function returns 10, which is impossible as it is larger than one of the dimensions.
The example is found using this code:
M = 10;
pP = [];
Y =[];
for P = 19:100
if isprime(P)
P
pause(3)
for i = 1:100
A=round(rand(M,M-1));
pP = [pP,P];
if(gfrank(A,P) > M-1) % A IS AN (M-1)*M MATRIX.
disp(A); % IF RANK OF A IS GREATER THAN OR
disp(gfrank(A,P)); % EQUAL TO M, WE HAVE AN ERROR!
end
end
end
end

Drawing a line of ones on a matrix

I have a matrix m = zeros(1000, 1000). Within this matrix I want to draw an estimate of the line which passes through 2 points from my matrix. Let's say x = [122 455]; and y = [500 500];.
How can I do this in Matlab? Are there any predefined functions to do this? I am using Matlab 2012b.
I'll denote the two endpoints as p1 and p2 because I'm planning to use x and y for something else. I'm also assuming that the first coordinate of p1 and p2 is x and the second is y. So here's a rather simple way to do it:
Obtain the equation of the line y = ax + b. In MATLAB, this can be done by:
x = p1(1):p2(1)
dx = p2(1) - p1(1);
dy = p2(2) - p1(2);
y = round((x - p1(1)) * dy / dx + p1(2));
Convert the values of x and y to indices of elements in the matrix, and set those elements to 1.
idx = sub2ind(size(m), y, x);
m(idx) = 1;
Example
Here's an example for a small 10-by-10 matrix:
%// This is our initial conditon
m = zeros(10);
p1 = [1, 4];
p2 = [5, 7];
%// Ensure the new x-dimension has the largest displacement
[max_delta, ix] = max(abs(p2 - p1));
iy = length(p1) - ix + 1;
%// Draw a line from p1 to p2 on matrix m
x = p1(ix):p2(ix);
y = round((x - p1(ix)) * (p2(iy) - p1(iy)) / (p2(ix) - p1(ix)) + p1(iy));
m(sub2ind(size(m), y, x)) = 1;
m = shiftdim(m, ix > iy); %// Transpose result if necessary
The result is:
m =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Update: I have patched this algorithm to work when dy > dx by treating the dimension with the largest displacement as if it were the x-dimension, and then transposing the result if necessary.
Neither of the provided answers work for displacements in y greater than in x (dy > dx).
As pointed out, Bresenham's line algorithm is exactly meant for that.
The matlab file provided here works similarly than the examples provided in the other answers but covers all the use-cases.
To relate to the previously provided example, the script can be used like this:
% initial conditions
m = zeros(10);
p1 = [1, 4];
p2 = [5, 10];% note dy > dx
% use file provided on file exchange
[x y] = bresenham(p1(1),p1(2),p2(1),p2(2));
% replace entries in matrix m
m(sub2ind(size(m), y, x)) = 1;
result looks like this:
m =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
For me (matlab R2013b) following line did not work, when p1(1)>p2(2) (":" can not count backwards):
x = p1(1):p2(1);
E.G.:
1:10
1 2 3 4 5 6 7 8 9 10
10:1
Empty matrix: 1-by-0
But it worked when I used linspac instead:
x = linspace(p1(1), p2(1), abs(p2(1)-p1(1))+1);