Library of Assembled Shared Sources
db.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_DB_INL
44#define LASS_GUARDIAN_OF_INCLUSION_NUM_DB_INL
45
46#include "num_traits.h"
47
48namespace lass
49{
50 namespace num
51 {
52 #define LASS_NUM_REFERENCE_PRESSURE_P0 20.e-6
53 #define LASS_NUM_REFERENCE_INTENSITY_I0 1.e-12
54 #define LASS_NUM_REFERENCE_POWER_W0 1.e-12
55
56 template< typename T > T p2dB( const T& pressure )
57 {
58 if (pressure < T(0))
59 {
60 LASS_THROW( "Negative argument '" << pressure << "'" );
61 }
62 if (isNaN(pressure))
63 {
64 return pressure; // nan
65 }
66 return T(LASS_NUM_DB_20_OVER_LN10) * num::log(pressure / T(LASS_NUM_REFERENCE_PRESSURE_P0));
67 }
68
69 template< typename T > T W2dB( const T& power )
70 {
71 if (power < T(0))
72 {
73 LASS_THROW( "Negative argument '" << power << "'" );
74 }
75 if (isNaN(power))
76 {
77 return power; // nan
78 }
79 return T(LASS_NUM_DB_10_OVER_LN10) * num::log(power / T(LASS_NUM_REFERENCE_POWER_W0));
80 }
81
82 template< typename T > T I2dB( const T& intensity )
83 {
84 if (intensity < T(0))
85 {
86 LASS_THROW( "Negative argument '" << intensity << "'" );
87 }
88 if (isNaN(intensity))
89 {
90 return intensity; // nan
91 }
92 return T(LASS_NUM_DB_10_OVER_LN10) * num::log(intensity / T(LASS_NUM_REFERENCE_INTENSITY_I0));
93 }
94
95 template< typename T > T dB2p( const T& decibels )
96 {
97 return decibels == -NumTraits<T>::infinity ?
98 NumTraits<T>::zero :
99 T(LASS_NUM_REFERENCE_PRESSURE_P0) * num::exp(decibels * T(LASS_NUM_DB_LN10_OVER_20));
100 }
101
102 template< typename T > T dB2W( const T& decibels )
103 {
104 return decibels == -NumTraits<T>::infinity ?
105 NumTraits<T>::zero :
106 T(LASS_NUM_REFERENCE_POWER_W0) * num::exp(decibels * T(LASS_NUM_DB_LN10_OVER_10));
107 }
108
109 template< typename T > T dB2I( const T& decibels )
110 {
111 return decibels == -NumTraits<T>::infinity ?
112 NumTraits<T>::zero :
113 T(LASS_NUM_REFERENCE_INTENSITY_I0) * num::exp(decibels * T(LASS_NUM_DB_LN10_OVER_10));
114 }
115 }
116}
117
118#endif
119
120// EOF
T dB2W(const T &iValue)
Converts decibels into a power.
Definition db.inl:102
T dB2p(const T &iValue)
Converts decibels into a pressure .
Definition db.inl:95
T dB2I(const T &iValue)
Converts decibels into an intensity.
Definition db.inl:109
T W2dB(const T &iValue)
Converts a absolute acoustical power into decibels.
Definition db.inl:69
T I2dB(const T &iValue)
Converts an intensity into decibels.
Definition db.inl:82
T p2dB(const T &iValue)
Converts an absolute acoustical pressure into decibels.
Definition db.inl:56
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53