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.
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.
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?
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:
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.
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)
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.
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.
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*)
]