Mercurial > hg > svcore
comparison data/fft/FFTMemoryCache.h @ 159:e5879045d22b
* Pull memory cache out into its own file
author | Chris Cannam |
---|---|
date | Wed, 16 Aug 2006 14:59:09 +0000 |
parents | |
children | b23eea68357e |
comparison
equal
deleted
inserted
replaced
158:74abef65711b | 159:e5879045d22b |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #ifndef _FFT_MEMORY_CACHE_H_ | |
17 #define _FFT_MEMORY_CACHE_H_ | |
18 | |
19 #include "FFTCache.h" | |
20 | |
21 #include "base/ResizeableBitset.h" | |
22 | |
23 /** | |
24 * For the in-memory FFT cache, we would like to cache magnitude with | |
25 * enough resolution to have gain applied afterwards and determine | |
26 * whether something is a peak or not, and also cache phase rather | |
27 * than only phase-adjusted frequency so that we don't have to | |
28 * recalculate if switching between phase and magnitude displays. At | |
29 * the same time, we don't want to take up too much memory. It's not | |
30 * expected to be accurate enough to be used as input for DSP or | |
31 * resynthesis code. | |
32 * | |
33 * This implies probably 16 bits for a normalized magnitude and at | |
34 * most 16 bits for phase. | |
35 * | |
36 * Each column's magnitudes are expected to be stored normalized | |
37 * to [0,1] with respect to the column, so the normalization | |
38 * factor should be calculated before all values in a column, and | |
39 * set appropriately. | |
40 */ | |
41 | |
42 class FFTMemoryCache : public FFTCache | |
43 { | |
44 public: | |
45 FFTMemoryCache(); // of size zero, call resize() before using | |
46 virtual ~FFTMemoryCache(); | |
47 | |
48 virtual size_t getWidth() const { return m_width; } | |
49 virtual size_t getHeight() const { return m_height; } | |
50 | |
51 virtual void resize(size_t width, size_t height); | |
52 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size | |
53 | |
54 virtual float getMagnitudeAt(size_t x, size_t y) const { | |
55 return getNormalizedMagnitudeAt(x, y) * m_factor[x]; | |
56 } | |
57 | |
58 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const { | |
59 return float(m_magnitude[x][y]) / 65535.0; | |
60 } | |
61 | |
62 virtual float getMaximumMagnitudeAt(size_t x) const { | |
63 return m_factor[x]; | |
64 } | |
65 | |
66 virtual float getPhaseAt(size_t x, size_t y) const { | |
67 int16_t i = (int16_t)m_phase[x][y]; | |
68 return (float(i) / 32767.0) * M_PI; | |
69 } | |
70 | |
71 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const { | |
72 float mag = getMagnitudeAt(x, y); | |
73 float phase = getPhaseAt(x, y); | |
74 real = mag * cosf(phase); | |
75 imag = mag * sinf(phase); | |
76 } | |
77 | |
78 virtual void setNormalizationFactor(size_t x, float factor) { | |
79 if (x < m_width) m_factor[x] = factor; | |
80 } | |
81 | |
82 virtual void setMagnitudeAt(size_t x, size_t y, float mag) { | |
83 // norm factor must already be set | |
84 setNormalizedMagnitudeAt(x, y, mag / m_factor[x]); | |
85 } | |
86 | |
87 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) { | |
88 if (x < m_width && y < m_height) { | |
89 m_magnitude[x][y] = uint16_t(norm * 65535.0); | |
90 } | |
91 } | |
92 | |
93 virtual void setPhaseAt(size_t x, size_t y, float phase) { | |
94 // phase in range -pi -> pi | |
95 if (x < m_width && y < m_height) { | |
96 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI)); | |
97 } | |
98 } | |
99 | |
100 virtual bool haveSetColumnAt(size_t x) const { | |
101 return m_colset.get(x); | |
102 } | |
103 | |
104 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) { | |
105 setNormalizationFactor(x, factor); | |
106 for (size_t y = 0; y < m_height; ++y) { | |
107 setMagnitudeAt(x, y, mags[y]); | |
108 setPhaseAt(x, y, phases[y]); | |
109 } | |
110 m_colset.set(x); | |
111 } | |
112 | |
113 virtual void setColumnAt(size_t x, float *reals, float *imags); | |
114 | |
115 private: | |
116 size_t m_width; | |
117 size_t m_height; | |
118 uint16_t **m_magnitude; | |
119 uint16_t **m_phase; | |
120 float *m_factor; | |
121 ResizeableBitset m_colset; | |
122 | |
123 void resize(uint16_t **&, size_t, size_t); | |
124 }; | |
125 | |
126 | |
127 #endif | |
128 |