Python:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import random | |
from numpy import * | |
import sys | |
def generador_palabra(largo_palabra, frecuencia): | |
"""genera palabras binarias pseudoaleatoriamente | |
usando frecuencias definidas como parametros para los | |
ceros y unos | |
""" | |
palabra = "" | |
for i in range(largo_palabra): | |
x = random.random() | |
if(x < frecuencia): | |
palabra = palabra + str(0) | |
else: | |
palabra = palabra + str(1) | |
return palabra | |
def repetir(num_rep,largo_palabra, frecuencia, Q): | |
"""repite el experimento | |
""" | |
exito = 0 | |
palabra = generador_palabra(largo_palabra, frecuencia) | |
for i in range(num_rep): | |
transmitida = transmite(palabra, Q) | |
if transmitida == palabra: | |
exito = exito + 1 | |
porcentaje = (exito * 100.0)/num_rep | |
return porcentaje | |
def transmite(palabra, prob): | |
"""toma como parametro una palabra binaria y simula su | |
transmision por un canal cuyas probabilidades de transicion | |
estan dadas como parametros de 2x2 | |
""" | |
transmitida = "" | |
for i in palabra: | |
bit = i | |
x = random.random() | |
if x < prob[int(i)]: | |
transmitida = transmitida + i | |
else: | |
if i == "0": | |
transmitida= transmitida + "1" | |
else: | |
transmitida = transmitida + "0" | |
return transmitida | |
largo_palabra = int(sys.argv[1]) | |
frecuencia = float(sys.argv[2]) | |
q_0 = float(sys.argv[3]) | |
q_1 = float(sys.argv[4]) | |
repeticiones = int(sys.argv[5]) | |
porcentaje = repetir(repeticiones,largo_palabra, frecuencia, [q_0,q_1]) | |
print porcentaje |
In the shell script I do repetitions of words of a certain length, also i change the length of the word and the probability of 0 and 1, then in an awk script I can get the standard deviation and the averange, and send this data to the final file and process the data.
Shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pot=0 | |
tam=1 | |
fr=0.5 | |
rep=30 | |
lim=100 | |
init=1 | |
count=0 | |
res=1 | |
palabras=30 | |
touch datos.dat | |
touch tmp.dat | |
rm datos.dat | |
rm tmp.dat | |
#se comienzan a crear las palabras con diferentes tamanos | |
while [[ tam -le lim ]] | |
do | |
for fr in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 | |
#diferentes frecuencias | |
do | |
for q0 in 0.1 0.3 0.5 0.7 | |
#probabilidades de obtener 0 | |
do | |
for q1 in 0.1 0.3 0.5 0.7 | |
#probabilidades de obtener 1 | |
do | |
touch temp.dat | |
rm temp.dat | |
touch temp.dat | |
conta=1 | |
while [[ conta -le palabras ]] | |
#crea 30 palabras con el mismo largo | |
do | |
res=`python tarea_1.py $tam $fr $q0 $q1 $rep` | |
echo $conta $tam $fr $res >> tmp.dat | |
conta=$((1+$conta)) | |
done | |
resu=`./experimento.awk -v tam=$tam -v fr=$fr tmp.dat` | |
echo $pot $fr $resu $q0 $q1 >> datos.dat | |
rm tmp.dat | |
done | |
done | |
done | |
pot=$((1+$pot))#sirve para graficar | |
tam=$((2*$tam)) | |
done |
In awk script I get the average and standard deviation
AWK script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pot=0 | |
tam=1 | |
fr=0.5 | |
rep=30 | |
lim=100 | |
init=1 | |
count=0 | |
res=1 | |
palabras=30 | |
touch datos.dat | |
touch tmp.dat | |
rm datos.dat | |
rm tmp.dat | |
#se comienzan a crear las palabras con diferentes tamanos | |
while [[ tam -le lim ]] | |
do | |
for fr in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 | |
#diferentes frecuencias | |
do | |
for q0 in 0.1 0.3 0.5 0.7 | |
#probabilidades de obtener 0 | |
do | |
for q1 in 0.1 0.3 0.5 0.7 | |
#probabilidades de obtener 1 | |
do | |
touch temp.dat | |
rm temp.dat | |
touch temp.dat | |
conta=1 | |
while [[ conta -le palabras ]] | |
#crea 30 palabras con el mismo largo | |
do | |
res=`python tarea_1.py $tam $fr $q0 $q1 $rep` | |
echo $conta $tam $fr $res >> tmp.dat | |
conta=$((1+$conta)) | |
done | |
resu=`./experimento.awk -v tam=$tam -v fr=$fr tmp.dat` | |
echo $pot $fr $resu $q0 $q1 >> datos.dat | |
rm tmp.dat | |
done | |
done | |
done | |
pot=$((1+$pot))#sirve para graficar | |
tam=$((2*$tam)) | |
done |
With gnuplot I produce a graph to know what happened in the experiment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set grid layerdefault | |
set term png | |
set pm3d map | |
set xlabel "Frecuencia de ceros" offset 1,-1,1 | |
set ylabel "Probabilidades de exito" offset 1,-1,1 | |
set zlabel "Probabilidad de 0" offset 6,4,1 | |
set cblabel "Probabilidad de 0" | |
set title "Largo 4 " | |
set output "largo_4_6.png" | |
set palette color positive | |
set size square | |
set key off | |
splot "cuatro_palabra.dat" using 2:3:5 |
And some plots that can help us to underestand the results.
q0 = 0.1
q1 = 0.1
While the length of the chain grows the probability of success is weakened and if the frequency of 0 is high but the probability of success to 0 It is not as high like the probability of 1 then while the frequencies of 0 going up so these will lose the success rate because In longer words are more failures.
Your readers would probably appreciate having the figure legends and the code commentary in English as well ;) 5 pts for the code, and in the report, having written it in English compensates the spelling errors and it's also worth 5 pts.
ResponderEliminar