When dealing with both positive and negative numbers, especially when dealing with a repetitive signal like a sine wave. The mean I described in the previous article may not be that useful at all because the negative values cancel out the positives. In some cases the average power of a signal is more gives you more information, in those cases you can use the **Root Mean Square** (also called rms or quadratic mean).

**Root Mean Square**takes the squares of all values and divides them by the number of values. And then taking the square root of that number.

In the code sample I will show the difference by computing the mean and the root mean square of a set of data: -1,0,1,0,-1,0,1. Because of the negatives canceling the positives, the mean will be 0. If you were more interested in the average magnitude of this signal you would use the root mean square and get an rms value of 0.76.

using System; using System.Collections.Generic; namespace SampleApp { internal static class Program { private static void Main() { List<double> data = new List<double> {-1,0,1,0,-1,0,1}; double mean = data.Mean(); double rms = data.RootMeanSquare(); Console.WriteLine("Mean: {0}, RootMeanSquare: {1}", mean, rms); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } public static class MyListExtensions { public static double Mean(this List<double> values) { return values.Count == 0 ? 0 : values.Mean(0, values.Count); } private static double Mean(this IList<double> values, int start, int end) { double s = 0; for (int i = start; i < end; i++) { s += values[i]; } return s / (end - start); } public static double RootMeanSquare(this List<double> values) { return values.Count == 0 ? 0 : values.RootMeanSquare(0, values.Count); } private static double RootMeanSquare(this IList<double> values, int start, int end) { double s = 0; int i; for (i = start; i < end; i++) { s += values[i] * values[i]; } return Math.Sqrt(s / (end - start)); } } }