From a sensor, find the beat

Hi, i want to find a threshold for a heart rate sensor. Above that threshold, it will count 1 beat and wait 1/2 interbeat, then will try to find again a value above that threshold.

To define that threshold, i want to build a function based on statistics.
My idea is to monitor the value of my sensor, store a number of them in an array, and then take the mean of the first 3rd quartile.

int findThreshold();

{

int sensePin = 13;
long storageArray[200];

for(int i = 0 ; i<=199 ; i++)
{
val = analogRead(sensePin); // reading the value on the pin
storageArray[i] = val; //store it into the array 
}

// once the array is completed
//something like

findTheMean of the firtquartile (storageArray) ;

}

Thanks a lot for you ideas and answers !!