Sponsored Content
Top Forums Shell Programming and Scripting Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD). Post 303037057 by wisecracker on Monday 22nd of July 2019 12:33:42 PM
Old 07-22-2019
A very practical use of scicalc and this will be the last upload...
I am creating an audio function generator and wanted to use ASCII only for all of the waveforms so that a simple arbitrary waveform could be created using a text editor, and all other waveforms can be edited too.
Well this is not fast but generates a sinewave in pure ASCII only. It is a practical use of 'scicalc'.
Code:
#!/bin/sh

# #!/usr/local/bin/dash
# text_sinewaves.sh

WAVEFORM=""

# 64 bytes per cycle:
# for ANGLE in $( seq 0.0 5.625 359.0 )
# 8 bytes per cycle:
for ANGLE in $( seq 0.0 45.0 359.0 )
do
    FLOAT=$( ~/Desktop/Code/Shell/scicalc "(((sin((${ANGLE}*pi)/180)+1)/2.0)*94)+32" )
    # Don't use awk's """int()""" function as it rounds down only, use """printf "%.0f" ...""" instead.
    WAVEFORM=${WAVEFORM}$( printf \\$( printf "%03o" $( printf "%.0f" "${FLOAT}" ) ) )
done
echo "${WAVEFORM}"
echo "Length = ${#WAVEFORM} bytes."

Results; editing for both for loops:
Code:
Last login: Mon Jul 22 16:18:46 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./text_sinewaves.sh
OTX]aeimpsvxz|}~~~}|zxvspmiea]XTOJFA=951.+(&$"!   !"$&(+.159=AFJ
Length = 64 bytes.
AMIGA:amiga~/Desktop/Code/Shell> ./text_sinewaves.sh
Op~pO. .
Length = 8 bytes.
AMIGA:amiga~/Desktop/Code/Shell> _

How does it work:
First things first, there are 95 normal printable ASCII characters from ASCII 32 decimal " ", and ASCII 126 decimal "~".
The _centreline_ character is ASCII 79 decimal "O", (not ZERO but capital O).
So we have three conditions to take care of, the minimum value must be no less than ASCII 32 " ", the centreline pseudo-DC ASCII 79 "O", (Direct Current level), and finally the maximum value must be no more ASCII 126 "~".

Now consider the expression "(((sin((${ANGLE}*pi)/180)+1)/2.0)*94)+32" ...
There is no need for any start and end parentheses as this is catered for inside 'scicalc'; it does not matter if you add them though.
As 'sin(x)' from 0 to 360 degrees goes from 0 to +1 to 0 to -1 and finally back to 0 again we have to remove the negative part so +1 is added to every calculated result.
This now moves the values to 1 to 2 to 1 to 0 and finally back to 1 again.
When that is done those values are divided by 2 to give the range 0.5 to 1 to 0.5 to 0 and finally back to 0.5.
0.5 is now the centreline, or DC, and this is now multiplied by the (number_of_printable_characters - 1).
We are not done yet; as this will go from 0.0 to 94.0 then the whole has to be shifted up so 0.0 becomes 32.0 and 94.0 becomes 126.0 so 32 must be finally added, and voila, using the script 8 or 64 byte ASCII single cycle pseudo-sinewave is created.
I hope this is lucid enough.
Note that 'printf "%.0f" ' is used to create the integer values, see the code...
(And also doing this manually would take ages.)
Enjoy...

Bazza.

Last edited by wisecracker; 07-22-2019 at 01:49 PM.. Reason: Code tags, adding spaces.
 

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple script to return environment variable

HI , In the below script I am trying to return the value of the environment variable TIBCO_HOME to the caller #! /usr/bin/csh set VAR_NAME=$1 echo VAR_NAME On the aix console.. set to setenv TIBCO_HOME /app/tibco When I execute the script... myscript.sh TIBCO_HOME, the script... (5 Replies)
Discussion started by: bce_groups
5 Replies

2. Shell Programming and Scripting

How to calculate unix epoch time with DC(Calculator)

Hi All, In our code we are using below code to calculate the UNIX Epoch Time from the time stamp present in the file. Can any one explain how the DC operates in below command and how we calculate the UNIX EPOCH time from this. Date incide the file is 20:26:51 Output we get is 1289943554... (3 Replies)
Discussion started by: mithunpatel
3 Replies

3. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

4. Shell Programming and Scripting

Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed. eg. 1 + 1 = or 1 + 1 + 2 + 1 = Both of these should be possible. #!/bin/bash read -p "what's the first number? " n1... (3 Replies)
Discussion started by: redshine6
3 Replies

5. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

6. Programming

Casio FX-CG50 Scientific Calculator and Python

Well, I think I must have this for Christmas! Python on the Casio FX-CG50 with Data Acquisition YT: CASIO fx-CG50 E-CON4 Movie YT: Update Your CG50 Operating System. Now Includes Python! (OS update) This is a great deal on Amazon! See YT Code Tag Video (9 Replies)
Discussion started by: Neo
9 Replies
gnome-calculator(1)						   User Commands					       gnome-calculator(1)

NAME
gnome-calculator, gcalctool - perform basic, financial, and scientific calculations SYNOPSIS
gnome-calculator | gcalctool [-a accuracy] [-D] [-E] [-v] [gnome-std-options] DESCRIPTION
Gnome calculator is a desktop calculator. It has been designed to be used with either the mouse or the keyboard. It is visually similar to a lot of hand-held calculators. Calculator has the following modes: Basic Mode Provides standard calculator functions. You can store numbers in 10 different memory registers, and easily retrieve and replace the numbers in the memory registers. Basic Mode is the default mode. You can use all of the Basic Mode functions in each of the other modes. Financial Mode Provides several complex financial functions. Scientific Mode Provides many additional mathematical functions, including trigonometric and logical functions. You can also store your own functions and constants, when you use Scientific Mode. EXTENDED DESCRIPTION
Some of the calculator keys have menu marks, this indicates that there is a menu associated with that key. One of the most important things to remember about gnome-calculator is that calculations are performed from left to right, with no arith- metic precedence. If you need arithmetic precedence, then you should use parentheses. Internal arithmetic is now down with multi-precision floating point numbers. Accuracy can be adjusted from zero to nine numeric places in fixed notation, but numbers can be displayed in engineering and scientific notation as well. There is also an option to show or remove trailing zeroes after the numeric point. The calculator reverts to scientific notation when the number is larger than the display would allow in fixed notation. In the scientific mode, the base of operation can be changed between binary, octal, decimal and hexidecimal. Num- bers are initially displayed in fixed notation to nine numeric places, with trailing zeroes removed, in the decimal base. You can use the Copy and Paste functions in conjunction with the numeric display to store or retrieve characters from the clipboard. You can also remove the last digit entered, completely clear the displayed entry and totally reset the calculator. There are ten memory registers. Numbers can be stored or retrieved in these locations, and arithmetic can be performed upon register con- tents. Context sensitive help is also available. Control-F1 toggles whether a tooltip is displayed for the item which currently has focus. On startup, Calculator uses the following configuration resources stored in a GConf database. Resource: /schemes/apps/gcalctool/accuracy Values: Accuracy value Description: The number of digits displayed after the numeric point. This value must be in the range 0 to 9. Resource: /schemes/apps/gcalctool/base Values: Numeric Base Description: The initial numeric base. Valid values are "BIN" (binary), "OCT" (octal), "DEC" (deci- mal), and "HEX" (hexadecimal). Resource: /schemes/apps/gcalctool/display Values: Display mode Description: The initial display mode. Valid values are "ENG" (engineering), "FIX" (fixed-point), and SCI" (scientific). Resource: /schemes/apps/gcalctool/mode Values: Mode Description: The initial calculator mode. Valid values are "BASIC", "FINANCIAL", and "SCIENTIFIC". Resource: /schemes/apps/gcalctool/showzeroes Values: true, false (true) Description: Whether to show trailing zeroes after the numeric point in the display value. Resource: /schemes/apps/gcalctool/showthousands Values: true, false (false) Description: Whether fixed numbers in the decimal base are displayed with thousands separated. Resource: /schemes/apps/gcalctool/showregisters Values: true, false (true) Description: Whether to display the memory register window when Calculator starts. Resource: /schemes/apps/gcalctool/trigtype Values: Trig. type Description: The initial trigonometric type. Valid values are "DEG" (degrees), "GRAD" (grads), and "RAD" (radians). Any constants or functions that the user defines are also stored in the GConf database. OPTIONS
The following options are supported: -a accuracy Specifies the number of digits displayed after the numeric point. This value must be in the range 0 to 9. -D Enable debug mode. -E Enable debug mode in the multiple-precision arithmetic package. -v Show the version number and usage information. gnome-std-options Standard options available for use with most GNOME applications. See gnome-std-options(5) for more information. EXAMPLES
Example 1: Launching Calculator example% gnome-calculator EXIT STATUS
The following exit values are returned: 0 Application exited successfully >0 Application exited with failure FILES
The following files are used by this application: /usr/bin/gcalctool Executable for Calculator. /usr/bin/gnome-calculator Symbolic link to gcalctool. ~/.gcalctoolrc User-specific resources for customizing the appearance and color of Calculator. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWgnome-calculator | +-----------------------------+-----------------------------+ |Interface stability |Volatile | +-----------------------------+-----------------------------+ SEE ALSO
Calculator Manual Latest version of the GNOME Desktop User Guide for your platform. attributes(5), gnome-std-options(5) NOTES
Written by Glynn Foster and Rich Burridge, Sun Microsystems Inc., 2003. Updated by Matt Keenan, Sun Microsystems Inc., 2006. SunOS 5.11 12 Sep 2006 gnome-calculator(1)
All times are GMT -4. The time now is 05:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy