User Tools

Site Tools


code:mathparser

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

code:mathparser [2015/11/19 15:03] (current)
michfiel created
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,​ that is:
 +
 +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:​round|Rounding functions]]:​ ceil(), floor(), round(), trunc(), rint(), near()
 +  * 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://​en.wikipedia.org/​wiki/​Sawtooth_wave
 +    * 0.5 - 1/pi * (sin(2*pi*x) + sin(4*pi*x)/​2 + sin(6*pi*x)/​3 ​ + sin(8*pi*x)/​4) + sin(10*x*pi)/​5
 +    * 2/pi * (-sin(2*pi*x) + sin(4*pi*x)/​2 - sin(6*pi*x)/​3 ​ + sin(8*pi*x)/​4) - sin(10*x*pi)/​5
 +
 +    * Approximated rectangle
 +    * http://​en.wikipedia.org/​wiki/​Square_wave
 +      * 4/pi * (sin(2*pi*x) + sin(6*pi*x)/​3 + sin(10*pi*x)/​5 + sin(14*pi*x)/​7 + sin(18*pi*x)/​9
 +
 +    * 3-bit sampled sine function: round(sin(x*2*pi) * 4)/4
 +
 +==== Gaussian tone burst ====
 +
 +Gauss-Funktion für einen Vektor
 +http://​etools.fernuni.ch/​matlab/​matlab3/​de/​html/​loops_learningObject4.html
 +
 +<​code>​
 +Mittelwert m und Standardabweichung s
 +
 +s=0.5
 +m=pi/2
 +
 +4/​(2*sqrt(2*pi)) * exp(-1/​2*((x*pi-m)/​s)^2) * sin(16*pi*x)
 +
 +</​code>​
 +
 +==== Implementation ====
 +
 +[[http://​apptree.net/​parser.htm|GCMathParser]] is a Cocoa class © 2006-2008 Graham Cox 
 +
 +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)
 +
 +<​code>​
 +GCMathParser* parser = [GCMathParser parser];
 +NSString* expression = @"​sin(x)";​
 +for (int sampleIndex = 0; sampleIndex < 64; sampleIndex++) {
 + double x = sampleIndex / 64.;
 +    [parser setSymbolValue:​x forKey:​@"​x"​];​
 +    wave[sampleIndex] = [parser evaluate:​expression];​
 +}
 +</​code>​
 +
 +Another parser on the web is https://​github.com/​davedelong/​DDMathParser ​
  
code/mathparser.txt · Last modified: 2015/11/19 15:03 by michfiel