qm-dsp 1.8
DecimatorB.cpp
Go to the documentation of this file.
1/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3/*
4 QM DSP Library
5
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13*/
14
15#include "DecimatorB.h"
16
17#include "maths/MathUtilities.h"
18
19#include <iostream>
20
21using std::vector;
22
23DecimatorB::DecimatorB(int inLength, int decFactor)
24{
25 m_inputLength = 0;
27 m_decFactor = 1;
28 m_aaBuffer = 0;
29 m_tmpBuffer = 0;
30
31 initialise(inLength, decFactor);
32}
33
38
39void DecimatorB::initialise(int inLength, int decFactor)
40{
41 m_inputLength = inLength;
42 m_decFactor = decFactor;
44
46 std::cerr << "ERROR: DecimatorB::initialise: Decimation factor must be a power of 2 and at least 2 (was: " << m_decFactor << ")" << std::endl;
47 m_decFactor = 0;
48 return;
49 }
50
51 if (m_inputLength % m_decFactor != 0) {
52 std::cerr << "ERROR: DecimatorB::initialise: inLength must be a multiple of decimation factor (was: " << m_inputLength << ", factor is " << m_decFactor << ")" << std::endl;
53 m_decFactor = 0;
54 return;
55 }
56
57 m_aaBuffer = new double[m_inputLength];
58 m_tmpBuffer = new double[m_inputLength];
59
60 // Order 6 Butterworth lowpass filter
61 // Calculated using e.g. MATLAB butter(6, 0.5, 'low')
62
63 m_b[0] = 0.029588223638661;
64 m_b[1] = 0.177529341831965;
65 m_b[2] = 0.443823354579912;
66 m_b[3] = 0.591764472773216;
67 m_b[4] = 0.443823354579912;
68 m_b[5] = 0.177529341831965;
69 m_b[6] = 0.029588223638661;
70
71 m_a[0] = 1.000000000000000;
72 m_a[1] = 0.000000000000000;
73 m_a[2] = 0.777695961855673;
74 m_a[3] = 0.000000000000000;
75 m_a[4] = 0.114199425062434;
76 m_a[5] = 0.000000000000000;
77 m_a[6] = 0.001750925956183;
78
79 for (int factor = m_decFactor; factor > 1; factor /= 2) {
80 m_o.push_back(vector<double>(6, 0.0));
81 }
82}
83
85{
86 delete [] m_aaBuffer;
87 delete [] m_tmpBuffer;
88}
89
90void DecimatorB::doAntiAlias(const double *src, double *dst, int length,
91 int filteridx)
92{
93 vector<double> &o = m_o[filteridx];
94
95 for (int i = 0; i < length; i++) {
96
97 double input = src[i];
98 double output = input * m_b[0] + o[0];
99
100 o[0] = input * m_b[1] - output * m_a[1] + o[1];
101 o[1] = input * m_b[2] - output * m_a[2] + o[2];
102 o[2] = input * m_b[3] - output * m_a[3] + o[3];
103 o[3] = input * m_b[4] - output * m_a[4] + o[4];
104 o[4] = input * m_b[5] - output * m_a[5] + o[5];
105 o[5] = input * m_b[6] - output * m_a[6];
106
107 dst[i] = output;
108 }
109}
110
112{
113 int filteridx = 0;
114 int factorDone = 1;
115 int factorRemaining = m_decFactor;
116
117 while (factorDone < m_decFactor) {
118
120 m_inputLength / factorDone,
121 filteridx);
122
123 filteridx ++;
124 factorDone *= 2;
125
126 for (int i = 0; i < m_inputLength / factorDone; ++i) {
127 m_tmpBuffer[i] = m_aaBuffer[i * 2];
128 }
129 }
130}
131
132void DecimatorB::process(const double *src, double *dst)
133{
134 if (m_decFactor == 0) return;
135
136 for (int i = 0; i < m_inputLength; ++i) {
137 m_tmpBuffer[i] = src[i];
138 }
139
140 doProcess();
141
142 for (int i = 0; i < m_outputLength; ++i) {
143 dst[i] = m_tmpBuffer[i];
144 }
145}
146
147void DecimatorB::process(const float *src, float *dst)
148{
149 if (m_decFactor == 0) return;
150
151 for (int i = 0; i < m_inputLength; ++i) {
152 m_tmpBuffer[i] = src[i];
153 }
154
155 doProcess();
156
157 for (int i = 0; i < m_outputLength; ++i) {
158 dst[i] = m_tmpBuffer[i];
159 }
160}
void deInitialise()
int m_decFactor
Definition DecimatorB.h:52
virtual ~DecimatorB()
void doProcess()
DecimatorB(int inLength, int decFactor)
Construct a DecimatorB to operate on input blocks of length inLength, with decimation factor decFacto...
int m_inputLength
Definition DecimatorB.h:50
void doAntiAlias(const double *src, double *dst, int length, int filteridx)
void process(const double *src, double *dst)
double m_b[7]
Definition DecimatorB.h:57
double m_a[7]
Definition DecimatorB.h:56
double * m_aaBuffer
Definition DecimatorB.h:59
double * m_tmpBuffer
Definition DecimatorB.h:60
int m_outputLength
Definition DecimatorB.h:51
void initialise(int inLength, int decFactor)
std::vector< std::vector< double > > m_o
Definition DecimatorB.h:54
static bool isPowerOfTwo(int x)
Return true if x is 2^n for some integer n >= 0.