I am trying to store in a Look-Up-Table(sine) sin(x) values normalized between 0 and 255, with x being mapped from [0; 2Pi] to [0; 255], all integers. However when using trying to access the 0th or 256th index it shows up soemtimes as sine[@]
#! /bin/bash
function load_sin {
sine=${1}[@]
PI=3.14159
for angle in {0..256}
do
sine[$angle]=$(awk "BEGIN{ printf \"%8.0f \", ((sin($angle*($PI/128))*255))}")
done
}
function sin {
sine=${1}[@]
angle=$(($2%256))
echo "from sin function :sine[$angle] = ${sine[$angle]}"
}
declare -a sine
load_sin sine
for angle in {0..255}
do
echo -n "from main scope : ${sine[$(($angle%255))]} "
sin sine $(($angle))
done
output is the following:
#from main scope : 0 from sin function :sine[0] = sine[@]
#from main scope : 6 from sin function :sine[1] = 6
#from main scope : 13 from sin function :sine[2] = 13
#...
#from main scope : -25 from sin function :sine[252] = -25
#from main scope : -19 from sin function :sine[253] = -19
#from main scope : -13 from sin function :sine[254] = -13
#from main scope : sine[@] from sin function :sine[255] = -6
What I'd like to return is in every case:
sine[255] = -6
and
sine [0] = 0
sine=${1}[@]to mean or do? If you want an indirect reference to an array, that's whatdeclare -nis for.sine=${1}[@]}, you're changing${sine[0]}because changing the first element of an array is what assigning to that array does.