简体   繁体   中英

How can I average a subset of an array and store the result in another array?

I have a C array fftArray[64] that contains values that I want averaged and placed into another array frequencyBar[8] . Getting the average of the entire array would be easy enough using a for statement.

int average, sum = 0;
for (i = 0; i < 64; i++)
{
     sum += fftArray[i];
}
average = sum/64;

But I just can't seem to figure out how to get the average from fftArray[0] through fftArray[8] and store this in frequencyBar[0] , the average of fftArray[9] through fftArray[16] and store this in frequencyBar[1] , etc. Can anyone help me out with this? Thanks

This looks like a homework assignment, so, rather than give you the outright answer, I'd rather just point you in the right direction...

use a nested loop (one inside the other). One loop cycles 0-7, the other one 0 - 63. Use the smaller one to populate your sliced averages.

or better yet use the % operator to see when you've gone through 8 elements and do an average of your total, then reset the total for the next set. Then you'll have learned how to use the % operator too! :)

[EDIT] ok, if not homework then something like this... I haven't written C in 5 years, so treat this as pseudo code:

//assuming you have a fftArray[64] with data, as per your question
int i,sum,avCounter,total;
int averages[8]; 
for(i=0 , avCounter=0, total=0 ; i<64; ){
    total += fftArray[i];
    if(++i % 8 == 0){   //%gives you the remainder which will be 0 every 8th cycle
      averages[avCounter++] = total / 8
      total = 0;  //reset for next cycle
    }
}

I think this will work better than a nested loop... but I'm not sure since % is division which is more processor heavy than addition... however... I doubt anyone would notice :)

int i, j;
for (i = 0; i < 8; i++) {
    int sum = 0;
    for (j = 0; j < 8; j++) {
        sum += fftArray[ 8*i + j ];
    }
    frequencyBar[i] = sum / 8;
}

Bonus exercise: Optimize this code for speed on your chosen platform.

TF,

DISCLAIMER: This code is just off the top of my head... it hasn't even been compiled, let alone tested.

// returns the average of array[first..last] inclusive.
int average(int[] array, int first, int last) {
    int sum = 0; 
    for (i = first; i <= last; i++)
        sum += array[i];
    return sum / (last - first + 1); // not sure about the +1
}

Then what you'd do is loop through the indexes of your frequencyBar array [0..7], setting frequencyBar[i] = average(array, first, last); ... the tricky bit is calculating the first and last indexes... try i*8 and (i+1)*8 respectively... that may not be exactly right, but it'll be close ;-)

Cheers. Keith.


EDIT: Bored... waiting for my test results to come back. No news is good news, right? ;-)

It turns out that passing the length is a fair bit simpler than passing the last index.

#include <stdio.h>

int sum(int array[], int first, int length) {
    int sum = 0;
    for (int i = first; i < first+length; i++)
        sum += array[i];
    return sum;
} 

double average(int array[], int first, int length) {
    double total = sum(array, first, length);
#ifdef DEBUG
    printf("DEBUG: [%2d..%2d] %d", first, first+length-1, array[first]);
    for (int i = first+1; i < first+length; i++)
        printf(" + %d", array[i]);
    printf(" = %d / %d = %f\n", (int)total, length, total/length);
#endif
    return total / length;
} 

int main(int argc, char* argv[]) {
    int array[] = {             // average
        1, 2, 3, 4, 5, 1, 2, 3, // 2.625 
        4, 5, 1, 2, 3, 4, 5, 1, // 3.125 
        2, 3, 4, 5, 1, 2, 3, 4, // 3
        5, 1, 2, 3, 4, 5, 1, 2, // 2.875
        3, 4, 5, 1, 2, 3, 4, 5, // 3.375
        1, 2, 3, 4, 5, 1, 2, 3, // 2.625
        4, 5, 1, 2, 3, 4, 5, 1, // 3.125
        2, 3, 4, 5, 1, 2, 3, 4  // 3
    };
    double frequency[8];
    for (int i = 0; i < 8; i++)
        frequency[i] = average(array, i*8, 8);

    for (int i = 0; i < 8; i++)
        printf("%f ", frequency[i]);
    printf("\n");
}

如果fftArray价值fftArray注意你的sum不会四处fftArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM