1 /*
2 MPEG Maaate: An Australian MPEG audio analysis toolkit
3 Copyright (C) 2000 Commonwealth Scientific and Industrial Research Organisation
4 (CSIRO), Australia.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <math.h>
22 #include "SOUNDfile.H"
23 #include "tools.H"
24
25 #define SECURE 1
26 //set it to 0 to remove test at the begining of functions
27
28 /************************* Other tools **********************/
29
30
31 /*------------------------ search_array --------------------*/
32
33 CSAPI_PLUGINS
search_array(double tab[],double what,int lenght)34 int search_array ( double tab[], double what, int lenght) {
35
36 int where;
37 int from = 0;
38 int to = lenght-1;
39
40 #ifdef SECURE
41 if ( tab == NULL ) {
42 cerr << "ToolsLibrary: array pointer NULL" << endl;
43 return 0;
44 }
45
46 if (lenght <= 0) {
47 cerr << "ToolsLibrary: wrong lenght" << endl;
48 }
49
50 #endif
51
52 //finding the 2 nearest values
53 while (to != (from+1)) {
54 where = from + (to-from)/2;
55 if ( what >= tab[where]) {
56 from = where;
57 } else {
58 to = where;
59 }
60 }
61
62 if ( tab[from] == what ) {
63 return from;
64 } else {
65 return to ;
66 }
67
68 }
69
70
71 /****************************** windows functions ***********************************/
72
73 /*---------------------- square_window --------------------*/
74 CSAPI_PLUGINS
square_window(unsigned int upto,int x)75 double square_window ( unsigned int upto, int x) {
76
77 if ((x >= 0) &&( x <= (int) upto)) {
78 return 1.0;
79 } else {
80 return 0.0;
81 }
82 }
83
84
85 /*---------------------- Hamming window ----------------------*/
86 CSAPI_PLUGINS
hamming_window(unsigned int upto,int x)87 double hamming_window ( unsigned int upto, int x) {
88
89 if ((x >= 0) &&( x <= (int) upto)) {
90 return 0.54 - 0.46 * cos( 2 * PI * x/upto);
91 } else {
92 return 0.0;
93 }
94 }
95
96
97 /*---------------------- Welch window ----------------------*/
98 CSAPI_PLUGINS
welch_window(unsigned int upto,int x)99 double welch_window ( unsigned int upto, int x) {
100
101 if ((x >= 0) &&( x <= (int) upto)) {
102 return 1 - pow( ((x - 0.5*upto) / (0.5 * upto)) , 2 );
103 } else {
104 return 0.0;
105 }
106 }
107
108
109 /*---------------------- Bartlett window ----------------------*/
110 CSAPI_PLUGINS
bartlett_window(unsigned int upto,int x)111 double bartlett_window ( unsigned int upto, int x) {
112
113 if ((x >= 0) &&( x <= (int) upto)) {
114 return 1 - fabs( ((x - 0.5*upto) / (0.5 * upto)) );
115 } else {
116 return 0.0;
117 }
118 }
119
120