summaryrefslogtreecommitdiff
path: root/libs/qm-dsp/dsp/rateconversion/DecimatorB.cpp
blob: 5f27130c49f2a66d27de621b76262dfd3c05a69f (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    QM DSP Library

    Centre for Digital Music, Queen Mary, University of London.

    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 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "DecimatorB.h"

#include "maths/MathUtilities.h"

#include <iostream>

using std::vector;

DecimatorB::DecimatorB(int inLength, int decFactor)
{
    m_inputLength = 0;
    m_outputLength = 0;
    m_decFactor = 1;
    m_aaBuffer = 0;
    m_tmpBuffer = 0;

    initialise(inLength, decFactor);
}

DecimatorB::~DecimatorB()
{
    deInitialise();
}

void DecimatorB::initialise(int inLength, int decFactor)
{
    m_inputLength = inLength;
    m_decFactor = decFactor;
    m_outputLength = m_inputLength / m_decFactor;

    if (m_decFactor < 2 || !MathUtilities::isPowerOfTwo(m_decFactor)) {
        std::cerr << "ERROR: DecimatorB::initialise: Decimation factor must be a power of 2 and at least 2 (was: " << m_decFactor << ")" << std::endl;
        m_decFactor = 0;
        return;
    }

    if (m_inputLength % m_decFactor != 0) {
        std::cerr << "ERROR: DecimatorB::initialise: inLength must be a multiple of decimation factor (was: " << m_inputLength << ", factor is " << m_decFactor << ")" << std::endl;
        m_decFactor = 0;
        return;
    }        

    m_aaBuffer = new double[m_inputLength];
    m_tmpBuffer = new double[m_inputLength];

    // Order 6 Butterworth lowpass filter
    // Calculated using e.g. MATLAB butter(6, 0.5, 'low')

    m_b[0] = 0.029588223638661;
    m_b[1] = 0.177529341831965;
    m_b[2] = 0.443823354579912;
    m_b[3] = 0.591764472773216;
    m_b[4] = 0.443823354579912;
    m_b[5] = 0.177529341831965;
    m_b[6] = 0.029588223638661;

    m_a[0] = 1.000000000000000;
    m_a[1] = 0.000000000000000;
    m_a[2] = 0.777695961855673;
    m_a[3] = 0.000000000000000;
    m_a[4] = 0.114199425062434;
    m_a[5] = 0.000000000000000;
    m_a[6] = 0.001750925956183;

    for (int factor = m_decFactor; factor > 1; factor /= 2) {
        m_o.push_back(vector<double>(6, 0.0));
    }
}

void DecimatorB::deInitialise()
{
    delete [] m_aaBuffer;
    delete [] m_tmpBuffer;
}

void DecimatorB::doAntiAlias(const double *src, double *dst, int length,
                             int filteridx)
{
    vector<double> &o = m_o[filteridx];

    for (int i = 0; i < length; i++) {

	double input = src[i];
	double output = input * m_b[0] + o[0];

	o[0] = input * m_b[1] - output * m_a[1] + o[1];
	o[1] = input * m_b[2] - output * m_a[2] + o[2];
	o[2] = input * m_b[3] - output * m_a[3] + o[3];
	o[3] = input * m_b[4] - output * m_a[4] + o[4];
	o[4] = input * m_b[5] - output * m_a[5] + o[5];
	o[5] = input * m_b[6] - output * m_a[6];

	dst[i] = output;
    }
}

void DecimatorB::doProcess()
{
    int filteridx = 0;
    int factorDone = 1;

    while (factorDone < m_decFactor) {

        doAntiAlias(m_tmpBuffer, m_aaBuffer,
                    m_inputLength / factorDone,
                    filteridx);

        filteridx ++;
        factorDone *= 2;

        for (int i = 0; i < m_inputLength / factorDone; ++i) {
            m_tmpBuffer[i] = m_aaBuffer[i * 2];
        }
    }
}

void DecimatorB::process(const double *src, double *dst)
{
    if (m_decFactor == 0) return;

    for (int i = 0; i < m_inputLength; ++i) {
        m_tmpBuffer[i] = src[i];
    }

    doProcess();
    
    for (int i = 0; i < m_outputLength; ++i) {
        dst[i] = m_tmpBuffer[i];
    }
}

void DecimatorB::process(const float *src, float *dst)
{
    if (m_decFactor == 0) return;

    for (int i = 0; i < m_inputLength; ++i) {
        m_tmpBuffer[i] = src[i];
    }

    doProcess();
    
    for (int i = 0; i < m_outputLength; ++i) {
        dst[i] = m_tmpBuffer[i];
    }
}