} //ScanArrayForGridPattern() /// <summary> /// Steps through the passed array and and at each step cuts out a segment having length = numberOfCycles * cyclePeriod. /// Each segment is then reduced to length = numberOfCycles * 2. /// Then the reduced segment is passed to check for a grating pattern having period = 2 signal samples. /// Use this method when the array to be scanned will be reduced on the fly. /// </summary> /// <param name="array"></param> /// <param name="step"></param> /// <param name="numberOfCycles"></param> /// <param name="cyclePeriod">NB! MUST BE AN EVEN NUMBER!!!</param> /// <returns></returns> public static double[] ScanArrayForGratingPattern(double[] array, int step, int numberOfCycles, int cyclePeriod) { //noise reduce the array to get acoustic events double Q, oneSD; double[] noiseReducedArray = SNR.NoiseSubtractMode(array, out Q, out oneSD); double threshold = 0.01; int length = array.Length; double[] gridScore = new double[length]; int segmentLength = numberOfCycles * cyclePeriod; var output = new double[length]; for (int i = 0; i < length; i++) { if (noiseReducedArray[i] < threshold) { continue; } double[] extract = DataTools.Subarray(array, i, segmentLength); if (extract == null) { return(output); // reached end of array } //now reduce the segment double[] reducedSegment = null; if (cyclePeriod == 2) { reducedSegment = extract; } else { int halfPeriod = cyclePeriod / 2; int reducedLength = numberOfCycles * 2; reducedSegment = new double[reducedLength]; for (int x = 0; x < reducedLength; x++) { ///////two ways to reduce: (1) by average of the period or (2) by max of the period double sum = 0; for (int c = 0; c < halfPeriod; c++) { sum += extract[(x * halfPeriod) + c]; } reducedSegment[x] = sum / (double)cyclePeriod; ///////(2) //double max = -Double.MaxValue; //for (int c = 0; c < halfPeriod; c++) //{ // double value = extract[(x * halfPeriod) + c]; // if (max < value) max = value; //} //reducedSegment[x] = max; } } //DataTools.writeBarGraph(reducedSegment); double score = DetectPeriod2Grating(reducedSegment); //write score to output array for (int x = 0; x < segmentLength; x++) { if (output[i + x] < score) { output[i + x] = score; } } i += (step - 1); } return(output); } //ScanArrayForGridPattern()