0

ok so i'm not exactly sure how to describe this problem as i'm new to Audio analysis as a whole so i'm going to explain it step by step.

  1. So i have this algorithm here written in csharp
// Put into groups
for (int i = 0; i < onsets.Count-1; i++){
    for (float i2 = 1f; i2 < range+1; i2++){
        if (onsets[i] >= rangemult*(i2-1f) && onsets[i] <= rangemult*i2){
           sorted.Add(((rangemult*i2)*2f)*10f);
        }
    }
}
  1. Here's what it does:

onsets --> a List containing the onsets found (excluding any 0's)

range --> Song Sample Depth (1024)

rangemult --> Is the maximum value in the list of onsets (so onsets.Max()) divided by the range so (max DIV range)

sorted --> list the sorted onsets are added to

(rangemult*i2)*2f)*10f --> i do this in order to get the value to be a whole number (which doesn't happen most of the time but it's really just for a sake of neatness - so for the most part it can be ignored)

in principle this algorithm would convert an array (0.1, 0.2, 0.4, 0.45, 0.5) with

range = 10 to 2, 4, 8, 9, 10

range = 20 to 4, 8, 16, 18, 20

etc...

  1. so given this why is it that when 1024 is set as range the maximum value in sorted is always 20?

Additional info:

  • i'm using a lomont FFT
  • 1024 is constant throughout the onset detection algo
  • treat onsets.max() as always 1 (i'm yet to find an instance where onsets.Max() is not 1)

I greatly appreciate it if you can provide even the smallest hint as to what's wrong - if there is not enough information here or the information is uneeded please inform me as i'm still new so i have hard time trying to explain my questions in an accurate manner - thankyou for understanding :)

  • 1
    1. So `onsets` is actually a `List` and not an array, since you reference `onsets.Count`? 2. you say `onsets.Max()` is `1` but your example has a max of 0.5??? 3. I guess `sorted` is `List` since the `Add` argument is a `double`? 4. Your code doesn't work with your examples, why should it work 1024? – NetMage Dec 16 '21 at 21:28
  • 1
    Consider: `range` is 1024 then `rangemult` is `1/1024` and in `sorted.Add` you have `rangemult*i2*2f*10` => `rangemult*i2*20` and the highest `i2` is `range` so you have `rangemult*range*20` => `1/1024*1024*20` => `1 * 20` => `20`. – NetMage Dec 16 '21 at 21:32
  • oh christ im so sorry dude - i rewrote it once and forgot to change my words cus' i keep confusing array with list - furthermore i see what you mean it doesn't make sense - sorry dude lemme edit it a sec – Bobbymcbobface Dec 16 '21 at 21:33
  • holy moly you managed to get that from what i wrote! - dude tysm i understand what i've done wrong now – Bobbymcbobface Dec 16 '21 at 21:35

1 Answers1

1

Given your apparent purpose, all you need is a simple LINQ statement:

var rangemult = range/onsets.Max();
var sorted = onsets.Select(onset => onset*rangemult).ToList();

However, this doesn't round the result to integers. You can add any desired rounding such as Math.Floor or Math.Round or Math.Ceil in the Select:

var sorted = onsets.Select(onset => Math.Round(onset*rangemult)).ToList();
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • Hi so i think you misunderstood me - i need to have the onsets sorted into `range` different groups so bassically it finds the nearest whole number to the onset then adds that to `sorted` so if `range` was `10` and the highest number in onsets was `0.5` (even though subsequently it's not) `0.1 = 2, 0.2 = 4, 0.3 = 6, 0.5 = 10` etc... unless of course i'm missing something out. – Bobbymcbobface Dec 16 '21 at 21:58
  • however the linq expression has helped a bit so thankyou :) – Bobbymcbobface Dec 16 '21 at 22:00
  • Yep i was missing something - thankyou kind stranger for putting up with me being a muppet for 20 minutes flat - for future users note that my question is completely invalid since like NetMage said "you say onsets.Max() is 1 but your example has a max of 0.5???" which i should've read a bit more as that would of made me realise sooner that the onset detection system i was using was messing up and for some reason adding a 1 to the end of the array - i'm still yet to find out why it did this - but removing it did the trick – Bobbymcbobface Dec 16 '21 at 22:49