Group Abstract Group Abstract

Message Boards Message Boards

How to write (For Loop) in the communication system simulation?

GROUPS:
Hi,

I want to simulate simple AWGN communication system by Mathematica.

I tried to write the code, using comparison with MatLab, but I found some command difficult. for example For Loop.

the simulation steps is as the following:

x = transmitted signal

SNR range from 0 to 20 dB

For i=1 to (length SNR)

generating random noise
adding noise to the transmitted signal
y = x + n
y = received signal
compare y and x for total number of errors (number of different bits) (BER)

End For

Ploting BER vs SNR

My problem is how to write this (For loop), since there are several operations inside this loop.

Thanks

Jamal
POSTED BY: Jamal Hussein
Answer
11 months ago
In Mathematica, just like other programming languages, multiple commands can be included in a For loop by separating them with semicolons. Consider this example where the For loop does two different assignments.
a = 0; b = 1;
For[i = 0, i < 4, i++,
      a = a + i;
      b = a*b + 1;
]

The first step to working out how to write your for loop is to write the code for a single loop of it. That is, write the code which performs a single iteration of what you want to do.

Please try writing out the code for one loop of your for loop and test it to make sure that it works as you expect it to.
POSTED BY: Sean Clarke
Answer
11 months ago
Thank you very much for the answer.
if I have two array as the followings:

ab = {1, -1, -1, -1, 1, 1};
ac = {1, -1, -1, 1, 1, 1};

How I can find the difference between them?
Now in out case the result should be 1, since there is one item difference between ab and ac.
How I can find this difference in Mathematica?

Thank you again
POSTED BY: Jamal Hussein
Answer
11 months ago
Thanks for everyone,

two arrays are Transmitted (Tx) (i.e ab in our example) and Received signals (Rx) (i.e ac in our example).
their values are only 1 and -1
I want to find out how many bits (i.e positions) got wrong, so that I can find the error in the receive side.

in my example the result should be 1, because there is only one bit that changed in the receive side.
it mean that I have one error out of 6.

I want to traet the difference between Tx and Rx as a number so that I can do mathematical calculation on it.


For example, in MatLab to calculate the error (or mismatch bits) I do like this:

K>> ab = [1, -1, -1, -1, 1, 1];
K>> ac = [1, -1, -1, 1, 1, 1];
K>> error = 6 - sum(ab==ac)

error  =  1

So, How I do the same calculation in Mathematica.

Note: total number of bits is 6.

Thanks
POSTED BY: Jamal Hussein
Answer
11 months ago
Thanks everyone.
I am now doing the rest of the coding for the simulation of the AWGN.
POSTED BY: Jamal Hussein
Answer
11 months ago
Hi again,

I come out with this code for BER calculation of the AWGN communication system:

 (*THIS CODE IS FOR BER (Bit Error Rate) SIMULATION OF THE AWGN \
 COMMUNICATION SYSTEM*)
 Clear["`*"] ; (*Clear All previous saved variables and symbols*)
 bits = 10^6; (*total number of bits*)
 sym = bits/2; (*total number of symbols*)
 tx = ((2*RandomInteger[1, sym]) -
     1) + (I* ((2*RandomInteger[1, sym]) -
       1));  (*Transmitted signal*)
 For[i = 1, i < 2, i++, snrdB = i; (*value of SNR in dB*)
snr = 10^(0.1*snrdB);(*changing SNR to linear*)
sd = 1/Sqrt[snr/2];   (*calculation of noise vaiance*)
noise = 1/Sqrt[
   2]*(RandomReal[NormalDistribution[], {sym, 1}] +
     I*RandomReal[
       NormalDistribution[], {sym, 1}]);  (*AWGN noise generation*)
y = tx + (sd*noise); (*Received Signal after adding the noise*)
rx = Sign[
    Re[y]] + (I*
     Sign[Im[y]]);  (*Hard dicision demodulation of the Received \
Signal*)
error = Count[tx rx, -1];  (*calculating number of errors*)
ber = error/bits;  (*calculating bit errors rate*)
Print[ber]; (*printing bit errors rate*)
]


the problem is that there must be error, but there is no error !!!!

I just started with simple one loop for SNR = 1 dB, in this case (low SNR) there must be error. However, for high SNR (e.g 20 dB the probability of error is very low, but still there is probability of error.

there is mistake, but I can't point it.

Hope people who have experience help me for fixing this.

Note:
SNR is the signal to noise ratio we take the range from 0 dB to 20 dB, and we measure the BER (Bit Error Rate), then we plot the relation between them.
bits; is the total number of bits, for the purpose of accuracy generally we choose 10^6, but here for simplicity I choose 10^2.

Kind regadrs

Jamal
POSTED BY: Jamal Hussein
Answer
11 months ago
Hi Hussein

This might work if you need all array position, which are different. If the array is still empty there is no difference.
 ab = {1, -1, -1, -1, 1, 1};
 ac = {1, -1, -1, 1, 1, 1};
 
 differentPositionList = {};
 For[j = 1, j <= Length[ac], j++,
    If[ac[[j]] != ab[[j]],
        differentPositionList = Append[differentPositionList, j]
    ]
 ];
differentPositionList
Regards, Markus
POSTED BY: Markus Linder
Answer
11 months ago
If the lists are numerical data only, you can simply take the difference and look for zero. You can use any other test appropriate for your case
r = ab - ac
Position[r, x_ /; FreeQ[x, 0], Heads -> False]
(* {{3}} *)
POSTED BY: Nasser
Answer
11 months ago
One way is to use the Count function in Mathematica and count things that do not have a zero difference:
ab = {1, -1, -1, -1, 1, 1};
ac = {1, -1, -1, 1, 1, 1};

Count[ab - ac, Except[0], {1}]

Or use MapThread with SameQ to get a list of True and False and count the False elements:
result = MapThread[SameQ, {ab, ac}]; (* result = {True, True, True, False, True, True} *)
Count[result, False]
POSTED BY: Arnoud Buzing
Answer
11 months ago
Isn't that what both of my solutions do (return 1 for your example case)?
POSTED BY: Arnoud Buzing
Answer
11 months ago
What are you trying to do here (it's wrong, but without knowing what you are trying to do I can not correct it):
rx = Sign[Re] + (I*Sign[Im]);

Re is a function which takes one argument (a complex number) and returns the real part of that number, so the correct way to use it would be:
Re[z] (* where z is a complex number *)
POSTED BY: Arnoud Buzing
Answer
11 months ago
tx is the transmitted signal which is complex number (Re + Im)
rx is the received signal which is also complex number (Re + Im)
tx has value of 1, -1
rx is the modified version of tx after adding noise to it, we need to recover it, we call this demodulation. we need to get back the origional signal.
the simplest type of demodulation is to take the sign of the signal, if +ve then its 1, if -ve then its -1 (by this operation we get the same format as the transmitted side).
then we compare the tx and rx to find the number of errors.
if the noise is strong we must have error.
POSTED BY: Jamal Hussein
Answer
11 months ago
Here is the MatLab code for performing the above simulation:
 clear all;
 bits=10^6;
 EbN_dB=0:1:20;
 EbNo=10.^(EbN_dB/10);
 s = bits/2;
 x = ((2*(rand(s,1)>0.5)-1) + 1i*(2*(rand(s,1)>0.5)-1));
 for i=1:length(EbNo)
     sd = 1/sqrt(EbNo(i)/2);
     n=1/sqrt(2).*(randn(s,1)+1i*randn(s,1)); 
    y=x+(sd*n);
    xr=sign(real(y))+1i*sign(imag(y));
    g = x==xr;
    err = s-sum(g);
    BER(i)=(err)/s;   
    fprintf('Total number of errors = %d\n',err);
    fprintf('BER = %12.8f\n',BER(i));
end
I am soory I can't paste code inside the (orange spikey/mathematica icon button in the editor). when I paste it paste out the boundary of the Mathematica editor.
POSTED BY: Jamal Hussein
Answer
11 months ago
Here is a slightly better formatted and corrected version (I changed the last Print; statement to actually print something out: Print;)
 (*THIS CODE IS FOR BER (Bit Error Rate) SIMULATION OF THE AWGN \
 COMMUNICATION SYSTEM*)
 Clear["Global`*"];(*Clear All previous saved variables and symbols*)
 bits = 10^2;(*total number of bits*)
 sym = bits/2;(*total number of symbols*)
 tx = ((2*RandomInteger[1, sym]) - 1) + (I*((2*RandomInteger[1, sym]) - 1));(*Transmitted signal*)
 
 For[i = 1, i < 2, i++,
  snrdb = i;(*value of SNR in dB*)
snr = 10^(0.1*snrdb);(*changing SNR to linear*)
sd = 1/Sqrt[snr/2];(*calculation of noise variance*)
noise = 1/Sqrt[2]*(RandomReal[NormalDistribution[], {sym, 1}] + I*RandomReal[NormalDistribution[], {sym, 1}]);(*AWGN noise generation*)
y = tx + (sd*noise);(*Received Signal after adding the noise*)
rx = Sign[Re] + (I*Sign[Im]);(*Hard decision demodulation of the Received Signal*)
error = Count[tx rx, -1];(*calculating number of errors*)
ber = error/bits;(*calculating bit errors rate*)
Print[ber](*printing bit errors rate*)
]
(you can use that orange spikey/mathematica icon button in the editor to format code like this, which makes it easier to read for other people)
POSTED BY: Arnoud Buzing
Answer
11 months ago
OK, I think I understand now. Is this closer to what you need?
 (*THIS CODE IS FOR BER (Bit Error Rate) SIMULATION OF THE AWGN COMMUNICATION SYSTEM*)
 Clear["Global`*"];(*Clear All previous saved variables and symbols*)
 bits = 10^2;(*total number of bits*)
 sym = bits/2;(*total number of symbols*)
 tx = ((2*RandomInteger[1, sym]) - 1) + (I*((2*RandomInteger[1, sym]) - 1));(*Transmitted signal*)
 
 For[i = 1, i < 2, i++,
  snrdb = i;(*value of SNR in dB*)
  snr = 10^(0.1*snrdb);(*changing SNR to linear*)
sd = 1/Sqrt[snr/2];(*calculation of noise variance*)
noise = 1/Sqrt[2]*(RandomReal[NormalDistribution[], {sym, 1}] + I*RandomReal[NormalDistribution[], {sym, 1}]);(*AWGN noise generation*)
y = tx + (sd*noise);(*Received Signal after adding the noise*)
rx = Flatten[Sign[Re[y]] + I*Sign[Im[y]]];(*Hard decision demodulation of the Received Signal*)
error = Unitize[tx - rx];(*calculating number of errors*)
ber = Total[error]/bits;(*calculating bit errors rate*)
Print["Total number of errors: ", Total[error]];
Print["Error rate: ", N[ber]](*printing bit errors rate*)
]
With sample output:
Total number of errors: 14
Error rate: 0.14
POSTED BY: Arnoud Buzing
Answer
11 months ago
EXACTLY.

Thank you very much.

I appreciate your help.

Thanks everyone.
POSTED BY: Jamal Hussein
Answer
11 months ago
its fine now
POSTED BY: Jamal Hussein
Answer
11 months ago
I have a nother question please;

If I have a Matrix with three rows and 100 columbs,
ABC = RandomReal[NormalDistribution[], {3, 100}];

Can I use For loop to call members of the above Matrix for the purpose of doing some calculation?

for example if I want to multiply each members in the first row by a constant number then add these results to a nother row of a nother Matrix.

Thank you
POSTED BY: Jamal Hussein
Answer
11 months ago