aboutsummaryrefslogtreecommitdiffstats
path: root/src/transient_detector.cpp
blob: fb46aad40e4bf14154e20c3973b9f0a6c880918f (plain) (blame)
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
#include "transient_detector.h"
#include <stdlib.h>

namespace NAtracDEnc {

static double calculateRMS(const double* in, uint32_t n) {
    double s = 0;
    for (uint32_t i = 0; i < n; i++) {
        s += in[i] * in[i];
    }
    s /= n;
    return sqrt(s);
}

void TTransientDetector::HPFilter(const double* in, double* out) {
    const uint32_t firLen = 21;
    static const double fircoef[] = {
        -8.65163e-18 * 2.0, -0.00851586 * 2.0, -6.74764e-18 * 2.0, 0.0209036 * 2.0,
        -3.36639e-17 * 2.0, -0.0438162 * 2.0, -1.54175e-17 * 2.0, 0.0931738 * 2.0,
        -5.52212e-17 * 2.0, -0.313819 * 2.0
    };
    const uint32_t x = prevBufSz;
    memcpy(HPFBuffer.data() + x, in, BlockSz * sizeof(double));
    const double* inBuf = HPFBuffer.data();
    for (int i = 0; i < BlockSz; ++i) {
        double s = inBuf[i + 10];
        double s2 = 0;
        for (int j = 0; j < ((firLen - 1) / 2) - 1 ; j += 2) {
            s += fircoef[j] * (inBuf[i + j] + inBuf[i + firLen - j]);
            s2 += fircoef[j + 1] * (inBuf[i + j + 1] + inBuf[i + firLen - j - 1]);
        }
        out[i] = (s + s2)/2;
    }
    memcpy(HPFBuffer.data(), in + (BlockSz - x),  x * sizeof(double));
}


bool TTransientDetector::Detect(const double* buf) {
    const uint32_t nBlocksToAnalize = NShortBlocks + 1;
    double* rmsPerShortBlock = reinterpret_cast<double*>(alloca(sizeof(double) * nBlocksToAnalize));
    std::vector<double> filtered(BlockSz);
    HPFilter(buf, filtered.data());
    bool trans = false;
    rmsPerShortBlock[0] = LastEnergy;
    for (uint32_t i = 1; i < nBlocksToAnalize; ++i) {
        rmsPerShortBlock[i] = 19.0 * log10(calculateRMS(&filtered[(i - 1) * ShortSz], ShortSz));
        if (rmsPerShortBlock[i] - rmsPerShortBlock[i - 1] > 16) {
            trans = true;
        }
        if (rmsPerShortBlock[i - 1] - rmsPerShortBlock[i] > 20) {
            trans = true;
        }
    }
    LastEnergy = rmsPerShortBlock[NShortBlocks];
    return trans;
}

}