We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › fft - minim - code optimization..
Page Index Toggle Pages: 1
fft - minim - code optimization.. (Read 1976 times)
fft - minim - code optimization..
Aug 21st, 2007, 11:38pm
 


hello,

this is more like a syntax question but since its related to the minim library, i decided to post here. I've got some code but I need to make it more efficient or optimize it somehow. I do something like this:

void draw
{
  for (int i = 0; i < fft.specSize(); i++)
  {
   line( x1, y1, fft.getBand(i), fft.getBand(i))  }
  }
}


a lot of times in the code.

This creates some occasional glithces and screen refresh rate problems on my macbook ( 2Ghz intel 2 dual core ) I was thinking, if I create an array and put that getBand value to this array once in every loop of the program (say, in the beginning of ( void draw{} )) and use that array instead of fft.getBand(i) each time I need it, I would save a lot of cpu power. (by not calculating all these every time I use this function) Would it help? If so, can someone show me how to do this?

float[] map;

map = new float[fft.specSize()];

for (i = 0; i < fft.specSize; i++)
{
map[] = fft.getBand(i);
}

this doesn't work.

Also, I set the buffer size for the audio file that is played back as 1024 when creating the new AudioPlayer object. is this the same as fft.specSize() ? (so that I could put 1024 instead of fft.specSize() each time I use this 'for' loop and save some cpu power from calculating the spectrum size again and again)

do you think I get this right?

Can someone suggest how to optimize my code?

Thanks a lot.

Emre.

Re: fft - minim - code optimization..
Reply #1 - Aug 22nd, 2007, 11:59am
 
error wrote on Aug 21st, 2007, 11:38pm:
float[] map;

map = new float[fft.specSize()];

for (i = 0; i < fft.specSize; i++)
{
map[] = fft.getBand(i);
}

this doesn't work.


php programmer 8)

the fft.specSize in the for loop needs to be the same as the one in the map definition (ie with "()")

map[] needs to be map[i] in the loop

as for not working out fft.specSize() on every loop i'd assign it to a local variable and then use that rather than hardcoding 1024 everywhere:

int fftsize = fft.specSize();
for (i = 0 ; i < fftsize ; i++)
...

screen refresh problems may just be a fact of life - synchronisation problems. look into double buffering if the above doesn't help.
Re: fft - minim - code optimization..
Reply #2 - Aug 22nd, 2007, 2:14pm
 
I think you may be wasting your time copying it, as I think that the data is already held within the FFT as an array, which is only updated when you call fft.forward(...) so copying the data won't add any speed up.
Re: fft - minim - code optimization..
Reply #3 - Aug 22nd, 2007, 3:13pm
 


well,

float[] map;
map = new float[fft.specSize()];
for (i = 0; i < fft.specSize; i++)  //this was intended to be fft.specSize()_sorry.
{
map[] = fft.getBand(i);
}

changing map[] to map[i] works perfectly and putting that instead of fft.getBand(i) each time I call it, actually helped resolve the screen refresh rate problem, not completely but mostly. also I don't know if it helps making the application run faster, but

int fftsize = fft.specSize();

also works nicely.

Thanks for the suggestions.

Emre.



Re: fft - minim - code optimization..
Reply #4 - Aug 22nd, 2007, 5:26pm
 
> I think you may be wasting your time copying it, as I think that the data is already held within the FFT as an array, which is only updated when you call fft.forward(...) so copying the data won't add any speed up.

using a lookup to a local copy of an array will be faster than calling a method on an object, i'd've thought. but, yes, you'd have to be doing this lookup multiple times to justify the expense of the copying.
Page Index Toggle Pages: 1