get sum of value of array?

edited August 2014 in How To...

i got fftArray with many slots, is there a way to get sum values of all slot values at once? like fftArray.getAllValueSumSomething???

Tagged:

Answers

  • No.

    You have to loop over each value and add it to a variable.
    Now, you can make a function out of this functionality, so you get your one-liner...

  • edited August 2014

    Dunno what an fftArray is, but both IntList & FloatList got a built-in sum() method:

    // forum.processing.org/two/discussion/6768/
    // get-sum-of-value-of-array
    
    final IntList nums = IntList.fromRange(-10, 20);
    println(nums);
    println("\nSum = " + nums.sum() + "\n");
    
    final FloatList fracs = new FloatList(new float[] {
      PI, TAU, EPSILON
    }
    );
    println(fracs);
    println("\nSum = " + fracs.sum() + "\n");
    
    exit();
    

    But if your class container doesn't have such method, you gotta implement it yourself, like @PhiLho said!

  • edited August 2014

    hi, i am getting signal frequency spectrum of audio via array of osc messages.

    i get many slots with incoming values for each frequency band.

    how to filter results, lf i am interested in only 500 slots from 1024?

    then i want to sum values of those 500 slots to one variable x, so i can scale it as i wish.

    in the context of audio that means to get specific spectrum regions i am interested to visualize, not each frequency band.

    full code is here: http://sccode.org/1-4Ty

    can anyone help me with example please?

  • edited August 2014

    How to filter results, lf i am interested in only 500 slots from 1024?

    Make a custom sum method! Here's a quick 1 I come up w/ called rangeSum().
    There are 3 parameters: Container, start index, end index:

    // forum.processing.org/two/discussion/6768/
    // get-sum-of-value-of-array
    
    final IntList nums = IntList.fromRange(100);
    
    void setup() {
      println(nums);
    
      int idx = 30, qty = 50, sum = rangeSum(nums, idx, idx+qty);
      float avr = (float) sum/qty;
    
      println("\nSum: " + sum + "\t\tAverage: " + avr);
      exit();
    }
    
    static final int rangeSum(IntList iList, int start, int end) {
      int sum = 0, arr[] = iList.values();
    
      start = abs(start);
      end = abs(end);
      if (start >= end | end > arr.length)  return MIN_INT;
    
      for ( int i = start; i != end; sum += arr[i++] );
      return sum;
    }
    
  • edited August 2014

    A FloatList version too: :bz

    // forum.processing.org/two/discussion/6768/
    // get-sum-of-value-of-array
    
    static final int NUM = 100;
    final FloatList nums = new FloatList(NUM);
    
    void setup() {
      for ( int i = 0; i++ != NUM;
        nums.append(random(-EPSILON, EPSILON)) );
    
      println(nums);
    
      int idx = 30, qty = 50;
      float sum = rangeSum(nums, idx, idx+qty), avr = sum/qty;
    
      println("\nSum: " + sum + "\t\tAverage: " + avr);
      exit();
    }
    
    static final float rangeSum(FloatList iList, int start, int end) {
      float sum = 0, arr[] = iList.values();
    
      start = abs(start);
      end = abs(end);
      if (start >= end | end > arr.length)  return MIN_FLOAT;
    
      for ( int i = start; i != end; sum += arr[i++] );
      return sum;
    }
    
Sign In or Register to comment.