# HG changeset patch
# User Andreas Weber <andy.weber.aw@gmail.com>
# Date 1427995282 -7200
#      Thu Apr 02 19:21:22 2015 +0200
# Node ID ec6d6ab2ac6f9eed1a807933505f820ef68b1a79
# Parent  84c97aa9186f573406884a7d42c04c8111a99d46
Two new functions "rms" and "peak2rms"

* inst/peak2rms.m: New function for peak to rms ratio
* inst/rms.m: New Function root-mean-square

diff -r 84c97aa9186f -r ec6d6ab2ac6f NEWS
--- a/NEWS	Sat Mar 28 16:57:55 2015 -0400
+++ b/NEWS	Thu Apr 02 19:21:22 2015 +0200
@@ -13,6 +13,10 @@
       buttord    cheb2ord
       cheb1ord   ellipord
 
+ ** The following functions are new:
+
+      rms        peak2rms
+
 ===============================================================================
 signal-1.3.0   Release Date: 2014-01-26   Release Manager: Mike Miller
 ===============================================================================
diff -r 84c97aa9186f -r ec6d6ab2ac6f inst/peak2rms.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/peak2rms.m	Thu Apr 02 19:21:22 2015 +0200
@@ -0,0 +1,42 @@
+## Copyright (C) 2015 Andreas Weber <octave@tech-chat.de>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{y} =} peak2rms (@var{x})
+## @deftypefnx {Function File} {@var{y} =} peak2rms (@var{x}, @var{dim})
+##
+## Return the ratio of the largest absolute value to the root-mean-square
+## (RMS) value of @var{x}.
+##
+## For matrix arguments, return a row vector containing the ratio of each column.
+##
+## If the optional argument @var{dim} is given, operate along this dimension.
+## @seealso{rms}
+## @end deftypefn
+
+function y = peak2rms (x, dim)
+
+  if (nargin == 1)
+    y = max (abs (x)) ./ sqrt (meansq (x));
+  elseif (nargin == 2)
+    y = max (abs (x), [], dim) ./ sqrt (meansq (x, dim));
+  else
+    print_usage ();
+  endif
+
+endfunction
+
+%!assert (peak2rms ([-2 3; 4 -2]), [4/sqrt(10), 3/sqrt((9+4)/2)])
+%!assert (peak2rms ([-2 3; 4 -2], 2), [3/sqrt((9+4)/2); 4/sqrt(10)])
diff -r 84c97aa9186f -r ec6d6ab2ac6f inst/rms.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/rms.m	Thu Apr 02 19:21:22 2015 +0200
@@ -0,0 +1,53 @@
+## Copyright (C) 2015 Andreas Weber <octave@tech-chat.de>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{y} =} rms (@var{x})
+## @deftypefnx {Function File} {@var{y} =} rms (@var{x}, @var{dim})
+##
+## Return the root-mean-square (RMS) of @var{x}.
+## @tex
+## $$
+## {\rm rms} (x) = {\sqrt{\sum_{i=1}^N {x_i}^2 \over N}}
+## $$
+## @end tex
+## @ifnottex
+##
+## @example
+## @group
+## rms (x) = SQRT (1/N SUM_i x(i)^2)
+## @end group
+## @end example
+##
+## @end ifnottex
+## For matrix arguments, return a row vector containing the root mean square
+## of each column.
+##
+## If the optional argument @var{dim} is given, operate along this dimension.
+## @seealso{peak2rms}
+## @end deftypefn
+
+function y = rms (varargin)
+
+  if (nargin != 1 && nargin != 2)
+    print_usage ();
+  endif
+
+  y = sqrt (meansq (varargin{:}));
+
+endfunction
+
+%!assert (rms ([1 2 -1]), sqrt (2))
+%!assert (rms ([1 2 -1]'), sqrt (2))
