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.
Index › sonia - frequency bin values
 Pages: 1
 sonia - frequency bin values (Read 1919 times)
 ch sonia - frequency bin values May 22nd, 2006, 2:54pm   hi alldoes anyone know of a way to retrieve the frequency values of specific frequency bins in sonia? i.e. if i am using 256 bins, how do i determine the frequency band of the 139th bin? i presume it's divided logarithmically over 0 to samplingF/2 but i'm not sure how to calculate the precise band.....thanks,c
 watz Re: sonia - frequency bin values Reply #1 - May 22nd, 2006, 3:38pm   Toxi posted some illuminating clarifications about FFT results a few days ago. See that discussion for details. marius watz // unlekker // code & form // Generator.x
 ch Re: sonia - frequency bin values Reply #2 - May 22nd, 2006, 4:07pm   thanks, i saw that one (it's the only one that comes up when you search for "frequency bin"!) but it's discussing a slightly different issue. my question relates to working out the interval of the frequency values of each particular bin -- often its worked out on a logarithmic basis (i.e. the first bin would be, say 0-20Hz (i.e the first bin is 20 wide), while the top bin would be, say, 17500 to 20000Hz (i.e. the  last bin is 2500 wide); but if the bins are separated regularly then the first bin might be (20000/256) 0 to 78Hz (i.e. 78 wide) while the last one would be 19922 - 20000 Hz (i.e. 78 wide).if its logarithmic (which is the more likely case) then i'm trying to find out how to work out exactly what the interval is for each particular bin.......c
 toxi Re: sonia - frequency bin values Reply #3 - May 22nd, 2006, 8:34pm   If i remember correctly (no time to properly check right now, so please take with caution!) - the bins are divided in a linear fashion, so: Code:```binWidth = nyquistFreq / numBinsbinWidth = 22050 / 256 = 86.1328 Hz (example) ```To important thing is that the very 1st bin of the spectrum will always only be half the width of all other bins: Code:```binWidth_0 = binWidth / 2; ```That leads us then to the Matching-A-Freq-To-A-Bin equation: Code:```int binIndex = (int)( (freq-binWidth_0) / binWidth + 1 );binIndex = (int)((440-43.0664)/86.1328 + 1) = 5 ```In order to get the average level of a whole frequency band, you'd then have to get the bin indices of the band's lower and upper bounds and then compute the mean of all contributing bin values... Marius, maybe that's a nice candidate function for your helper library? Re: logarithmic scale - this is more required when trying to work out the bandwidth of filters and/or for musical analysis, since the frequency range of each consecutive octave is doubling. e.g. A0 = 440Hz, A1 = 880Hz, A2 = 1760Hz etc.Again, this is all just off the top of my hat, without any warranty... I'd very much appreciate getting any clarifications about this myself, though - so please do let us know if the above is incorrect! Cheers.
 ch Re: sonia - frequency bin values Reply #4 - May 23rd, 2006, 1:24am   hi toxithanks for the answer, all makes sense!just one thing though: if i'm not mistaken the bins are actually divided logarithmically UNLESS you use the .useEqualizer() function which according to the documentation says "Use an equalizer on the FFT signal for an even output values between low and hi frequencies." -- which i took to mean that if LiveInput.useEqualizer(true) then the bins are divided linearly; otherwise logarithmically... and i may be wrong but i think normal fft calculations by default are usually output in logarithmically sized bins, at least they are in most other environments i've used them in...it would be great if someone had some definitive info on this!thanks again....p.s. i should say the reason i started asking this question is that i tried it using an external signal sine wave signal generator and as i sweeped it up the frequencies it seemed to indicate that the bins were not linearly sized... but again i may be wrong....
 ch Re: sonia - frequency bin values Reply #5 - Jun 5th, 2006, 9:34am   *bumpety bump*sorry to pester, but really no one knows the answer this...?
 liminal Re: sonia - frequency bin values Reply #6 - Jun 5th, 2006, 3:55pm   I ran a little test and found the bins to be spaced linearly. I did not verify the specific frequency ranges of the bins.
 ch Re: sonia - frequency bin values Reply #7 - Jun 5th, 2006, 7:44pm   strange... my test definitely suggested the opposite.out of interest were you using .useEqualizer(true)?
 liminal Re: sonia - frequency bin values Reply #8 - Jun 7th, 2006, 5:02am   Here's a test I wrote. I get a linear relationship with the equalizer off (assuming I'm interpreting the results correctly!). With the equalizer on, I get weird results.The test just generates a sin wave and doubles the frequency every second. Code:```import pitaru.sonia_v2_9.*;Sample mySample; float[] data; void setup() { size(512, 512); Sonia.start(this); LiveInput.start(256);// LiveInput.useEqualizer(true); mySample = new Sample(1024); // Create an empty sample with 1024 frames. data = new float[1024]; // create an array with as many frames as as sample. //populate the array with sample data, a sine wave in this case fillData(); // LiveInput.useEnvelope(true, 1); mySample.write(data); // write the data from the 'data' array into the sample mySample.repeat(); // loop the sample mySample.connectLiveInput(true); framerate(1);}int freq = 10;void draw() { background(255); float level = LiveInput.getLevel(); LiveInput.getSpectrum(); float maxSpec = Float.NEGATIVE_INFINITY; int maxBin = -1; for (int i=0; i < LiveInput.spectrum.length; i++) { if (LiveInput.spectrum[i] > maxSpec) { maxBin = i; maxSpec = LiveInput.spectrum[i]; } } println(maxBin); float scale = (height - 20) / maxSpec; for (int i=0; i < LiveInput.spectrum.length; i++) { line(i*2, height, i*2, height - LiveInput.spectrum[i] * scale); } freq *= 2; fillData(); // LiveInput.useEnvelope(true, 1); mySample.write(data); // write the data from the 'data' array into the sample mySample.repeat(); // loop the sample mySample.connectLiveInput(true);}//populate the array with sample data, a sine wave in this case private void fillData() { float oneCycle = TWO_PI/1024; for(int i = 0; i < 1024; i++){ data[i] = sin(i*oneCycle*freq); } } public void stop(){ Sonia.stop(); super.stop(); } ```
 toxi Re: sonia - frequency bin values Reply #9 - Jun 12th, 2006, 8:07pm   That looks linear to me too. Also the useEqualizer(true) setting only affects the individual gain of each bin, not the bin widths or their distribution.The equalizer function is a logarithmic curve multiplied with the raw result values after the FFT is completed. Sorry to be so brief...
 Pages: 1