# Finding average time from an array based on peak signal strength

Hello all.

I have a project in which I will be recording a signal as well as time values into a 2 dimensional array, and then I need to calculate a number based on those values. The array is such that element(#)(0) stores the signal value, and element(#)(1) stores the time data.

I need to find the peaks in the data, and calculate the average time in between those signals. I have no code to show, because I am not sure how to do it.

My gut reaction would be to say loop through the array, and if the signal in the current position is higher than the position to the left and to the right, it must be a maximum, and then copy the time data to a new single dimension array.

``````for (int i = 1; i < sizeof(array); i++){

if (array[i] >= array[i-1] && array[i] >= array[i+1]){

newArray[i] = array[i]

}
}
``````

From there, (since the average is the sum over the count) loop through the new array and add up all the values. But how can I get the count of the array? I know there’s sizeof([array]), but does that give you the declared length, or only the populated entries?

If I was doing this in SQL or another scripting language, this would be easy, but my C skills are not where my SQL skills are.

sizeof gives you the declared size in bytes.
You can calculate the declared number of elements with sizeof(array)/sizeof(array). But you would have to have a variable which keeps the number of valid entries. Just increment it as you fill the array.

• and don't let it go over the declared number of entries.

Pete

You should have 2 additional variables instead, 1 for the total sum, and 1 for the count. Then whenever you find the peak, add the time to the total sum and increase the count by 1.

But how can I get the count of the array?

As you are adding elements to it, keep track of how many you added in a variable.

P.S. your method won't necessarily find what you consider to be the true peaks in the data. If there's any noise on the signal, your method will find lots of apparent maxima.

Consider this sequence which has a clear maximum of 8.

``````0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0
^
``````

``````0,3,2,5,4,7,6,7,8,7,8,5,6,3,4,1,0
^   ^   ^     ^   ^   ^   ^
``````

Pete

Google "peak search algorithm" for lots of ideas. It is an important problem, and many people have thought hard about how to solve it. Take advantage of their efforts.

And use struct datatype to better organize your data:

``````struct data {
int strength;
int time; // in seconds
}
``````

And you can have an array of the struct like this:

``````data myData[SIZE];
``````

Populate data like this:

``````myData.strength = 10;
myData.time = 0;
``````

Thank you everyone for your replies. A counter that increments every time I add an entry is the easiest way to do it. As for the method and quality of peak detection, this is part of an autocorrelation function. The signal that I'll be analyzing will be several volts large, and autocorrelation creates extremely distinct peaks (much greater than the noise), so I think that the peaks will be easily distinguished.