DrawWaveform Component preview

Hi guys

I startet to write a component for drawing a waveform for oszillators. It works with polynom-interpolation.

I’ve a lot to do for finishing it. And my code ist very messy.
So don’t take it for finished. It’s not very nice yet. But I’ll improve it.

Any suggestions are welcome.

http://www.zhwin.ch/~woodtluk/WaveDrawComponent.zip

Cheers LukeS

To smooth the curve, you could use a Path to draw it instead:

Path path;
for (int i=0; i<getWidth(); i++)
{
   int j = /*yCoordinateShownToComponent*/(polynomInterpolation(i));
   if (i == 0)
   {
      path.startNewSubPath(0, j);
   }
   else
   {
      path.lineTo(i, j);
   }   
}
g.strokePath(path, PathStrokeType(1.0f));

Thanks for your idea. I’ve never worked with pathes yet. So I’ll have a look at them.

But right now I have 2 mayor problems with this component:

  1. Conversion between coordinate-systems doesn’t work
  2. I don’t now how to keep the waveform in the range [-1, 1]

Cheers LukeS

could you make the polynomInterpolation() to generalized function to support multiple control point ? to support unlimited control points.

i.e. WaveformComponent::polynomInterpolation(int *points, int num_points, int x);

Additionally, do you know how to make curve function inside MS Powerpoint ?

Callee:
double WaveformComponent::polynomInterpolation(double x)
{
double x0, y0, x1, y1, x2, y2, x3, y3, b0, b1, b2, b3;
x0 = leftKnot->getX();
y0 = leftKnot->getY();
x3 = rightKnot->getX();
y3 = rightKnot->getY();

x1 = lmKnot->getX();
y1 = lmKnot->getY();
x2 = rmKnot->getX();
y2 = rmKnot->getY();

b0 = y0;
b1 = (y1 - y0) / (x1 - x0);
b2 = (y2 - y0 - b1 * (x2 - x0)) / ((x2 - x0) * (x2 - x1));
b3 = (y3 - y0 - b1 * (x3 - x0) - b2 * (x3 - x0)*(x3 - x1)) / ((x3 - x0) * (x3 - x1) * (x3 - x2));

double y = b0 + (x - x0)*(b1 + (x - x1)*(b2 + b3 * (x - x2)));
return y;

}

Caller:
if(_waveFormType == PolynomInterpolation)
{
#if 0
for (int i=0; i<getWidth(); i++)
{
int j = (int)polynomInterpolation(i);
g.setPixel(i, j);
}
#else
Path path;
for (int i=0; i<getWidth(); i++)
{
int j = (int)polynomInterpolation(i);
if (i == 0)
{
path.startNewSubPath(0, j);
}
else
{
path.lineTo(i, j);
}
}
g.strokePath(path, PathStrokeType(1.0f));
#endif
}

Sory I answer so so late. I haven’t been programming for a long time.

Theoretically it would be possible to programm the polynomial interpolation to as many points as you want. But it gets very complicated.

You need to calculate the coefficients (bi) for as many points as you want for your polynomial function. And you see that the formula for b4 is already very complicated.

I am a very bad programmer (still learning the basics). And I don’t understand the mathematical theory about polynomial interpolation very well. I was quite surprised that my code is working.

So in the near future I won’t expand my polynomial interpolation function to support more then four points. Sory about that.
But maybe you could do it yourself. Just find some informations about Newton polinomial interpolation and expand my function.

Please tell me, if you make some progress.

I don’t know very much about Powerpoint. But for mathematical stuff you shoud use Excel (or a real math program).

Cheers LukeS

P.S. I’ll upload my WaveDrawComponent code ASAP