c@0
|
1 //
|
c@0
|
2 // FIRFilter.cpp
|
c@0
|
3 // Tempogram
|
c@0
|
4 //
|
c@0
|
5 // Created by Carl Bussey on 25/06/2014.
|
c@0
|
6 // Copyright (c) 2014 Carl Bussey. All rights reserved.
|
c@0
|
7 //
|
c@0
|
8
|
c@0
|
9 #include "FIRFilter.h"
|
c@7
|
10
|
c@0
|
11 using namespace std;
|
c@0
|
12 using Vamp::FFT;
|
c@0
|
13
|
c@0
|
14 FIRFilter::FIRFilter(const unsigned int lengthInput, const unsigned int numberOfCoefficients) :
|
c@0
|
15 _lengthInput(lengthInput),
|
c@0
|
16 _numberOfCoefficients(numberOfCoefficients),
|
c@0
|
17 fftInput(NULL),
|
c@0
|
18 fftCoefficients(NULL),
|
c@0
|
19 fftReal1(NULL),
|
c@0
|
20 fftImag1(NULL),
|
c@0
|
21 fftReal2(NULL),
|
c@0
|
22 fftImag2(NULL),
|
c@0
|
23 fftFilteredReal(NULL),
|
c@0
|
24 fftFilteredImag(NULL),
|
c@0
|
25 fftOutputReal(NULL),
|
c@0
|
26 fftOutputImag(NULL)
|
c@0
|
27 {
|
c@0
|
28 initialise();
|
c@0
|
29 }
|
c@0
|
30
|
c@0
|
31 FIRFilter::~FIRFilter()
|
c@0
|
32 {
|
c@0
|
33 cleanup();
|
c@0
|
34 }
|
c@0
|
35
|
c@0
|
36 void
|
c@0
|
37 FIRFilter::initialise()
|
c@0
|
38 {
|
c@0
|
39 _lengthFIRFFT = pow(2,(ceil(log2(_lengthInput+_numberOfCoefficients-1))));
|
c@0
|
40
|
c@0
|
41 fftInput = new double[_lengthFIRFFT];
|
c@0
|
42 fftCoefficients = new double[_lengthFIRFFT];
|
c@0
|
43 fftReal1 = new double[_lengthFIRFFT];
|
c@0
|
44 fftImag1 = new double[_lengthFIRFFT];
|
c@0
|
45 fftReal2 = new double[_lengthFIRFFT];
|
c@0
|
46 fftImag2 = new double[_lengthFIRFFT];
|
c@0
|
47 fftFilteredReal = new double[_lengthFIRFFT];
|
c@0
|
48 fftFilteredImag = new double[_lengthFIRFFT];
|
c@0
|
49 fftOutputReal = new double[_lengthFIRFFT];
|
c@0
|
50 fftOutputImag = new double[_lengthFIRFFT];
|
c@0
|
51
|
c@0
|
52 for(int i = 0; i < _lengthFIRFFT; i++){
|
c@0
|
53 fftInput[i] = fftCoefficients[i] = fftReal1[i] = fftImag1[i] = fftReal2[i] = fftImag2[i] = fftFilteredReal[i] = fftFilteredImag[i] = fftOutputReal[i] = fftOutputImag[i] = 0.0;
|
c@0
|
54 }
|
c@0
|
55 }
|
c@0
|
56
|
c@0
|
57 void
|
c@0
|
58 FIRFilter::process(const float* input, const float* coefficients, float* output)
|
c@0
|
59 {
|
c@7
|
60 for(int i = 0; i < _lengthFIRFFT; i++){
|
c@7
|
61 fftInput[i] = i < _lengthInput ? input[i] : 0.0;
|
c@7
|
62 fftCoefficients[i] = i < _numberOfCoefficients ? coefficients[i] : 0.0;
|
c@0
|
63 }
|
c@0
|
64
|
c@0
|
65 FFT::forward(_lengthFIRFFT, fftInput, NULL, fftReal1, fftImag1);
|
c@0
|
66 FFT::forward(_lengthFIRFFT, fftCoefficients, NULL, fftReal2, fftImag2);
|
c@7
|
67
|
c@0
|
68 for (int i = 0; i < _lengthFIRFFT; i++){
|
c@0
|
69 fftFilteredReal[i] = (fftReal1[i] * fftReal2[i]) - (fftImag1[i] * fftImag2[i]);
|
c@0
|
70 fftFilteredImag[i] = (fftReal1[i] * fftImag2[i]) + (fftReal2[i] * fftImag1[i]);
|
c@0
|
71 }
|
c@0
|
72 FFT::inverse(_lengthFIRFFT, fftFilteredReal, fftFilteredImag, fftOutputReal, fftOutputImag);
|
c@0
|
73
|
c@7
|
74 for (int i = 0; i < _lengthInput; i++){
|
c@0
|
75 output[i] = fftOutputReal[i];
|
c@0
|
76 }
|
c@0
|
77 }
|
c@0
|
78
|
c@0
|
79 void
|
c@0
|
80 FIRFilter::cleanup()
|
c@0
|
81 {
|
c@0
|
82 delete []fftInput;
|
c@0
|
83 delete []fftCoefficients;
|
c@0
|
84 delete []fftReal1;
|
c@0
|
85 delete []fftImag1;
|
c@0
|
86 delete []fftReal2;
|
c@0
|
87 delete []fftImag2;
|
c@0
|
88 delete []fftFilteredReal;
|
c@0
|
89 delete []fftFilteredImag;
|
c@0
|
90 delete []fftOutputReal;
|
c@0
|
91 delete []fftOutputImag;
|
c@7
|
92 fftInput = fftCoefficients = fftReal1 = fftImag1 = fftReal2 = fftImag2 = fftFilteredReal = fftFilteredImag = fftOutputReal = fftOutputImag = NULL;
|
c@0
|
93 } |