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@0
|
10 #include <cmath>
|
c@0
|
11 #include <vamp-sdk/FFT.h>
|
c@0
|
12 #include <assert.h>
|
c@0
|
13 #include <iostream>
|
c@0
|
14 using namespace std;
|
c@0
|
15
|
c@0
|
16 using Vamp::FFT;
|
c@0
|
17
|
c@0
|
18 FIRFilter::FIRFilter(const unsigned int lengthInput, const unsigned int numberOfCoefficients) :
|
c@0
|
19 _lengthInput(lengthInput),
|
c@0
|
20 _numberOfCoefficients(numberOfCoefficients),
|
c@0
|
21 fftInput(NULL),
|
c@0
|
22 fftCoefficients(NULL),
|
c@0
|
23 fftReal1(NULL),
|
c@0
|
24 fftImag1(NULL),
|
c@0
|
25 fftReal2(NULL),
|
c@0
|
26 fftImag2(NULL),
|
c@0
|
27 fftFilteredReal(NULL),
|
c@0
|
28 fftFilteredImag(NULL),
|
c@0
|
29 fftOutputReal(NULL),
|
c@0
|
30 fftOutputImag(NULL)
|
c@0
|
31 {
|
c@0
|
32 initialise();
|
c@0
|
33 }
|
c@0
|
34
|
c@0
|
35 FIRFilter::~FIRFilter()
|
c@0
|
36 {
|
c@0
|
37 cleanup();
|
c@0
|
38 }
|
c@0
|
39
|
c@0
|
40 void
|
c@0
|
41 FIRFilter::initialise()
|
c@0
|
42 {
|
c@0
|
43 _lengthFIRFFT = pow(2,(ceil(log2(_lengthInput+_numberOfCoefficients-1))));
|
c@0
|
44
|
c@0
|
45 fftInput = new double[_lengthFIRFFT];
|
c@0
|
46 fftCoefficients = new double[_lengthFIRFFT];
|
c@0
|
47 fftReal1 = new double[_lengthFIRFFT];
|
c@0
|
48 fftImag1 = new double[_lengthFIRFFT];
|
c@0
|
49 fftReal2 = new double[_lengthFIRFFT];
|
c@0
|
50 fftImag2 = new double[_lengthFIRFFT];
|
c@0
|
51 fftFilteredReal = new double[_lengthFIRFFT];
|
c@0
|
52 fftFilteredImag = new double[_lengthFIRFFT];
|
c@0
|
53 fftOutputReal = new double[_lengthFIRFFT];
|
c@0
|
54 fftOutputImag = new double[_lengthFIRFFT];
|
c@0
|
55
|
c@0
|
56 for(int i = 0; i < _lengthFIRFFT; i++){
|
c@0
|
57 fftInput[i] = fftCoefficients[i] = fftReal1[i] = fftImag1[i] = fftReal2[i] = fftImag2[i] = fftFilteredReal[i] = fftFilteredImag[i] = fftOutputReal[i] = fftOutputImag[i] = 0.0;
|
c@0
|
58 }
|
c@0
|
59 }
|
c@0
|
60
|
c@0
|
61 void
|
c@0
|
62 FIRFilter::process(const float* input, const float* coefficients, float* output)
|
c@0
|
63 {
|
c@0
|
64 float max = 0;
|
c@0
|
65 for(int i = 0; i < _lengthInput; i++){
|
c@0
|
66 fftInput[i] = input[i];
|
c@0
|
67 max = max > fftInput[i] ? max : fftInput[i];
|
c@0
|
68 //cout << fftInput[i] << endl;
|
c@0
|
69 }
|
c@0
|
70 //cout << max << endl;
|
c@0
|
71 for(int i = 0; i < _numberOfCoefficients; i++){
|
c@0
|
72 fftCoefficients[i] = coefficients[i];
|
c@0
|
73 //cout << fftCoefficients[i] << endl;
|
c@0
|
74 }
|
c@0
|
75
|
c@0
|
76 FFT::forward(_lengthFIRFFT, fftInput, NULL, fftReal1, fftImag1);
|
c@0
|
77 FFT::forward(_lengthFIRFFT, fftCoefficients, NULL, fftReal2, fftImag2);
|
c@0
|
78 for (int i = 0; i < _lengthFIRFFT; i++){
|
c@0
|
79 fftFilteredReal[i] = (fftReal1[i] * fftReal2[i]) - (fftImag1[i] * fftImag2[i]);
|
c@0
|
80 fftFilteredImag[i] = (fftReal1[i] * fftImag2[i]) + (fftReal2[i] * fftImag1[i]);
|
c@0
|
81 }
|
c@0
|
82 FFT::inverse(_lengthFIRFFT, fftFilteredReal, fftFilteredImag, fftOutputReal, fftOutputImag);
|
c@0
|
83
|
c@0
|
84 max = 0;
|
c@0
|
85 for(int i = 0; i < _lengthInput; i++){
|
c@0
|
86 output[i] = fftOutputReal[i];
|
c@0
|
87 max = max > output[i] ? max : output[i];
|
c@0
|
88 }
|
c@0
|
89 }
|
c@0
|
90
|
c@0
|
91 void
|
c@0
|
92 FIRFilter::cleanup()
|
c@0
|
93 {
|
c@0
|
94 delete []fftInput;
|
c@0
|
95 fftInput = NULL;
|
c@0
|
96 delete []fftCoefficients;
|
c@0
|
97 fftCoefficients = NULL;
|
c@0
|
98 delete []fftReal1;
|
c@0
|
99 fftReal1 = NULL;
|
c@0
|
100 delete []fftImag1;
|
c@0
|
101 fftImag1 = NULL;
|
c@0
|
102 delete []fftReal2;
|
c@0
|
103 fftReal2 = NULL;
|
c@0
|
104 delete []fftImag2;
|
c@0
|
105 fftImag2 = NULL;
|
c@0
|
106 delete []fftFilteredReal;
|
c@0
|
107 fftFilteredReal = NULL;
|
c@0
|
108 delete []fftFilteredImag;
|
c@0
|
109 fftFilteredImag = NULL;
|
c@0
|
110 delete []fftOutputReal;
|
c@0
|
111 fftOutputReal = NULL;
|
c@0
|
112 delete []fftOutputImag;
|
c@0
|
113 fftOutputImag = NULL;
|
c@0
|
114 } |