#!/bin/sh

# Test vtsid, vtsidex

. ./testlib

cleanup(){
   [ "$RDIR" != "" -a -d $RDIR ] && {
      rm -rf $RDIR
   }

   rm -f $TMP.* 
}

RDIR=$TMP.sid
mkdir -p $RDIR

# dF = 11.71875 
M1=4101.5625    # Bin 350 center
M2=4107.421875  # Midway between bins 350,351
M3=4104.492188  # 350.25
M4=4103.236607  # 350 + 1/7

SIG=$TMP.signal

L=+13

clear_data()
{
   rm -rf $RDIR/spec $RDIR/mon
}

#
#  1-axis tests
#

config1()
{
   echo "
    datadir $RDIR
    bins 8192
    monitor_interval 5
    monitor N1 noise,f=$F,w=50
    monitor N2 noise,f=$F,w=8000
    " > $TMP.sid.conf
}

#
#  3-axis tests
#

config3()
{
   echo "
    datadir $RDIR
    bins 8192
    monitor_interval 5
    phase
    channel hfield az=$A1
    channel hfield az=$A2
    channel efield
    monitor S signal,f=$F,w=50
    monitor M cw,f=$F,w=50
    monitor N noise,f=$F,w=50
    " > $TMP.sid.conf
}

generate3()
{
   a1=`echo "$B $A1" | awk '{
         printf( "%.5e\n", cos(($1 - $2)*3.14159265/180))
      }'`
   a2=`echo "$B $A2" | awk '{
         printf( "%.5e\n", cos(($1 - $2)*3.14159265/180))
      }'`

   T=`vtdate -n now`
   vtgen -r192000 -sa=1,f=$F,p=$P -T$T,$L |
      vtmix -c$a1 -c$a2 -c1 > $SIG.join
}

config_msk_fast()
{
   echo "
    datadir $RDIR
    resolution 50
    monitor_interval 0
    phase
    channel hfield az=$A1
    channel hfield az=$A2
    channel efield
    monitor M msk,mod=90,fast,f=$F,br=100,az=$B,w=800
    " > $TMP.sid.conf
}

config_msk_slow()
{
   echo "
    datadir $RDIR
    bins 8192
    monitor_interval 5
    phase
    channel hfield az=$A1
    channel hfield az=$A2
    channel efield
    monitor M1 msk,mod=180,f=$F,br=100,az=$B,w=800
    monitor M2 msk,mod=90,f=$F,br=100,az=$B,w=800
    " > $TMP.sid.conf
}

generate_msk()
{
   a1=`echo "$B $A1" | awk '{
         printf( "%.5e\n", cos(($1 - $2)*3.14159265/180))
      }'`
   a2=`echo "$B $A2" | awk '{
         printf( "%.5e\n", cos(($1 - $2)*3.14159265/180))
      }'`

   T=`vtdate -n now`
   vtgen -r192000 -m a=1,f=$F,b=100.0,p=$P,m=0 -T$T,$L |
      vtmix -c$a1 -c$a2 -c1 > $SIG.join
}

A1=80
A2=340

# Monitor type 'cw'

F=$M2
P=23
B=25

config3 ; generate3 ; clear_data
vtsid -c $TMP.sid.conf $SIG.join
vtsidex -m M $RDIR | awk '{
  if( $2 != 4.056e-01 || $3 != 5.000e-01 || $4 != 7.071e-01 ||
      $5 != 23.0 || $6 != 23.0 || $7 != 23.0 || $8 != 25.0) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t1"

B=147
config3 ; generate3 ; clear_data
vtsid -c $TMP.sid.conf $SIG.join
vtsidex -m M $RDIR | awk '{
  if( $2 != 2.763e-01 || $3 != 6.890e-01 || $4 != 7.071e-01 ||
      $5 != 23.0 || $6 != 203.0 || $7 != 23.0 || $8 != 147.0) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t2"

B=220
config3 ; generate3 ; clear_data
vtsid -c $TMP.sid.conf $SIG.join
vtsidex -m M $RDIR | awk '{
  if( $2 != 5.417e-01 || $3 != 3.536e-01 || $4 != 7.071e-01 ||
      $5 != 203.0 || $6 != 203.0 || $7 != 23.0 || $8 != 220.0) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t3"

B=320
config3 ; generate3 ; clear_data
vtsid -c $TMP.sid.conf $SIG.join
vtsidex -m M $RDIR | awk '{
  if( $2 != 3.536e-01 || $3 != 6.645e-01 || $4 != 7.071e-01 ||
      $5 != 203.0 || $6 != 23.0 || $7 != 23.0 || $8 != 320.0) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t4a"

# Monitor type 'signal'

vtsidex -m S $RDIR | awk '{
  if( $2 != 3.536e-01 || $3 != 6.645e-01 || $4 != 7.071e-01 ||
      $5 != 320.0) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t4b"

# Monitor type 'noise'. 1 unit RMS in 96kHz = 0.0032275 units in 1Hz
F=9000
config1
vtgen -na=1.0 -r192000 -T$T,$L > $SIG.e
clear_data
vtsid -c $TMP.sid.conf $SIG.e
vtsidex -m N1 $RDIR | awk '{
  if( $2 < 2.9e-03 || $2 > 3.5e-3) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t5a"

vtsidex -m N2 $RDIR | awk '{
  if( $2 < 2.9e-03 || $2 > 3.5e-3) exit( 1)
}END{
   if( NR != 2) exit(1);
}'

result "t5b"

# Monitor type 'mskfast'

F=19728.31
B=142
for P in 23 161 219 301
do
   config_msk_fast;  generate_msk; clear_data
   vtsid -c $TMP.sid.conf $SIG.join 

   vtsidex -m M $RDIR | awk '{
      Q = (P+360) % 90
      if( $5 < Q-0.2 || $5 > Q+0.2 || $6 != 142.0) exit( 1)
   }END{
     if( NR < 649) exit(1);
   }' P=$P

   result "t7a$P"
done

# Monitor type 'msk180' and 'msk90'

F=19728.31
B=142
for P in 23 161 219 301
do
   config_msk_slow;  generate_msk; clear_data
   vtsid -c $TMP.sid.conf $SIG.join 

   vtsidex -m M1 $RDIR  | awk '{
      Q = (P+360) % 180
      if( $5 < Q-0.2 || $5 > Q+0.2 || $6 != 142.0) exit( 1)
   }END{
     if( NR != 2) exit(1);
   }' P=$P

   result "t6a$P"

   vtsidex -m M2 $RDIR | awk '{
      Q = (P+360) % 90
      if( $5 < Q-0.2 || $5 > Q+0.2 || $7 != 142.0) exit( 1)
   }END{
     if( NR != 2) exit(1);
   }' P=$P

   result "t6b$P"
done

cleanup

