Trig Performance

I’m trying to improve the performance of my calculations of a hanning window which uses trig functions. Does anyone have a recommendation for a fast trig library?

(This stuff is above my pay grade so I may be talking rubbish!)

As I understand it, you use the trig functions to calculate a set of coefficients, by which you then multiply your DFT samples.

So the calculations you’re talking about only happen at startup, with a few trig operations per DFT bin. Is that really taking up so much time it’s worth optimising? Or are they repeated, for reasons that I’m not familiar with (see my opening comment :slight_smile: ).

I’m using them in a granular module so right now I’m computing the coefficients every frame because the grains might be different lengths.

Here is a snippet of my code that I call every frame (per active grain)

float evalGrain(int channelIndex, Grain & grain){
	int p = grain.srcPos;

	float nOverM = ((float)grain.outPos / grain.length) - 0.5f;
	float tapper = 0.5f * (1.f + std::cos(TWO_PI * nOverM));

	float value = buffer[channelIndex][p];

	grain.srcPos ++;
	if(grain.srcPos >= MAX_BUFFER_SIZE) grain.srcPos = 0;
	grain.outPos ++; = grain.outPos < grain.length;

	return value * tapper;

So what I’m looking for is just a faster cosine function.

Yeah, don’t call trig functions every frame. Is the one in the sdk not adequate for what you are doing?

I thought there was one in the SDK but I looked and couldn’t find it…

I think fundamental pre v2 used t for sine waveform. Inthnk I use it in my basicVCO, although now that I think of it I thinking made my own. All these things are just like a fifth order Taylor series, usually implemented with SIMD. Your are student, aren’t you?

There is a set of polyphonic sse fast trig functions in the SDK. They are based on non-sse functions in the cephes library. These are probably good for what you want, if the license is compatible with your needs.

I had nothing to do with creating these functions, but i did write up an explanation of them if you need it. Examining the Fast Sine Approximations · david-c14/SubmarineFree Wiki · GitHub

1 Like

One would presume that someone selling VCV plugins is comfortable with the VCV sdk licensing issues?

I was more referring to the cephes library, if one was going to use a non-sse version. I don’t remember the licence off the top of my head.

Try a hash table look-up of pre-computed cosine values (maybe thousands), that you initialize at the beginning of the life of your plugin for a somewhat quantized value for X. It could be faster than the cos() function with (perhaps) acceptable enough accuracy.

You can get arbitrarily good approximations with a lookup table. In my BasicVCO I use the SSE polynomial for the “regular” sine wave output, and use an interpolating lookup table for the “super pure” version. (of course an interpolating lookup is much, much better that a straight lookup).