# ArrayIndexOutOfBoundsException on FFT analyze when bands above 2^14

**URL:** <https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980>\
**Category:** Libraries\
**Created:** [April 4, 2019, 10:04pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980 "2019-04-04T22:04:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 4, 2019, 10:04pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/1 "2019-04-04T22:04:39Z")

</div>

So I know that the amount of bands you assign to an FFT needs to be a power of 2, but when I go over 2^14 (16384) to 2^15 (32768) bands, I get the fft.analyze(spectrum) code highlighted and the error code - ArrayIndexOutOfBoundsException. Is this a limitation of hardware, processing or something else? Is there a way around this? There are many more frequencies than 16384 so this is really inconvenient.

Example code of the problem below.

```auto
import processing.sound.*;
SinOsc sin;
FFT fft;
int bands = 16384; //32768 is out of bounds.
float[] spectrum = new float[bands];

void setup() {
  sin = new SinOsc(this);
  fft = new FFT(this, bands);
  fft.input(sin);
}

void draw() {

}

void mouseReleased() {
sin.stop();
sin.play(440, 1);
fft.analyze(spectrum);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 5, 2019, 12:57am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/2 "2019-04-05T00:57:02Z")

</div>

hi, seems like i could not help you  
[Frequencies not adding up](https://discourse.processing.org/t/frequencies-not-adding-up/9170/4) here,  
with a explanation about bands, sorry.

now for this question, yes, for speed that DFT is done in  
array with integer pointer, using integer values  
( and not double or floating point …) so the band limit on your computer seems reasonable.

again, processing is not doing a FFT, it just sends a sample array  
and when you set ( using sound lib )  
bands = 512;  
it sends 1024 sample batches  
and your FFT shows your 440Hz as a max on about band 10.  
here some tests of your modified code ( show print FFT )

// pls set :  
int bands = 512;  
// and test:  
// Hz  
// 50 in band 1  
// 440 in band 10  
// 10,000 in band 232  
// 20,000 in band 464 of 512 \*

( \* my computer/sound card or my ears not work for this )

so 512 bands is what you have to use.

actually when you complain about frequency resolution of FFT  
it should be on the low range ( \< 50Hz ) if that is a issue.

---

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 5, 2019, 2:06am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/3 "2019-04-05T02:06:03Z")

</div>

Sorry, why do you recommend 512 bands?

I know 16384 gives 32768 bands, but I’d like to use more. No solution on that end?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 5, 2019, 2:36am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/4 "2019-04-05T02:36:12Z")

</div>

> [@Aeggy](#):
>
> I know 16384 gives 32768 bands

using sound lib  
bands 16384 is bands,  
it sends a array what is 32768 samples long  
to the system to make a DFT.  
and gets back a array of 16384 amplitudes  
where [0] is kind of offset/average of all below the frequency  
with a amp in [1].  
so… what is that (lowest ) frequency [1] in that case ??

if your signal is 44kHz it has 44000 samples in one second,  
so 32768 samples are 0.74 seconds  
so in [1] is a amplitude for a 1.3Hz sinus

and your highest?  
in [16383] 22kHz.

longer arrays seems not possible.

and as i show you with  
bands = 512 you can see also frequencies up to 22kHz  
and that is all a FFT can do on a signal with 44kHz  
( sampling theorem )  
and the number of bands can not change anything about that.

i think you still mix up bands and frequencies?  
or i misunderstand your question?

tell us what you are up to pls.

---

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 5, 2019, 2:57am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/5 "2019-04-05T02:57:43Z")

</div>

Ok, I think I actually get the difference with regards to the bands now. The amount of times the original clip is sampled by the FFT is double the band amount, yes? I’ve been doing the calculation right, but just didn’t seem to get what was going on.

But you’re saying spectrum[0] is not the amp of the lowest frequency, but the amp of the average frequency?

What I’m up to… is something over my head. Trying to figure out how to perfectly recreate sound samples in a format that’s very easy to manipulate.

I guess I should ask what’s the most basic way to produce a sound from the PC? I thought it was with a sine wave, but I think it’s just power over time (and the resulting frequencies), but not sure at the moment how you play power.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 5, 2019, 3:23am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/6 "2019-04-05T03:23:14Z")

</div>

> [@Aeggy](#):
>
> spectrum[0] is not the amp of the lowest frequency, but the amp of the average frequency?

no, it is a offset, but as frequencies below [1]  
are also seen es partial offset.  
even more difficult,  
if a sinus fits into a band you get one peak,  
and [0] and all other bands show 0.  
if the sample batch is out of phase with that sinus  
( what is normal ) you get a distribution ( also [0] not 0 )

think of like your sample show a cosinus instead of a sinus?  
you think FFT would not see it?  
it recreates it by give you amplitudes for several sinus what would add up to that cosinus.  
( ok, no problem, ignore it )

* * *

back to your point:  
you want create easy sound with processing sound lib  
and use the

> **[SinOsc / Libraries](https://processing.org/reference/libraries/sound/SinOsc.html)**
>
> This is a simple Sine Wave Oscillator.

good.

when you analyze a  
sound sample with a FFT and  
want recreate that you must use a INVERSE FFT  
( sound lib not show

> **[FFT / Libraries](https://processing.org/reference/libraries/sound/FFT.html)**
>
> This is a Fast Fourier Transform (FFT) analyzer. It calculates the normalized power spectrum of an audio stream the moment it is queried with the analyze() method.

but MINIM might do it:  
[http://code.compartmental.net/minim/fft\_method\_inverse.html](http://code.compartmental.net/minim/fft_method_inverse.html)  
) anyhow you can do manually

```auto
signal = ftt[0]/2 + fft[1]*sin(w)+fft[2]*sin(2*w) ....

```

is that what you are looking for?

---

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 5, 2019, 3:38am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/7 "2019-04-05T03:38:56Z")

</div>

I’ll have to do research on the use case of Inverse FFT.

[This link](https://stackoverflow.com/questions/1932490/java-generating-sound) might be useful too, no?

With regards to SinOsc, the way I’ve read it, a single sample doesn’t represent any particular frequency, it’s only when they accumulate over time that they create a frequency and tone, and so I was wondering if their’s a more basic way to output a sound than using an Oscillator. I’ve actually tried with a Sine Oscillator and had some success, though the result is limited by the draw framerate and amount of oscillators that can be used at once, so that’s why I’m looking for something more basic. I think the link above might be doing that.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 5, 2019, 4:38am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/8 "2019-04-05T04:38:32Z")

</div>

i not know what post of that topic you linked you ref to,  
but i not see how that fits to FFT.

> [@Aeggy](#):
>
> recreate sound samples

i have no idea if you can do it online with processing,  
but from theory yes, you can make FFTs from a sample sound  
and manipulate the AMPs and recreate the sound.  
a example would be a very good FILTER to disable one specific  
frequency ( noise? ) instead of a semi analog filter.

but for now start just with making sound by adding sinus of different frequencies.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 6, 2019, 10:51pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/9 "2019-04-06T22:51:53Z")

</div>

> [@Aeggy](#):
>
> Trying to figure out how to perfectly recreate sound samples in a format that’s very easy to manipulate.

By “very easy to manipulate” … what do you mean? Easy to change the amplitude, or pitch? Easy to pan in stereo? Easy to reverse / pause / speed / slow?

Digital computers are great at “perfectly recreate.” Unless there is a pseudo-random element, any algorithmic digital sound generation methods are going to produce the same results every time (for one piece of given hardware, of course). Unless I’m not understanding you, and you are talking about something like recording external audio for playback into a lossless encoding format…?

---

<div class="post-metadata">

**Author:** ![Aeggy](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Aeggy](https://discourse.processing.org/u/Aeggy)\
**Post date:** [April 7, 2019, 5:10am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/10 "2019-04-07T05:10:14Z")

</div>

I mean being able to take an audio clip, capture all its individual samples (ex. 44100 \* secs of clip), and perform any type of modulation to those samples over time. For example, if I captured a light touch on a guitar string, a medium strike, and a full-force heavy strike, I could examine how the samples between them change over time, and be able to modulate any amount between those states. For example. 2/3rds of the way to medium strike. Also it would be nice if I could pan each sample individually or each frequency instead, but I’m not sure how possible that is or if it’s supper intense on computing. Lots of question marks you see, which is why I’m trying to do this bit by bit and not overloading the forum with questions, heh.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 7, 2019, 7:00am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-on-fft-analyze-when-bands-above-2-14/9980/11 "2019-04-07T07:00:44Z")

</div>

while a tone is a sum of frequencies,  
a sound is much more: ADSR

> **[Envelope (music)](https://en.wikipedia.org/wiki/Envelope_(music))**
>
> In sound and music, an envelope describes how a sound changes over time. Envelopes may relate to elements such as amplitude (volume), filters (frequencies), or pitch. For example, a piano key, when struck and held, creates a near-immediate initial sound which gradually decreases in volume to zero. Envelope generators are common features of synthesizers, samplers, and other electronic musical instruments.
> The most common kind of envelope generator has four stages: attack, decay, sustain, and rele...

[https://processing.org/reference/libraries/sound/Env.html](https://processing.org/reference/libraries/sound/Env.html)  
or  
[http://code.compartmental.net/minim/adsr\_class\_adsr.html](http://code.compartmental.net/minim/adsr_class_adsr.html)

i only play it in PYTHON  
[http://kll.engineering-news.org/kllfusion01/articles.php?article\_id=80](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=80)  
pls. note the graphic way to edit signal and adsr with ONE mouse drag!  
but you need a real processing synthesizer specialist here
