Mercurial > hg > svcore
comparison data/fft/FFTMemoryCache.h @ 334:aa8dbac62024
* Pass StorageAdviser::Criteria into FFTModel constructor etc
author | Chris Cannam |
---|---|
date | Sun, 11 Nov 2007 20:31:12 +0000 |
parents | 260032c26c4f |
children | 02d2ad95ea52 |
comparison
equal
deleted
inserted
replaced
333:1afaf98dbf11 | 334:aa8dbac62024 |
---|---|
42 class FFTMemoryCache : public FFTCache | 42 class FFTMemoryCache : public FFTCache |
43 { | 43 { |
44 public: | 44 public: |
45 enum StorageType { | 45 enum StorageType { |
46 Compact, // 16 bits normalized polar | 46 Compact, // 16 bits normalized polar |
47 Polar, // floating point mag+phase | 47 Rectangular, // floating point real+imag |
48 Polar // floating point mag+phase | |
48 }; | 49 }; |
49 | 50 |
50 FFTMemoryCache(StorageType storageType); // of size zero, call resize() before using | 51 FFTMemoryCache(StorageType storageType); // of size zero, call resize() before using |
51 virtual ~FFTMemoryCache(); | 52 virtual ~FFTMemoryCache(); |
52 | 53 |
55 | 56 |
56 virtual void resize(size_t width, size_t height); | 57 virtual void resize(size_t width, size_t height); |
57 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size | 58 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size |
58 | 59 |
59 virtual float getMagnitudeAt(size_t x, size_t y) const { | 60 virtual float getMagnitudeAt(size_t x, size_t y) const { |
60 return getNormalizedMagnitudeAt(x, y) * m_factor[x]; | 61 if (m_storageType == Rectangular) { |
62 return sqrt(m_freal[x][y] * m_freal[x][y] + | |
63 m_fimag[x][y] * m_fimag[x][y]); | |
64 } else { | |
65 return getNormalizedMagnitudeAt(x, y) * m_factor[x]; | |
66 } | |
61 } | 67 } |
62 | 68 |
63 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const { | 69 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const { |
64 if (m_storageType == Polar) return m_fmagnitude[x][y]; | 70 if (m_storageType == Rectangular) return getMagnitudeAt(x, y) / m_factor[x]; |
71 else if (m_storageType == Polar) return m_fmagnitude[x][y]; | |
65 else return float(m_magnitude[x][y]) / 65535.0; | 72 else return float(m_magnitude[x][y]) / 65535.0; |
66 } | 73 } |
67 | 74 |
68 virtual float getMaximumMagnitudeAt(size_t x) const { | 75 virtual float getMaximumMagnitudeAt(size_t x) const { |
69 return m_factor[x]; | 76 return m_factor[x]; |
70 } | 77 } |
71 | 78 |
72 virtual float getPhaseAt(size_t x, size_t y) const { | 79 virtual float getPhaseAt(size_t x, size_t y) const { |
73 if (m_storageType == Polar) return m_fphase[x][y]; | 80 if (m_storageType == Rectangular) { |
74 int16_t i = (int16_t)m_phase[x][y]; | 81 return atan2f(m_fimag[x][y], m_freal[x][y]); |
75 return (float(i) / 32767.0) * M_PI; | 82 } else if (m_storageType == Polar) { |
83 return m_fphase[x][y]; | |
84 } else { | |
85 int16_t i = (int16_t)m_phase[x][y]; | |
86 return (float(i) / 32767.0) * M_PI; | |
87 } | |
76 } | 88 } |
77 | 89 |
78 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const { | 90 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const { |
79 float mag = getMagnitudeAt(x, y); | 91 if (m_storageType == Rectangular) { |
80 float phase = getPhaseAt(x, y); | 92 real = m_freal[x][y]; |
81 real = mag * cosf(phase); | 93 imag = m_fimag[x][y]; |
82 imag = mag * sinf(phase); | 94 } else { |
95 float mag = getMagnitudeAt(x, y); | |
96 float phase = getPhaseAt(x, y); | |
97 real = mag * cosf(phase); | |
98 imag = mag * sinf(phase); | |
99 } | |
83 } | 100 } |
101 | |
102 virtual bool haveSetColumnAt(size_t x) const { | |
103 return m_colset.get(x); | |
104 } | |
105 | |
106 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor); | |
107 | |
108 virtual void setColumnAt(size_t x, float *reals, float *imags); | |
109 | |
110 static size_t getCacheSize(size_t width, size_t height, StorageType type); | |
111 | |
112 private: | |
113 size_t m_width; | |
114 size_t m_height; | |
115 uint16_t **m_magnitude; | |
116 uint16_t **m_phase; | |
117 float **m_fmagnitude; | |
118 float **m_fphase; | |
119 float **m_freal; | |
120 float **m_fimag; | |
121 float *m_factor; | |
122 StorageType m_storageType; | |
123 ResizeableBitset m_colset; | |
84 | 124 |
85 virtual void setNormalizationFactor(size_t x, float factor) { | 125 virtual void setNormalizationFactor(size_t x, float factor) { |
86 if (x < m_width) m_factor[x] = factor; | 126 if (x < m_width) m_factor[x] = factor; |
87 } | 127 } |
88 | 128 |
103 if (x < m_width && y < m_height) { | 143 if (x < m_width && y < m_height) { |
104 if (m_storageType == Polar) m_fphase[x][y] = phase; | 144 if (m_storageType == Polar) m_fphase[x][y] = phase; |
105 else m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI)); | 145 else m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI)); |
106 } | 146 } |
107 } | 147 } |
108 | |
109 virtual bool haveSetColumnAt(size_t x) const { | |
110 return m_colset.get(x); | |
111 } | |
112 | |
113 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) { | |
114 setNormalizationFactor(x, factor); | |
115 for (size_t y = 0; y < m_height; ++y) { | |
116 setMagnitudeAt(x, y, mags[y]); | |
117 setPhaseAt(x, y, phases[y]); | |
118 } | |
119 m_colset.set(x); | |
120 } | |
121 | |
122 virtual void setColumnAt(size_t x, float *reals, float *imags); | |
123 | |
124 static size_t getCacheSize(size_t width, size_t height, StorageType type); | |
125 | |
126 private: | |
127 size_t m_width; | |
128 size_t m_height; | |
129 uint16_t **m_magnitude; | |
130 uint16_t **m_phase; | |
131 float **m_fmagnitude; | |
132 float **m_fphase; | |
133 float *m_factor; | |
134 StorageType m_storageType; | |
135 ResizeableBitset m_colset; | |
136 | 148 |
137 void resize(uint16_t **&, size_t, size_t); | 149 void resize(uint16_t **&, size_t, size_t); |
138 void resize(float **&, size_t, size_t); | 150 void resize(float **&, size_t, size_t); |
139 }; | 151 }; |
140 | 152 |