Mercurial > hg > vamp-tempogram
comparison FIRFilter.cpp @ 0:31d2a7e07786
Moved all to folder "tempogram".
author | Carl Bussey <c.bussey@se10.qmul.ac.uk> |
---|---|
date | Mon, 07 Jul 2014 10:08:14 +0100 |
parents | |
children | 597f033fa7a2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:31d2a7e07786 |
---|---|
1 // | |
2 // FIRFilter.cpp | |
3 // Tempogram | |
4 // | |
5 // Created by Carl Bussey on 25/06/2014. | |
6 // Copyright (c) 2014 Carl Bussey. All rights reserved. | |
7 // | |
8 | |
9 #include "FIRFilter.h" | |
10 #include <cmath> | |
11 #include <vamp-sdk/FFT.h> | |
12 #include <assert.h> | |
13 #include <iostream> | |
14 using namespace std; | |
15 | |
16 using Vamp::FFT; | |
17 | |
18 FIRFilter::FIRFilter(const unsigned int lengthInput, const unsigned int numberOfCoefficients) : | |
19 _lengthInput(lengthInput), | |
20 _numberOfCoefficients(numberOfCoefficients), | |
21 fftInput(NULL), | |
22 fftCoefficients(NULL), | |
23 fftReal1(NULL), | |
24 fftImag1(NULL), | |
25 fftReal2(NULL), | |
26 fftImag2(NULL), | |
27 fftFilteredReal(NULL), | |
28 fftFilteredImag(NULL), | |
29 fftOutputReal(NULL), | |
30 fftOutputImag(NULL) | |
31 { | |
32 initialise(); | |
33 } | |
34 | |
35 FIRFilter::~FIRFilter() | |
36 { | |
37 cleanup(); | |
38 } | |
39 | |
40 void | |
41 FIRFilter::initialise() | |
42 { | |
43 _lengthFIRFFT = pow(2,(ceil(log2(_lengthInput+_numberOfCoefficients-1)))); | |
44 | |
45 fftInput = new double[_lengthFIRFFT]; | |
46 fftCoefficients = new double[_lengthFIRFFT]; | |
47 fftReal1 = new double[_lengthFIRFFT]; | |
48 fftImag1 = new double[_lengthFIRFFT]; | |
49 fftReal2 = new double[_lengthFIRFFT]; | |
50 fftImag2 = new double[_lengthFIRFFT]; | |
51 fftFilteredReal = new double[_lengthFIRFFT]; | |
52 fftFilteredImag = new double[_lengthFIRFFT]; | |
53 fftOutputReal = new double[_lengthFIRFFT]; | |
54 fftOutputImag = new double[_lengthFIRFFT]; | |
55 | |
56 for(int i = 0; i < _lengthFIRFFT; i++){ | |
57 fftInput[i] = fftCoefficients[i] = fftReal1[i] = fftImag1[i] = fftReal2[i] = fftImag2[i] = fftFilteredReal[i] = fftFilteredImag[i] = fftOutputReal[i] = fftOutputImag[i] = 0.0; | |
58 } | |
59 } | |
60 | |
61 void | |
62 FIRFilter::process(const float* input, const float* coefficients, float* output) | |
63 { | |
64 float max = 0; | |
65 for(int i = 0; i < _lengthInput; i++){ | |
66 fftInput[i] = input[i]; | |
67 max = max > fftInput[i] ? max : fftInput[i]; | |
68 //cout << fftInput[i] << endl; | |
69 } | |
70 //cout << max << endl; | |
71 for(int i = 0; i < _numberOfCoefficients; i++){ | |
72 fftCoefficients[i] = coefficients[i]; | |
73 //cout << fftCoefficients[i] << endl; | |
74 } | |
75 | |
76 FFT::forward(_lengthFIRFFT, fftInput, NULL, fftReal1, fftImag1); | |
77 FFT::forward(_lengthFIRFFT, fftCoefficients, NULL, fftReal2, fftImag2); | |
78 for (int i = 0; i < _lengthFIRFFT; i++){ | |
79 fftFilteredReal[i] = (fftReal1[i] * fftReal2[i]) - (fftImag1[i] * fftImag2[i]); | |
80 fftFilteredImag[i] = (fftReal1[i] * fftImag2[i]) + (fftReal2[i] * fftImag1[i]); | |
81 } | |
82 FFT::inverse(_lengthFIRFFT, fftFilteredReal, fftFilteredImag, fftOutputReal, fftOutputImag); | |
83 | |
84 max = 0; | |
85 for(int i = 0; i < _lengthInput; i++){ | |
86 output[i] = fftOutputReal[i]; | |
87 max = max > output[i] ? max : output[i]; | |
88 } | |
89 //cout << max << endl; | |
90 } | |
91 | |
92 void | |
93 FIRFilter::cleanup() | |
94 { | |
95 delete []fftInput; | |
96 fftInput = NULL; | |
97 delete []fftCoefficients; | |
98 fftCoefficients = NULL; | |
99 delete []fftReal1; | |
100 fftReal1 = NULL; | |
101 delete []fftImag1; | |
102 fftImag1 = NULL; | |
103 delete []fftReal2; | |
104 fftReal2 = NULL; | |
105 delete []fftImag2; | |
106 fftImag2 = NULL; | |
107 delete []fftFilteredReal; | |
108 fftFilteredReal = NULL; | |
109 delete []fftFilteredImag; | |
110 fftFilteredImag = NULL; | |
111 delete []fftOutputReal; | |
112 fftOutputReal = NULL; | |
113 delete []fftOutputImag; | |
114 fftOutputImag = NULL; | |
115 } |