private static IEnumerable <TimeInterval> GetStressfulSpansForHealthIndicator( IEnumerable <StateRecord> statesOfPerson, Func <StateRecord, float> healthIndicatorExtractor, IndicatorParameters parameters) { DateTime begin = statesOfPerson.First().CreateTime; var normalizedStates = statesOfPerson.Select(state => new PhysicalState { StateIndicator = healthIndicatorExtractor(state), Time = state.CreateTime }).ToArray(); double mean = normalizedStates.Select(state => state.StateIndicator).Average(); double stdev = StandardDeviation(normalizedStates.Select(state => state.StateIndicator)); int abnormalValuesInRow = 0; for (int i = 0; i < normalizedStates.Length; i++) { PhysicalState state = normalizedStates[i]; float indicator = state.StateIndicator; bool isAbnormalValue = indicator - mean >= parameters.SigmaCoefficient * stdev; bool isLastRecord = i == normalizedStates.Length - 1; if (isAbnormalValue && !isLastRecord) { abnormalValuesInRow++; } else if (abnormalValuesInRow > 0) { var interval = new TimeInterval { Begin = normalizedStates[i - abnormalValuesInRow - 1].Time, End = normalizedStates[i].Time }; if (interval.GetDurationInSeconds() >= parameters.MinDurationSeconds) { yield return(interval); } abnormalValuesInRow = 0; } } }