changeset 613:0cd7ec2d2509

add db2pow and pow2db functions (patch #9587, patch #9589) * db2pow.m: New function. * pow2db.m: New function.
author P Sudeepam <sudeepam.pandey@gmail.com>
date Tue, 10 Apr 2018 09:24:00 -0700
parents 75d4fbd9b6ed
children 27289d5ac615
files inst/db2pow.m inst/pow2db.m
diffstat 2 files changed, 109 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/db2pow.m	Tue Apr 10 09:24:00 2018 -0700
@@ -0,0 +1,52 @@
+## Copyright (C) 2018 P Sudeepam
+##
+## 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; see the file COPYING. If not, see
+## <https://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{pow} =} db2pow (@var{db})
+## Convert dB value to its corresponding power value
+##
+## @var{db} is the decibel value and @var{pow} is the corosponding power value
+##
+## Example:
+##
+## @example
+## @group
+## db2pow([-10, 0, 10])
+## @result{} 0.10000 1.00000 10.00000
+## @end group
+## @end example
+## @seealso{pow2db}
+## @end deftypefn
+
+function pow = db2pow (db)
+
+  if (nargin != 1)
+    print_usage ();
+  endif
+
+  pow = 10.^(db/10);
+
+endfunction
+
+
+%!shared db
+%! db = [-10, 0, 10, 20, 25];
+
+%!assert (db2pow(db), [0.10000, 1.00000, 10.00000, 100.00000, 316.22777], 0.00001)
+%!assert (db2pow(db'), [0.10000; 1.00000; 10.00000; 100.00000; 316.22777], 0.00001)
+
+## Test input validation
+%!error db2pow ()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/pow2db.m	Tue Apr 10 09:24:00 2018 -0700
@@ -0,0 +1,57 @@
+## Copyright (C) 2018 P Sudeepam
+##
+## 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; see the file COPYING. If not, see
+## <https://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{db} =} pow2db (@var{pow})
+## Convert power value to its corresponding dB value
+##
+## @var{pow} is the power value and @var{db} is the corosponding decibel value
+##
+## Examples:
+##
+## @example
+## @group
+## pow2db([0, 10, 100])
+## @result{} -Inf 10 20
+## @end group
+## @end example
+## @seealso{db2pow}
+## @end deftypefn
+
+function db = pow2db (pow)
+
+  if (nargin != 1)
+    print_usage ();
+  endif
+
+  if (sum (pow < 0) != 0)
+    error("pow2db: power values must be non-negative real values");
+  endif
+
+  db = 10 .* log10 (pow);
+
+endfunction
+
+%!shared pow
+%! pow = [0, 10, 20, 60, 100];
+
+%!assert (pow2db(pow), [-Inf, 10.000, 13.010, 17.782, 20.000], 0.01)
+%!assert (pow2db(pow'), [-Inf; 10.000; 13.010; 17.782; 20.000], 0.01)
+
+## Test input validation
+%!error pow2db ()
+%!error <pow2db: power values must be non-negative real values> pow2db(-5)
+%!error <pow2db: power values must be non-negative real values> pow2db([-5 7])