Mercurial > hg > svcore
comparison data/fft/FFTCache.h @ 152:21792a550ec9 last-cc-copyright
* Move the current DenseThreeDimensionalModel to EditableDenseThreeDimensionalModel
(wow!), and make DTDM an abstract base
* Move FFTFuzzyAdapter to FFTModel as a new subclass of DTDM
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 17:05:18 +0000 |
parents | |
children | e5879045d22b |
comparison
equal
deleted
inserted
replaced
151:3c1d5ef43baa | 152:21792a550ec9 |
---|---|
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_CACHE_H_ | |
17 #define _FFT_CACHE_H_ | |
18 | |
19 #include <cstdlib> | |
20 #include <cmath> | |
21 | |
22 #include <stdint.h> | |
23 | |
24 class FFTCache | |
25 { | |
26 public: | |
27 virtual ~FFTCache() { } | |
28 | |
29 virtual size_t getWidth() const = 0; | |
30 virtual size_t getHeight() const = 0; | |
31 | |
32 virtual void resize(size_t width, size_t height) = 0; | |
33 virtual void reset() = 0; // zero-fill or 1-fill as appropriate without changing size | |
34 | |
35 virtual float getMagnitudeAt(size_t x, size_t y) const = 0; | |
36 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const = 0; | |
37 virtual float getMaximumMagnitudeAt(size_t x) const = 0; | |
38 virtual float getPhaseAt(size_t x, size_t y) const = 0; | |
39 | |
40 virtual void getValuesAt(size_t x, size_t y, float &real, float &imaginary) const = 0; | |
41 | |
42 virtual bool haveSetColumnAt(size_t x) const = 0; | |
43 | |
44 // may modify argument arrays | |
45 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) = 0; | |
46 | |
47 // may modify argument arrays | |
48 virtual void setColumnAt(size_t x, float *reals, float *imags) = 0; | |
49 | |
50 virtual void suspend() { } | |
51 | |
52 protected: | |
53 FFTCache() { } | |
54 }; | |
55 | |
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 |