v2.0.0
Loading...
Searching...
No Matches
cosinefilter.cpp
Go to the documentation of this file.
1//=============================================================================================================
35
36//=============================================================================================================
37// INCLUDES
38//=============================================================================================================
39
40#include "cosinefilter.h"
41
42#define _USE_MATH_DEFINES
43#include <math.h>
44
45#include <algorithm>
46
47//=============================================================================================================
48// EIGEN INCLUDES
49//=============================================================================================================
50
51//#ifndef EIGEN_FFTW_DEFAULT
52//#define EIGEN_FFTW_DEFAULT
53//#endif
54
55#include <unsupported/Eigen/FFT>
56
57//=============================================================================================================
58// USED NAMESPACES
59//=============================================================================================================
60
61using namespace UTILSLIB;
62using namespace Eigen;
63
64//=============================================================================================================
65// DEFINE MEMBER METHODS
66//=============================================================================================================
67
72
73//=============================================================================================================
74
76 float lowpass,
77 float lowpass_width,
78 float highpass,
79 float highpass_width,
80 double sFreq,
81 TPassType type)
82{
83 #ifdef EIGEN_FFTW_DEFAULT
84 fftw_make_planner_thread_safe();
85 #endif
86
87 m_iFilterOrder = fftLength;
88
89 int highpasss,lowpasss;
90 int highpass_widths,lowpass_widths;
91 int k,s,w;
92 int resp_size = fftLength/2+1; //Take half because we are not interested in the conjugate complex part of the spectrum
93
94 double pi4 = M_PI/4.0;
95 float mult,add,c;
96
97 RowVectorXcd filterFreqResp = RowVectorXcd::Ones(resp_size);
98
99 //Transform frequencies into samples
100 highpasss = ((resp_size-1)*highpass)/(0.5*sFreq);
101 lowpasss = ((resp_size-1)*lowpass)/(0.5*sFreq);
102
103 lowpass_widths = ((resp_size-1)*lowpass_width)/(0.5*sFreq);
104 lowpass_widths = (lowpass_widths+1)/2;
105
106 if (highpass_width > 0.0) {
107 highpass_widths = ((resp_size-1)*highpass_width)/(0.5*sFreq);
108 highpass_widths = (highpass_widths+1)/2;
109 }
110 else
111 highpass_widths = 3;
112
113 //Calculate filter freq response - use cosine
114 //Build high pass filter
115 if(type != LPF) {
116 if (highpasss > highpass_widths + 1) {
117 w = highpass_widths;
118 mult = 1.0/w;
119 add = 3.0;
120
121 for (k = 0; k < resp_size; k++)
122 filterFreqResp(k) = 0.0;
123
124 for (k = -w+1, s = highpasss-w+1; k < w; k++, s++) {
125 if (s >= 0 && s < resp_size) {
126 c = cos(pi4*(k*mult+add));
127 filterFreqResp(s) = filterFreqResp(s).real()*c*c;
128 }
129 }
130
131 for (k = std::max(0, highpasss + w); k < resp_size; ++k) {
132 filterFreqResp(k) = 1.0;
133 }
134 }
135 }
136
137 //Build low pass filter
138 if(type != HPF) {
139 if (lowpass_widths > 0) {
140 w = lowpass_widths;
141 mult = 1.0/w;
142 add = 1.0;
143
144 for (k = -w+1, s = lowpasss-w+1; k < w; k++, s++) {
145 if (s >= 0 && s < resp_size) {
146 c = cos(pi4*(k*mult+add));
147 filterFreqResp(s) = filterFreqResp(s).real()*c*c;
148 }
149 }
150
151 for (k = s; k < resp_size; k++)
152 filterFreqResp(k) = 0.0;
153 }
154 else {
155 for (k = lowpasss; k < resp_size; k++)
156 filterFreqResp(k) = 0.0;
157 }
158 }
159
160 m_vecFftCoeff = filterFreqResp;
161
162 //Generate windowed impulse response - invert fft coeeficients to time domain
163 Eigen::FFT<double> fft;
164 fft.SetFlag(fft.HalfSpectrum);
165
166 //invert to time domain and
167 fft.inv(m_vecCoeff, filterFreqResp);/*
168 m_vecCoeff = m_vecCoeff.segment(0,1024).eval();
169
170 //window/zero-pad m_vecCoeff to m_iFftLength
171 RowVectorXd vecCoeffZeroPad = RowVectorXd::Zero(fftLength);
172 vecCoeffZeroPad.head(m_vecCoeff.cols()) = m_vecCoeff;
173
174 //fft-transform filter coeffs
175 m_vecFftCoeff = RowVectorXcd::Zero(fftLength);
176 fft.fwd(m_vecFftCoeff,vecCoeffZeroPad);*/
177}
178
179//=============================================================================================================
#define M_PI
Declaration of the CosineFilter class.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Eigen::RowVectorXd m_vecCoeff
Eigen::RowVectorXcd m_vecFftCoeff