/
common.cpp
129 lines (114 loc) · 3.23 KB
/
common.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//---------------------------------------------------------------------------
#include <math.h>
#include <sstream>
#include "common.h"
#pragma hdrstop
const int SND_LEN = 12000;
const int SND_LEN2 = 48000;
const int SND_FD = 12000; //12000
const int SND_FD2 = 48000;
const int DF_LEN = 1000; //óñðåäíåíèå ÷àñòîòû
//---------------------------------------------------------------------------
std::string getAvgMediumLevel(float * x, double &Usr)
{
std::string res;
float uu=0;
for(int i=0; i<SND_LEN; i++)uu=uu+fabs(x[i]);
uu=uu/SND_LEN;
if(Usr == 0) Usr=uu;
Usr=0.7*(Usr)+0.3*uu;
if(Usr>0)
{
double v = 20*log10(Usr);
std::ostringstream sstream;
sstream << v;
std::string str_v = sstream.str();
res = "Volume " + str_v + " db";
}
return res;
};
void convertFrom48000_toDown(float* from, float* to, TIIRBPF2* filter1, TIIRBPF2* filter11, TIIRBPF2* filter111, double& dph2)
{
const double twoPi = 2*M_PI;
double ph2=M_PI/256.0;
for(int i=0; i<SND_LEN2; i++)
{
to[i/4]=(filter111->filter(filter11->filter(filter1->filter(from[i])))) * cos(ph2);
ph2=ph2+dph2; if(ph2>twoPi)ph2=ph2-twoPi;
}
};
int nb(float *x, int nb_level)
{
int cntClip=0;
for(int i=0; i<SND_LEN; i++)
if(fabs(x[i])>nb_level) {x[i]=0;cntClip++;}
return cntClip;
};
static unsigned long CNT_Z=0; //ñ÷åò÷èê ïåðåõîäîâ ÷åðåç íîëü
static unsigned long CNT_Z_ALL=0;//c÷åò÷èê îáðàáîòàííûõ îòñ÷åòîâ
static int cnt_F=0;
int freqCounter(float *x, float* xf, float* xf2, double *df0, double &deltaF, double &d_deltaF, TIIRBPF2 *BPF_F, TIIRBPF2 *BPF_F1, TIIRBPF2 *BPF_F2)
{
int Uf(0);
int Uf2(0);
int SNRF(0);
for(int k0=0; k0<SND_LEN-SND_LEN/10; k0+=SND_LEN/10)
{
float uuf=0, uuf2=0;
for(int i=0; i<SND_LEN/10; i++)
uuf=uuf+fabs(xf[i+k0]);
Uf=uuf;//0.7*Uf+0.3*(10*uuf/SND_LEN);
for(int i=0; i<SND_LEN/10; i++)
uuf2=uuf2+fabs(xf2[i+k0]);
int Uf2=uuf2;//0.7*Uf2+0.3*(10*uuf2/SND_LEN);
SNRF=0;
if(Uf<Uf2)
SNRF=20.0*log10(Uf/(Uf2-Uf));
float ddf=0;
if(SNRF > 6.0)
{
//ñ÷åò ïåðåõîäîâ ÷åðåç íîëü
for(int i=0; i<SND_LEN/10; i++)
{
if(i+k0+1>=SND_LEN) continue;
if(xf[i+k0]<0 && xf[i+k0+1]>=0)
CNT_Z++;
if(xf[i+k0]>=0 && xf[i+k0+1]<0)
CNT_Z++;
CNT_Z_ALL++;
}
if(1.0*CNT_Z_ALL/SND_FD>=2.0)
{
ddf=(1.0*CNT_Z/2.0/CNT_Z_ALL)-BPF_F->getF0();
ddf=ddf*SND_FD;
if(1.0*CNT_Z_ALL/SND_FD>=10.0)
{
CNT_Z_ALL=CNT_Z_ALL/2;
CNT_Z=CNT_Z/2;
}
df0[cnt_F]=ddf;
cnt_F++; cnt_F=cnt_F%DF_LEN;
int cf=1;
float df=0;
for(int i=0; i<DF_LEN; i++)
{
if(df0[i]!=0 && fabs(df0[i])<20.0)
{
cf++;
df+=df0[i];
}
}
deltaF=df/cf;
d_deltaF=0;
for(int i=0; i<DF_LEN; i++)
if(df0[i]!=0 && fabs(df0[i])<20.0)
d_deltaF+=(df0[i]-deltaF)*(df0[i]-deltaF);
d_deltaF=3.0*sqrt(d_deltaF)/cf;
if(cf<10)
d_deltaF=10.0;
}
}
}
return 0;
};
#pragma package(smart_init)