-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecibel.c
executable file
·89 lines (72 loc) · 2.22 KB
/
decibel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright (C) 2016 Metropolitan Airports Commission
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/geo_decls.h"
#include "utils/formatting.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
typedef float8 decibel_t;
#define DecibelGetDatum(x) Float8GetDatum(x)
#define DatumGetDecibel(x) DatumGetFloat8(x)
#define PG_GETARG_DECIBEL(n) DatumGetDecibel(PG_GETARG_DATUM(n))
#define PG_RETURN_DECIBEL(x) return DecibelGetDatum(x)
Datum decibelpascal(PG_FUNCTION_ARGS);
Datum pascaldecibel(PG_FUNCTION_ARGS);
Datum pascals(PG_FUNCTION_ARGS);
Datum decibel_in(PG_FUNCTION_ARGS);
Datum decibel_out(PG_FUNCTION_ARGS);
Datum decibel_sum(PG_FUNCTION_ARGS);
/* by value */
PG_FUNCTION_INFO_V1(decibelpascal);
Datum
decibelpascal(PG_FUNCTION_ARGS)
{
float8 arg = PG_GETARG_FLOAT8(0);
PG_RETURN_FLOAT8( pow( 10, arg / 10.0 ));
}
PG_FUNCTION_INFO_V1(pascaldecibel);
Datum
pascaldecibel(PG_FUNCTION_ARGS)
{
float8 arg = PG_GETARG_FLOAT8(0);
PG_RETURN_FLOAT8( 10 * log10(arg) );
}
PG_FUNCTION_INFO_V1(pascals);
Datum
pascals(PG_FUNCTION_ARGS)
{
float8 arg = PG_GETARG_DECIBEL(0);
PG_RETURN_FLOAT8( arg );
}
PG_FUNCTION_INFO_V1(decibel_in);
Datum
decibel_in(PG_FUNCTION_ARGS)
{
char *val_str = PG_GETARG_CSTRING(0);
float8 inval = atof( val_str );
PG_RETURN_DECIBEL( pow( 10, inval / 10.0) );
}
PG_FUNCTION_INFO_V1(decibel_out);
Datum
decibel_out(PG_FUNCTION_ARGS)
{
float8 val = 10 * log10( PG_GETARG_DECIBEL(0) );
char* outval = (char*)malloc(255);
sprintf(outval, "%.*lG", 15, val );
PG_RETURN_CSTRING(outval);
}