hgk:hacks:mathparser
Differences
This shows you the differences between two versions of the page.
hgk:hacks:mathparser [2015/11/17 12:09] – created michfiel | hgk:hacks:mathparser [2015/11/19 15:03] (current) – removed michfiel | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Math expression parser ===== | ||
- | |||
- | ==== Motivation ==== | ||
- | |||
- | In order to fill a wavetable with waves, a tool is needed to make this task as simple and effective as possible. | ||
- | So the idea to do it with the help of some math was born. An expression parser takes a formula as input and after evaluation all wavetable data gets visualized and can be dumped to the blofeld. | ||
- | |||
- | ==== How it works ==== | ||
- | |||
- | Two variables are set automatically, | ||
- | |||
- | x moves from 0.0 to 1.0 for all 128 samples in a wave.a | ||
- | y moves from 0.0 to 1.0 for each table entry in the range [0..64] | ||
- | |||
- | ==== Overview of implemented operators ==== | ||
- | |||
- | * Basic operators: +, -, * (multiply) and / (divide) | ||
- | * % Mod operator | ||
- | * Exponentiation operator: ^ | ||
- | * Negation: unary - | ||
- | * Assignment: = | ||
- | * Log functions: log(), log2(), ln(), exp() | ||
- | * Transcendental functions: sin(), cos(), tan(), asin(), acos(), atan(), sinh(), cosh(), tanh(), asinh(), acosh(), atanh() | ||
- | * Square root function: sqrt() | ||
- | * [[hgk: | ||
- | * Angular conversion functions: dtor(), rtod() | ||
- | * Absolute value function: abs() | ||
- | * Constants: pi | ||
- | |||
- | ==== Example expressions ==== | ||
- | |||
- | * Sawtooth: 2*x-1 | ||
- | * Rectangle: 2*round(x)-1 | ||
- | |||
- | * Approximated sawtooth | ||
- | * http:// | ||
- | * 0.5 - 1/pi * (sin(2*pi*x) + sin(4*pi*x)/ | ||
- | * 2/pi * (-sin(2*pi*x) + sin(4*pi*x)/ | ||
- | |||
- | * Approximated rectangle | ||
- | * http:// | ||
- | * 4/pi * (sin(2*pi*x) + sin(6*pi*x)/ | ||
- | |||
- | * 3-bit sampled sine function: round(sin(x*2*pi) * 4)/4 | ||
- | |||
- | ==== Gaussian tone burst ==== | ||
- | |||
- | Gauss-Funktion für einen Vektor | ||
- | http:// | ||
- | |||
- | < | ||
- | Mittelwert m und Standardabweichung s | ||
- | |||
- | s=0.5 | ||
- | m=pi/2 | ||
- | |||
- | 4/ | ||
- | |||
- | </ | ||
- | |||
- | ==== Implementation ==== | ||
- | |||
- | [[http:// | ||
- | |||
- | It uses ZExpParser MacZoop expression parser | ||
- | |||
- | The Xcode project compiles fine, just set SDKROOT = macosx and replace cString with UTF8String (in general is a much better choice when converting arbitrary NSStrings into 8-bit representations) | ||
- | |||
- | < | ||
- | GCMathParser* parser = [GCMathParser parser]; | ||
- | NSString* expression = @" | ||
- | for (int sampleIndex = 0; sampleIndex < 64; sampleIndex++) { | ||
- | double x = sampleIndex / 64.; | ||
- | [parser setSymbolValue: | ||
- | wave[sampleIndex] = [parser evaluate: | ||
- | } | ||
- | </ | ||
- | |||
- | Another parser on the web is https:// | ||
hgk/hacks/mathparser.1447758544.txt.gz · Last modified: 2015/11/17 12:09 by michfiel