comparison FIRFilter.cpp @ 13:7680cc4c0073

* Tidying - made length of array variables type size_t and for loops unsigned int, where index > 0. * Window length parameter is now a dropdown box.
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Wed, 13 Aug 2014 14:18:00 +0100
parents 17a260410116
children c11367df624d
comparison
equal deleted inserted replaced
12:d58409ecd720 13:7680cc4c0073
9 #include "FIRFilter.h" 9 #include "FIRFilter.h"
10 10
11 using namespace std; 11 using namespace std;
12 using Vamp::FFT; 12 using Vamp::FFT;
13 13
14 FIRFilter::FIRFilter(const unsigned int lengthInput, const unsigned int numberOfCoefficients) : 14 FIRFilter::FIRFilter(const size_t &lengthInput, const size_t &numberOfCoefficients) :
15 m_lengthInput(lengthInput), 15 m_lengthInput(lengthInput),
16 m_numberOfCoefficients(numberOfCoefficients), 16 m_numberOfCoefficients(numberOfCoefficients),
17 fftInput(0), 17 m_pFftInput(0),
18 fftCoefficients(0), 18 m_pFftCoefficients(0),
19 fftReal1(0), 19 m_pFftReal1(0),
20 fftImag1(0), 20 m_pFftImag1(0),
21 fftReal2(0), 21 m_pFftReal2(0),
22 fftImag2(0), 22 m_pFftImag2(0),
23 fftFilteredReal(0), 23 m_pFftFilteredReal(0),
24 fftFilteredImag(0), 24 m_pFftFilteredImag(0),
25 fftOutputReal(0), 25 m_pFftOutputReal(0),
26 fftOutputImag(0) 26 m_pFftOutputImag(0)
27 { 27 {
28 initialise(); 28 initialise();
29 } 29 }
30 30
31 FIRFilter::~FIRFilter() 31 FIRFilter::~FIRFilter()
38 FIRFilter::initialise() 38 FIRFilter::initialise()
39 { 39 {
40 //next power of 2 40 //next power of 2
41 m_lengthFIRFFT = pow(2,(ceil(log2(m_lengthInput+m_numberOfCoefficients-1)))); 41 m_lengthFIRFFT = pow(2,(ceil(log2(m_lengthInput+m_numberOfCoefficients-1))));
42 42
43 fftInput = new double[m_lengthFIRFFT]; 43 m_pFftInput = new double[m_lengthFIRFFT];
44 fftCoefficients = new double[m_lengthFIRFFT]; 44 m_pFftCoefficients = new double[m_lengthFIRFFT];
45 fftReal1 = new double[m_lengthFIRFFT]; 45 m_pFftReal1 = new double[m_lengthFIRFFT];
46 fftImag1 = new double[m_lengthFIRFFT]; 46 m_pFftImag1 = new double[m_lengthFIRFFT];
47 fftReal2 = new double[m_lengthFIRFFT]; 47 m_pFftReal2 = new double[m_lengthFIRFFT];
48 fftImag2 = new double[m_lengthFIRFFT]; 48 m_pFftImag2 = new double[m_lengthFIRFFT];
49 fftFilteredReal = new double[m_lengthFIRFFT]; 49 m_pFftFilteredReal = new double[m_lengthFIRFFT];
50 fftFilteredImag = new double[m_lengthFIRFFT]; 50 m_pFftFilteredImag = new double[m_lengthFIRFFT];
51 fftOutputReal = new double[m_lengthFIRFFT]; 51 m_pFftOutputReal = new double[m_lengthFIRFFT];
52 fftOutputImag = new double[m_lengthFIRFFT]; 52 m_pFftOutputImag = new double[m_lengthFIRFFT];
53 53
54 for(int i = 0; i < m_lengthFIRFFT; i++){ 54 for(unsigned int i = 0; i < m_lengthFIRFFT; i++){
55 fftInput[i] = fftCoefficients[i] = fftReal1[i] = fftImag1[i] = fftReal2[i] = fftImag2[i] = fftFilteredReal[i] = fftFilteredImag[i] = fftOutputReal[i] = fftOutputImag[i] = 0.0; 55 m_pFftInput[i] = m_pFftCoefficients[i] = m_pFftReal1[i] = m_pFftImag1[i] = m_pFftReal2[i] = m_pFftImag2[i] = m_pFftFilteredReal[i] = m_pFftFilteredImag[i] = m_pFftOutputReal[i] = m_pFftOutputImag[i] = 0.0;
56 } 56 }
57 } 57 }
58 58
59 void 59 void
60 FIRFilter::process(const float* input, const float* coefficients, float* output) 60 FIRFilter::process(const float* pInput, const float* pCoefficients, float* pOutput)
61 { 61 {
62 //Copy to same length FFT buffers 62 //Copy to same length FFT buffers
63 for(int i = 0; i < m_lengthFIRFFT; i++){ 63 for(unsigned int i = 0; i < m_lengthFIRFFT; i++){
64 fftInput[i] = i < m_lengthInput ? input[i] : 0.0; 64 m_pFftInput[i] = i < m_lengthInput ? pInput[i] : 0.0;
65 fftCoefficients[i] = i < m_numberOfCoefficients ? coefficients[i] : 0.0; 65 m_pFftCoefficients[i] = i < m_numberOfCoefficients ? pCoefficients[i] : 0.0;
66 } 66 }
67 67
68 FFT::forward(m_lengthFIRFFT, fftInput, 0, fftReal1, fftImag1); 68 FFT::forward(m_lengthFIRFFT, m_pFftInput, 0, m_pFftReal1, m_pFftImag1);
69 FFT::forward(m_lengthFIRFFT, fftCoefficients, 0, fftReal2, fftImag2); 69 FFT::forward(m_lengthFIRFFT, m_pFftCoefficients, 0, m_pFftReal2, m_pFftImag2);
70 70
71 //Multiply FFT coefficients. Multiplication in freq domain is convolution in time domain. 71 //Multiply FFT coefficients. Multiplication in freq domain is convolution in time domain.
72 for (int i = 0; i < m_lengthFIRFFT; i++){ 72 for (unsigned int i = 0; i < m_lengthFIRFFT; i++){
73 fftFilteredReal[i] = (fftReal1[i] * fftReal2[i]) - (fftImag1[i] * fftImag2[i]); 73 m_pFftFilteredReal[i] = (m_pFftReal1[i] * m_pFftReal2[i]) - (m_pFftImag1[i] * m_pFftImag2[i]);
74 fftFilteredImag[i] = (fftReal1[i] * fftImag2[i]) + (fftReal2[i] * fftImag1[i]); 74 m_pFftFilteredImag[i] = (m_pFftReal1[i] * m_pFftImag2[i]) + (m_pFftReal2[i] * m_pFftImag1[i]);
75 } 75 }
76 FFT::inverse(m_lengthFIRFFT, fftFilteredReal, fftFilteredImag, fftOutputReal, fftOutputImag); 76
77 FFT::inverse(m_lengthFIRFFT, m_pFftFilteredReal, m_pFftFilteredImag, m_pFftOutputReal, m_pFftOutputImag);
77 78
78 //copy to output 79 //copy to output
79 for (int i = 0; i < m_lengthInput; i++){ 80 for (unsigned int i = 0; i < m_lengthInput; i++){
80 output[i] = fftOutputReal[i]; 81 pOutput[i] = m_pFftOutputReal[i];
81 } 82 }
82 } 83 }
83 84
84 //remove memory allocations 85 //remove memory allocations
85 void 86 void
86 FIRFilter::cleanup() 87 FIRFilter::cleanup()
87 { 88 {
88 delete []fftInput; 89 delete []m_pFftInput;
89 delete []fftCoefficients; 90 delete []m_pFftCoefficients;
90 delete []fftReal1; 91 delete []m_pFftReal1;
91 delete []fftImag1; 92 delete []m_pFftImag1;
92 delete []fftReal2; 93 delete []m_pFftReal2;
93 delete []fftImag2; 94 delete []m_pFftImag2;
94 delete []fftFilteredReal; 95 delete []m_pFftFilteredReal;
95 delete []fftFilteredImag; 96 delete []m_pFftFilteredImag;
96 delete []fftOutputReal; 97 delete []m_pFftOutputReal;
97 delete []fftOutputImag; 98 delete []m_pFftOutputImag;
98 fftInput = fftCoefficients = fftReal1 = fftImag1 = fftReal2 = fftImag2 = fftFilteredReal = fftFilteredImag = fftOutputReal = fftOutputImag = 0; 99 m_pFftInput = m_pFftCoefficients = m_pFftReal1 = m_pFftImag1 = m_pFftReal2 = m_pFftImag2 = m_pFftFilteredReal = m_pFftFilteredImag = m_pFftOutputReal = m_pFftOutputImag = 0;
99 } 100 }