Main Archive Specials Wiki | FAQ Links Submit Forum

 Polynominal WaveshaperType : (discrete harmonics)References : Posted by Christian[AT]savioursofsoul[DOT]deNotes : The following code will describe how to excite discrete harmonics and only these harmonics. A simple polynominal waveshaper for processing the data is included as well. However the code don't claim to be optimized. Using a horner scheme with precalculated coefficients should be your choice here. Also remember to oversample the data (optimal in the order of the harmonics) to have them alias free.Code : We assume the input is a sinewave (works for any input signal, but this makes everything more clear). Then we have x = sin(a) the first harmonic is plain simple (using trigonometric identities): cos(2*a)= cos^2(a) - sin^2(a) = 1 - 2 sin^2(a) using the general trigonometric identities: sin(x + y) = sin(x)*cos(y) + sin(y)*cos(x) cos(x + y) = cos(x)*cos(y) - sin(y)*sin(x) together with some math, you can easily calculate: sin(3x), cos(4x), sin(5x), and so on... Here's how the resulting waveshaper may look like: // o = output, i = input o = fPhase[1]* i * fGains[0]+ fPhase[1]*( 2*i*i - 1 ) * fGains[1]+ fPhase[2]*( 4*i*i*i - 3*i ) * fGains[2]+ fPhase[3]*( 8*i*i*i*i - 8*i*i + 1 ) * fGains[3]- fPhase[4]*( 16*i*i*i*i*i - 20*i*i*i + 5 * i ) * fGains[4]+ fPhase[5]*( 32*i*i*i*i*i*i - 48*i*i*i*i + 18 * i*i - 1 ) * fGains[5]- fPhase[6]*( 64*i*i*i*i*i*i*i - 112*i*i*i*i*i + 56 * i*i*i - 7 * i ) * fGains[6]+ fPhase[7]*(128*i*i*i*i*i*i*i*i - 256*i*i*i*i*i*i + 160 * i*i*i*i - 32 * i*i + 1 ) * fGains[7]; fPhase[..] is the sign array and fGains[..] is the gain factor array. P.S.: I don't want to see a single comment about the fact that the code above is unoptimized. I know that!