comparison data/fft/FFTCache.h @ 159:e5879045d22b

* Pull memory cache out into its own file
author Chris Cannam
date Wed, 16 Aug 2006 14:59:09 +0000
parents 21792a550ec9
children 02d2ad95ea52
comparison
equal deleted inserted replaced
158:74abef65711b 159:e5879045d22b
52 protected: 52 protected:
53 FFTCache() { } 53 FFTCache() { }
54 }; 54 };
55 55
56 56
57 /**
58 * For the in-memory FFT cache, we would like to cache magnitude with
59 * enough resolution to have gain applied afterwards and determine
60 * whether something is a peak or not, and also cache phase rather
61 * than only phase-adjusted frequency so that we don't have to
62 * recalculate if switching between phase and magnitude displays. At
63 * the same time, we don't want to take up too much memory. It's not
64 * expected to be accurate enough to be used as input for DSP or
65 * resynthesis code.
66 *
67 * This implies probably 16 bits for a normalized magnitude and at
68 * most 16 bits for phase.
69 *
70 * Each column's magnitudes are expected to be stored normalized
71 * to [0,1] with respect to the column, so the normalization
72 * factor should be calculated before all values in a column, and
73 * set appropriately.
74 */
75
76 class FFTMemoryCache : public FFTCache
77 {
78 public:
79 FFTMemoryCache(); // of size zero, call resize() before using
80 virtual ~FFTMemoryCache();
81
82 virtual size_t getWidth() const { return m_width; }
83 virtual size_t getHeight() const { return m_height; }
84
85 virtual void resize(size_t width, size_t height);
86 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size
87
88 virtual float getMagnitudeAt(size_t x, size_t y) const {
89 return getNormalizedMagnitudeAt(x, y) * m_factor[x];
90 }
91
92 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const {
93 return float(m_magnitude[x][y]) / 65535.0;
94 }
95
96 virtual float getMaximumMagnitudeAt(size_t x) const {
97 return m_factor[x];
98 }
99
100 virtual float getPhaseAt(size_t x, size_t y) const {
101 int16_t i = (int16_t)m_phase[x][y];
102 return (float(i) / 32767.0) * M_PI;
103 }
104
105 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const {
106 float mag = getMagnitudeAt(x, y);
107 float phase = getPhaseAt(x, y);
108 real = mag * cosf(phase);
109 imag = mag * sinf(phase);
110 }
111
112 virtual void setNormalizationFactor(size_t x, float factor) {
113 if (x < m_width) m_factor[x] = factor;
114 }
115
116 virtual void setMagnitudeAt(size_t x, size_t y, float mag) {
117 // norm factor must already be set
118 setNormalizedMagnitudeAt(x, y, mag / m_factor[x]);
119 }
120
121 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) {
122 if (x < m_width && y < m_height) {
123 m_magnitude[x][y] = uint16_t(norm * 65535.0);
124 }
125 }
126
127 virtual void setPhaseAt(size_t x, size_t y, float phase) {
128 // phase in range -pi -> pi
129 if (x < m_width && y < m_height) {
130 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
131 }
132 }
133
134 virtual bool haveSetColumnAt(size_t) const {
135 return true;
136 }
137
138 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) {
139 setNormalizationFactor(x, factor);
140 for (size_t y = 0; y < m_height; ++y) {
141 setMagnitudeAt(x, y, mags[y]);
142 setPhaseAt(x, y, phases[y]);
143 }
144 }
145
146 virtual void setColumnAt(size_t x, float *reals, float *imags);
147
148 private:
149 size_t m_width;
150 size_t m_height;
151 uint16_t **m_magnitude;
152 uint16_t **m_phase;
153 float *m_factor;
154
155 void resize(uint16_t **&, size_t, size_t);
156 };
157
158 #endif 57 #endif