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