Welcome, guest | Sign In | My Account | Store | Cart

This bash script is a taster for a kids level, audio, text mode, sweep generator. The code just creates a single cycle of a quantised sine curve inside an 80 x 24 bash terminal. This will be the calculator for a sinewave sweep generator from about 50Hz the 12KHz... The code tells you more and the display is in comments at the end...

Bash, 57 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# plotsine.sh
# A DEMO to display a sinewave inside a standard bash terminal.
# Issued as Public Domain, 2014, B.Walker, G0LCU.
# Device: Macbook Pro 13", OSX 10.7.5, default bash terminal.
# Use variables so that you can see how it works.
angle=0
step_angle=5
vert_plot=0
horiz_plot=5
centreline=12
amplitude=11
PI=3.14159
clear
# Do a single cycle, quantised graph.
while [ $angle -le 359 ]
do
	# Create each floating point value...
	# CygWin now catered for... ;o)
	vert_plot=$(awk "BEGIN{ printf \"%.12f\", ((sin($angle*($PI/180))*$amplitude)+$centreline)}")
	#vert_plot=$(bc -l <<< "{print ((s($angle*($PI/180))*$amplitude)+$centreline)}")
	# Truncate the floating point value to an integer then invert the plot to suit the x y co-ordinates inside a terminal...
	vert_plot=$((24-${vert_plot/.*}))
	# Plot the point(s) and print the angle at that point...
	printf "\x1B["$vert_plot";"$horiz_plot"f*"
	printf "\x1B[22;1fAngle is $angle degrees..."
	sleep 0.1
	# Increment values...
	angle=$((angle+step_angle))
	horiz_plot=$((horiz_plot+1))
done
printf "\x1B[23;1fSinewave plotted as a quantised text mode graph.\n"
exit 0
#
#                  *********
#               ***         ***
#              *               *
#            **                 **
#           *                     *
#          *                       *
#         *                         *
#        *                           *
#       *                             *
#      *                               *
#    **                                 **
#                                         *                                 *
#                                          *                               *
#                                           *                             *
#                                            *                           *
#                                             *                         *
#                                              *                       *
#                                               *                     *
#                                                **                 **
#                                                  *               *
#Angle is 355 degrees...                            ***         ***
#Sinewave plotted as a quantised text mode graph.      *********
#AMIGA:barrywalker~> _

A DEMO purely as a calculator for a, bash, audio sine wave sweep generator under development. It might also be of use to students to show how to display curves in text mode.

CygWin does not have 'bc' by default so the line with 'bc' inside is commented out and the line with 'awk' added; nothing more sinister than that... ;o)

It now works on CygWin too... ;o)

Enjoy...

1 comment

Barry Walker (author) 9 years, 7 months ago  # | flag

12-08-2014, 10:00pm, UK local time...

Code changed to include CygWin as CygWin does not have the 'bc' command by default...

Created by Barry Walker on Sun, 10 Aug 2014 (CC0)
Bash recipes (41)
Barry Walker's recipes (73)

Required Modules

  • (none specified)

Other Information and Tasks