comparison data/model/FFTModel.cpp @ 1371:fad8f533ca13

Reduce amount of copying in FFTModel
author Chris Cannam
date Thu, 26 Jan 2017 14:11:20 +0000
parents 54af1e21705c
children 87ae75da6527
comparison
equal deleted inserted replaced
1370:54fabf5aceb8 1371:fad8f533ca13
42 m_windowSize(windowSize), 42 m_windowSize(windowSize),
43 m_windowIncrement(windowIncrement), 43 m_windowIncrement(windowIncrement),
44 m_fftSize(fftSize), 44 m_fftSize(fftSize),
45 m_windower(windowType, windowSize), 45 m_windower(windowType, windowSize),
46 m_fft(fftSize), 46 m_fft(fftSize),
47 m_cacheWriteIndex(0),
47 m_cacheSize(3) 48 m_cacheSize(3)
48 { 49 {
50 while (m_cached.size() < m_cacheSize) {
51 m_cached.push_back({ -1, cvec(m_fftSize / 2 + 1) });
52 }
53
49 if (m_windowSize > m_fftSize) { 54 if (m_windowSize > m_fftSize) {
50 cerr << "ERROR: FFTModel::FFTModel: window size (" << m_windowSize 55 cerr << "ERROR: FFTModel::FFTModel: window size (" << m_windowSize
51 << ") must be at least FFT size (" << m_fftSize << ")" << endl; 56 << ") must be at least FFT size (" << m_fftSize << ")" << endl;
52 throw invalid_argument("FFTModel window size must be at least FFT size"); 57 throw invalid_argument("FFTModel window size must be at least FFT size");
53 } 58 }
294 } 299 }
295 300
296 return data; 301 return data;
297 } 302 }
298 303
299 FFTModel::cvec 304 const FFTModel::cvec &
300 FFTModel::getFFTColumn(int n) const 305 FFTModel::getFFTColumn(int n) const
301 { 306 {
302 // The small cache (i.e. the m_cached deque) is for cases where 307 // The small cache (i.e. the m_cached deque) is for cases where
303 // values are looked up individually, and for e.g. peak-frequency 308 // values are looked up individually, and for e.g. peak-frequency
304 // spectrograms where values from two consecutive columns are 309 // spectrograms where values from two consecutive columns are
317 322
318 auto samples = getSourceSamples(n); 323 auto samples = getSourceSamples(n);
319 m_windower.cut(samples.data()); 324 m_windower.cut(samples.data());
320 breakfastquay::v_fftshift(samples.data(), m_fftSize); 325 breakfastquay::v_fftshift(samples.data(), m_fftSize);
321 326
322 cvec col(m_fftSize/2 + 1); 327 cvec &col = m_cached[m_cacheWriteIndex].col;
323 328
324 m_fft.forwardInterleaved(samples.data(), 329 m_fft.forwardInterleaved(samples.data(),
325 reinterpret_cast<float *>(col.data())); 330 reinterpret_cast<float *>(col.data()));
326 331
327 SavedColumn sc { n, col }; 332 m_cached[m_cacheWriteIndex].n = n;
328 if (m_cached.size() >= m_cacheSize) { 333
329 m_cached.pop_front(); 334 m_cacheWriteIndex = (m_cacheWriteIndex + 1) % m_cacheSize;
330 }
331 m_cached.push_back(sc);
332 335
333 return col; 336 return col;
334 } 337 }
335 338
336 bool 339 bool