public static float GetValueFromArray(float[] data, int index, EPaddleType paddleType) { if (index >= 0 && index < data.Length) { return(data[index]); } else { switch (paddleType) { case EPaddleType.Zero: return(0); case EPaddleType.Repeat: return(index < 0 ? data[0] : data[data.Length - 1]); case EPaddleType.Loop: int actualIndex = index; while (actualIndex < 0) { actualIndex += data.Length; } actualIndex %= data.Length; return(data[actualIndex]); default: return(0); } } }
/// <summary> /// Convolute data and filter. Result is sent to output, which must not be shorter than data. /// </summary> /// <param name="output">Array to store output. Must not be shorter than data.</param> /// <param name="data">Source data array.</param> /// <param name="filter">Filter array.</param> /// <param name="paddleType">Paddle type.</param> public static void Convolute(float[] data, float[] filter, EPaddleType paddleType, float[] output) { int filterMiddlePoint = Mathf.FloorToInt(filter.Length / 2); for (int n = 0; n < data.Length; ++n) { output[n] = 0.0f; for (int m = 0; m < filter.Length; ++m) { output[n] += MathToolBox.GetValueFromArray(data, n - filterMiddlePoint + m, paddleType) * filter[filter.Length - m - 1]; } } }