Mercurial > hg > constant-q-cpp
comparison src/dsp/Resampler.cpp @ 164:1081c73fbbe3
Various changes to make the code compatible with MSVC++ (though we don't provide projects for it yet)
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 08 Aug 2014 10:07:25 +0100 |
parents | edbec47f4a3d |
children | 5b1a1bbd6e7f |
comparison
equal
deleted
inserted
replaced
162:7c444fea4338 | 164:1081c73fbbe3 |
---|---|
38 #include <iostream> | 38 #include <iostream> |
39 #include <vector> | 39 #include <vector> |
40 #include <map> | 40 #include <map> |
41 #include <cassert> | 41 #include <cassert> |
42 | 42 |
43 #include <pthread.h> | |
44 | |
45 using std::vector; | 43 using std::vector; |
46 using std::map; | 44 using std::map; |
47 using std::cerr; | 45 using std::cerr; |
48 using std::endl; | 46 using std::endl; |
49 | 47 |
68 Resampler::~Resampler() | 66 Resampler::~Resampler() |
69 { | 67 { |
70 delete[] m_phaseData; | 68 delete[] m_phaseData; |
71 } | 69 } |
72 | 70 |
73 // peakToPole -> length -> beta -> window | |
74 static map<double, map<int, map<double, vector<double> > > > | |
75 knownFilters; | |
76 | |
77 static pthread_mutex_t | |
78 knownFilterMutex = PTHREAD_MUTEX_INITIALIZER; | |
79 | |
80 void | 71 void |
81 Resampler::initialise(double snr, double bandwidth) | 72 Resampler::initialise(double snr, double bandwidth) |
82 { | 73 { |
83 int higher = std::max(m_sourceRate, m_targetRate); | 74 int higher = std::max(m_sourceRate, m_targetRate); |
84 int lower = std::min(m_sourceRate, m_targetRate); | 75 int lower = std::min(m_sourceRate, m_targetRate); |
102 | 93 |
103 m_filterLength = params.length; | 94 m_filterLength = params.length; |
104 | 95 |
105 vector<double> filter; | 96 vector<double> filter; |
106 | 97 |
107 pthread_mutex_lock(&knownFilterMutex); | 98 KaiserWindow kw(params); |
108 | 99 SincWindow sw(m_filterLength, m_peakToPole * 2); |
109 if (knownFilters[m_peakToPole][m_filterLength].find(params.beta) == | 100 |
110 knownFilters[m_peakToPole][m_filterLength].end()) { | 101 filter = vector<double>(m_filterLength, 0.0); |
111 | 102 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0; |
112 KaiserWindow kw(params); | 103 sw.cut(filter.data()); |
113 SincWindow sw(m_filterLength, m_peakToPole * 2); | 104 kw.cut(filter.data()); |
114 | 105 |
115 filter = vector<double>(m_filterLength, 0.0); | |
116 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0; | |
117 sw.cut(filter.data()); | |
118 kw.cut(filter.data()); | |
119 | |
120 knownFilters[m_peakToPole][m_filterLength][params.beta] = filter; | |
121 } | |
122 | |
123 filter = knownFilters[m_peakToPole][m_filterLength][params.beta]; | |
124 | |
125 pthread_mutex_unlock(&knownFilterMutex); | |
126 | |
127 int inputSpacing = m_targetRate / m_gcd; | 106 int inputSpacing = m_targetRate / m_gcd; |
128 int outputSpacing = m_sourceRate / m_gcd; | 107 int outputSpacing = m_sourceRate / m_gcd; |
129 | 108 |
130 #ifdef DEBUG_RESAMPLER | 109 #ifdef DEBUG_RESAMPLER |
131 cerr << "resample " << m_sourceRate << " -> " << m_targetRate | 110 cerr << "resample " << m_sourceRate << " -> " << m_targetRate |
313 double v = 0.0; | 292 double v = 0.0; |
314 int n = pd.filter.size(); | 293 int n = pd.filter.size(); |
315 | 294 |
316 assert(n + m_bufferOrigin <= (int)m_buffer.size()); | 295 assert(n + m_bufferOrigin <= (int)m_buffer.size()); |
317 | 296 |
318 const double *const __restrict__ buf = m_buffer.data() + m_bufferOrigin; | 297 #if defined(__MSVC__) |
319 const double *const __restrict__ filt = pd.filter.data(); | 298 #define R__ __restrict |
299 #elif defined(__GNUC__) | |
300 #define R__ __restrict__ | |
301 #else | |
302 #define R__ | |
303 #endif | |
304 | |
305 const double *const R__ buf(m_buffer.data() + m_bufferOrigin); | |
306 const double *const R__ filt(pd.filter.data()); | |
320 | 307 |
321 for (int i = 0; i < n; ++i) { | 308 for (int i = 0; i < n; ++i) { |
322 // NB gcc can only vectorize this with -ffast-math | 309 // NB gcc can only vectorize this with -ffast-math |
323 v += buf[i] * filt[i]; | 310 v += buf[i] * filt[i]; |
324 } | 311 } |