20 FIR::FIR(
int taps,
double bandwidth,
double center) :
21 m_taps(taps), m_bandWidth(bandwidth), m_centerFreq(center) {
22 if(taps < 3) taps = 2;
25 int taplen = 2 * taps + 1;
27 int fftlen = lrint(pow(2.0, ceil(log(taplen * 5) / log(2.0))));
28 fftlen = std::max(64, fftlen);
30 m_pBufR = (
double*)fftw_malloc(
sizeof(
double) * fftlen);
31 m_pBufC = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * (fftlen / 2 + 1));
32 m_firWnd.resize(fftlen / 2 + 1);
33 m_rdftplan = fftw_plan_dft_r2c_1d(fftlen, m_pBufR, m_pBufC, FFTW_ESTIMATE);
34 m_ridftplan = fftw_plan_dft_c2r_1d(fftlen, m_pBufC, m_pBufR, FFTW_ESTIMATE);
36 double omega = M_PI * bandwidth;
37 for(
int i = 0; i < fftlen; i++)
40 for(
int i = -taps; i <= taps; i++) {
43 double y = (i == 0) ? 1.0 : (sin(x)/x);
44 y *= 0.54 + 0.46*cos(M_PI*(
double)i/taps);
45 m_pBufR[(fftlen + i) % fftlen] = y;
50 double omega_c = 2 * M_PI * center;
51 for(
int i = -taps; i <= taps; i++) {
52 m_pBufR[(fftlen + i) % fftlen] *= cos(omega_c * i) / (z * (double)(fftlen));
55 fftw_execute(m_rdftplan);
57 for(
int i = 0; i < (int)m_firWnd.size(); i++) {
58 m_firWnd[i] = m_pBufC[i][0];
62 fftw_destroy_plan(m_rdftplan);
63 fftw_destroy_plan(m_ridftplan);
68 FIR::exec(
const double *src,
double *dst,
int len) {
69 for(
int ss = 0; ss < len; ss += (int)m_fftLen - m_tapLen * 2) {
70 for(
int i = 0; i < m_fftLen; i++) {
71 int j = ss + i - m_tapLen;
73 j = std::min(-j - 1, len - 1);
75 j = std::max(2 * len - 1 - j, 0);
78 fftw_execute(m_rdftplan);
79 for(
int i = 0; i < (int)m_firWnd.size(); i++) {
80 m_pBufC[i][0] = m_pBufC[i][0] * m_firWnd[i];
81 m_pBufC[i][1] = m_pBufC[i][1] * m_firWnd[i];
83 fftw_execute(m_ridftplan);
84 for(
int i = m_tapLen; i < m_fftLen - m_tapLen; i++) {
85 int j = ss + i - m_tapLen;
86 if((j < 0) || (j >= len))