#!/bin/ksh
# AF_Spec_An_DEMO.sh
# KSH88
# @(#)PD KSH v5.2.12 Geek Gadgets Amiga port 97/08/13
# $VER AF_Spec_An.sh_(C)2018_B.Walker_CC0_Licence.

# Clear the screen and print the line below.
printf "%b" "\033c\033[1;1f\033mAF_Spec_An_DEMO.sh, a slow FFT DEMO...\n\n"
printf "This script creates a new file in the current drawer which is then run,\n"
printf "and this new running file creates the Python FFT file, also in the\n"
printf "current drawer along with another file created by the Python code.\n\n"
printf "The three extra files are:\n"
printf "AF_Spec_An.sh, FFT_RAW.py and DATA.txt.\n\nEnjoy...\n"

# Create the main shell script as a stand alone program.
: > AF_Spec_An.sh
chmod 755 AF_Spec_An.sh

# Create the shell script.
cat << 'SPEC_AN' > AF_Spec_An.sh
#!/bin/ksh
# AF_Spec_An.sh
# KSH88
# @(#)PD KSH v5.2.12 Geek Gadgets Amiga port 97/08/13
# $VER AF_Spec_An.sh_(C)2018_B.Walker_CC0_Licence.
# Create the blank files.
: > DATA.txt
: > FFT_RAW.py

# Current variables.
HORIZ=43
VERT=21
DRAW=21

# Create the display, active area x = 64, y = 21.
printf "%b" "\033c\033[1;1f\033[0m"
printf "\
      20 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
         ||       |       |       |       |       |       |       |       ||
      18 ++       +       +       +       +       +       +       +       ++
   R     ||       |       |       |       |       |       |       |       ||
   E  16 ++       +       +       +       +       +       +       +       ++
   L     ||       |       |       |       |       |       |       |       ||
   A  14 ++       +       +       +       +       +       +       +       ++
   T     ||       |       |       |       |       |       |       |       ||
   I  12 ++       +       +       +       +       +       +       +       ++
   V     ||       |       |       |       |       |       |       |       ||
   E  10 ++       +       +       +       +       +       +       +       ++
         ||       |       |       |       |       |       |       |       ||
   L   8 ++       +       +       +       +       +       +       +       ++
   E     ||       |       |       |       |       |       |       |       ||
   V   6 ++       +       +       +       +       +       +       +       ++
   E     ||       |       |       |       |       |       |       |       ||
   L   4 ++       +       +       +       +       +       +       +       ++
         ||       |       |       |       |       |       |       |       ||
       2 ++       +       +       +       +       +       +       +       ++
         ||       |       |       |       |       |       |       |       ||
       0 ++-------+-------+-------+-------+-------+-------+-------+-------++
  FREQ   -32     -24     -16     -8       0      +8      +16     +24     +32
Creating Python code..."

# Create the Python code to do the heavy FFT lifting.
cat << 'PYTHON_CODE' > FFT_RAW.py
# Python 2.0 (#1, Oct 29 2000, 23:53:20)  (SAS/C 6.x] on amiga
# Type "copyright", "credits" or "license" for more information.

# The line below is for progress info ONLY.
print("\033[23;1fNow running the Python FFT function...")

import sys
import cmath

def fft(DATA):
        N=len(DATA)
        if N<=1: return DATA
        EVEN=fft([DATA[K] for K in range(0,N,2)])
        ODD=fft([DATA[K] for K in range(1,N,2)])
        L=[EVEN[K]+cmath.exp(-2j*cmath.pi*K/N)*ODD[K] for K in range(N/2)]
        R=[EVEN[K]-cmath.exp(-2j*cmath.pi*K/N)*ODD[K] for K in range(N/2)]
        return L+R

# List of values for 2 cycles of a square wave, 64 samples in size.
FFT_LIST=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,\
 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\
 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,\
 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

# Python _variables_.
FFT=fft(FFT_LIST)
LENGTH=len(FFT)

# Create the required conversion for the display.
STDOUT=sys.stdout
sys.stdout=open("DATA.txt", "w")
for FFT_LISTING in range(0,LENGTH,1): print("%i" % (int(abs(FFT[FFT_LISTING]))))
sys.stdout=STDOUT

sys.exit()
PYTHON_CODE

printf "%b" "\033[23;1fLoading Python 2.0.x..."

# Now run the Python code.
# THIS PATH TO THE FILE 'Python20' MIGHT NEED TO BE CHANGED TO SUIT YOUR SYSTEM!
/PYTHON/Python20 FFT_RAW.py

# Finally plot the audio spectrum.
# Display window...
# HORIZ, 11 minimum, 75 maximum.
# VERT, 1 minimum, 21 maximum.
# VERT MUST be inverted.
printf "%b" "\033[23;1fNow plotting the graph...                "
while read -r VERT
do
	# Keep within the limits of a full sized NTSC Shell window.
	VERT=$(( 21 - $VERT ))
	if [ $VERT -lt 1 ]
	then
		VERT=1
	fi
	if [ $VERT -gt 21 ]
	then
		VERT=21
	fi
	if [ $HORIZ -eq 43 ]
	then
		VERT=21
	fi
	if [ $HORIZ -gt 75 ]
	then
		# This should never ever be reached.
		break
	fi
	# Plot and slowly draw the points.
	printf "%b" "\033[21;${HORIZ}f\033[1;32m*"
	for DRAW in $( seq 21 $VERT )
	do
		printf "%b" "\033[${DRAW};${HORIZ}f\033[1;32m*"
	done
	HORIZ=$(( $HORIZ + 1 ))
	if [ $HORIZ -gt 75 ]
	then
		HORIZ=12
	fi
done < DATA.txt
printf "%b" "\033[0m\033[23;1f                                 \033[23;1f"
SPEC_AN

# Call the newly created AF_Spec_An.sh file...
./AF_Spec_An.sh