5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * power_meter.c
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2003 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: power_meter.c,v 1.18 2006/11/19 14:07:25 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 #ifdef HAVE_CONFIG_H
|
|
31 #include <config.h>
|
|
32 #endif
|
|
33
|
|
34 #include <inttypes.h>
|
|
35 #include <stdlib.h>
|
|
36 #include <stdio.h>
|
|
37 #include <fcntl.h>
|
|
38 #include <string.h>
|
|
39 #include <float.h>
|
|
40 #if defined(HAVE_TGMATH_H)
|
|
41 #include <tgmath.h>
|
|
42 #endif
|
|
43 #if defined(HAVE_MATH_H)
|
|
44 #include <math.h>
|
|
45 #endif
|
|
46 #include <assert.h>
|
|
47
|
|
48 #include "spandsp/telephony.h"
|
|
49 #include "spandsp/power_meter.h"
|
|
50
|
|
51 power_meter_t *power_meter_init(power_meter_t *s, int shift)
|
|
52 {
|
|
53 if (s == NULL)
|
|
54 return NULL;
|
|
55 s->shift = shift;
|
|
56 s->reading = 0;
|
|
57 return s;
|
|
58 }
|
|
59 /*- End of function --------------------------------------------------------*/
|
|
60
|
|
61 power_meter_t *power_meter_damping(power_meter_t *s, int shift)
|
|
62 {
|
|
63 s->shift = shift;
|
|
64 return s;
|
|
65 }
|
|
66 /*- End of function --------------------------------------------------------*/
|
|
67
|
|
68 int32_t power_meter_update(power_meter_t *s, int16_t amp)
|
|
69 {
|
|
70 s->reading += ((amp*amp - s->reading) >> s->shift);
|
|
71 return s->reading;
|
|
72 }
|
|
73 /*- End of function --------------------------------------------------------*/
|
|
74
|
|
75 int32_t power_meter_level_dbm0(float level)
|
|
76 {
|
|
77 float l;
|
|
78
|
|
79 level -= DBM0_MAX_POWER;
|
|
80 if (level > 0.0)
|
|
81 level = 0.0;
|
|
82 l = powf(10.0f, level/10.0f)*(32767.0f*32767.0f);
|
|
83 return (int32_t) l;
|
|
84 }
|
|
85 /*- End of function --------------------------------------------------------*/
|
|
86
|
|
87 int32_t power_meter_level_dbov(float level)
|
|
88 {
|
|
89 float l;
|
|
90
|
|
91 if (level > 0.0)
|
|
92 level = 0.0;
|
|
93 l = powf(10.0f, level/10.0f)*(32767.0f*32767.0f);
|
|
94 return (int32_t) l;
|
|
95 }
|
|
96 /*- End of function --------------------------------------------------------*/
|
|
97
|
|
98 float power_meter_dbm0(power_meter_t *s)
|
|
99 {
|
|
100 if (s->reading <= 0)
|
|
101 return FLT_MIN;
|
|
102 /* This is based on A-law, but u-law is only 0.03dB different, so don't worry. */
|
|
103 return log10f((float) s->reading/(32767.0f*32767.0f))*10.0f + DBM0_MAX_POWER;
|
|
104 }
|
|
105 /*- End of function --------------------------------------------------------*/
|
|
106
|
|
107 float power_meter_dbov(power_meter_t *s)
|
|
108 {
|
|
109 if (s->reading <= 0)
|
|
110 return FLT_MIN;
|
|
111 return log10f((float) s->reading/(32767.0f*32767.0f))*10.0f;
|
|
112 }
|
|
113 /*- End of function --------------------------------------------------------*/
|
|
114 /*- End of file ------------------------------------------------------------*/
|