v2.0.0
Loading...
Searching...
No Matches
cosinefilter.cpp
Go to the documentation of this file.
1//=============================================================================================================
12
13//=============================================================================================================
14// INCLUDES
15//=============================================================================================================
16
17#include "cosinefilter.h"
18
19// MSVC builds already define _USE_MATH_DEFINES globally (see src/CMakeLists.txt),
20// so define it here only for the toolchains that do not.
21#ifndef _USE_MATH_DEFINES
22#define _USE_MATH_DEFINES
23#endif
24#include <math.h>
25
26#include <algorithm>
27
28//=============================================================================================================
29// EIGEN INCLUDES
30//=============================================================================================================
31
32//#ifndef EIGEN_FFTW_DEFAULT
33//#define EIGEN_FFTW_DEFAULT
34//#endif
35
36#include <unsupported/Eigen/FFT>
37
38//=============================================================================================================
39// USED NAMESPACES
40//=============================================================================================================
41
42using namespace UTILSLIB;
43using namespace Eigen;
44
45//=============================================================================================================
46// DEFINE MEMBER METHODS
47//=============================================================================================================
48
53
54//=============================================================================================================
55
57 float lowpass,
58 float lowpass_width,
59 float highpass,
60 float highpass_width,
61 double sFreq,
62 TPassType type)
63{
64#ifdef EIGEN_FFTW_DEFAULT
65 fftw_make_planner_thread_safe();
66#endif
67
68 m_iFilterOrder = fftLength;
69
70 int highpasss, lowpasss;
71 int highpass_widths, lowpass_widths;
72 int k, s, w;
73 int resp_size = fftLength / 2 + 1; //Take half because we are not interested in the conjugate complex part of the spectrum
74
75 double pi4 = M_PI / 4.0;
76 float mult, add, c;
77
78 RowVectorXcd filterFreqResp = RowVectorXcd::Ones(resp_size);
79
80 //Transform frequencies into samples
81 highpasss = ((resp_size - 1) * highpass) / (0.5 * sFreq);
82 lowpasss = ((resp_size - 1) * lowpass) / (0.5 * sFreq);
83
84 lowpass_widths = ((resp_size - 1) * lowpass_width) / (0.5 * sFreq);
85 lowpass_widths = (lowpass_widths + 1) / 2;
86
87 if (highpass_width > 0.0) {
88 highpass_widths = ((resp_size - 1) * highpass_width) / (0.5 * sFreq);
89 highpass_widths = (highpass_widths + 1) / 2;
90 } else
91 highpass_widths = 3;
92
93 //Calculate filter freq response - use cosine
94 //Build high pass filter
95 if (type != LPF) {
96 if (highpasss > highpass_widths + 1) {
97 w = highpass_widths;
98 mult = 1.0 / w;
99 add = 3.0;
100
101 for (k = 0; k < resp_size; k++)
102 filterFreqResp(k) = 0.0;
103
104 for (k = -w + 1, s = highpasss - w + 1; k < w; k++, s++) {
105 if (s >= 0 && s < resp_size) {
106 c = cos(pi4 * (k * mult + add));
107 filterFreqResp(s) = filterFreqResp(s).real() * c * c;
108 }
109 }
110
111 for (k = std::max(0, highpasss + w); k < resp_size; ++k) {
112 filterFreqResp(k) = 1.0;
113 }
114 }
115 }
116
117 //Build low pass filter
118 if (type != HPF) {
119 if (lowpass_widths > 0) {
120 w = lowpass_widths;
121 mult = 1.0 / w;
122 add = 1.0;
123
124 for (k = -w + 1, s = lowpasss - 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 = s; k < resp_size; k++)
132 filterFreqResp(k) = 0.0;
133 } else {
134 for (k = lowpasss; k < resp_size; k++)
135 filterFreqResp(k) = 0.0;
136 }
137 }
138
139 m_vecFftCoeff = filterFreqResp;
140
141 //Generate windowed impulse response - invert fft coeeficients to time domain
142 Eigen::FFT<double> fft;
143 fft.SetFlag(fft.HalfSpectrum);
144
145 //invert to time domain and
146 fft.inv(m_vecCoeff, filterFreqResp); /*
147 m_vecCoeff = m_vecCoeff.segment(0,1024).eval();
148
149 //window/zero-pad m_vecCoeff to m_iFftLength
150 RowVectorXd vecCoeffZeroPad = RowVectorXd::Zero(fftLength);
151 vecCoeffZeroPad.head(m_vecCoeff.cols()) = m_vecCoeff;
152
153 //fft-transform filter coeffs
154 m_vecFftCoeff = RowVectorXcd::Zero(fftLength);
155 fft.fwd(m_vecFftCoeff,vecCoeffZeroPad);*/
156}
157
158//=============================================================================================================
#define M_PI
Frequency-domain cosine-tapered (raised-cosine) FIR filter design.
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Eigen::RowVectorXd m_vecCoeff
Eigen::RowVectorXcd m_vecFftCoeff