CiCueTea v0.0.1
Loading...
Searching...
No Matches
CQT.hpp
Go to the documentation of this file.
1//
2// CQT.hpp
3// CQTDSP
4//
5// Created by Juan Sierra on 3/9/25.
6//
7
15
16#pragma once
17
18#include <memory>
19#include <vector>
20
21#include <Eigen/Core>
22
23#include "FFT.hpp"
24
25namespace jsa::cicuetea {
26
43{
44 public:
49 struct BandInfo {
50 Eigen::Index nBands;
51 Eigen::Index nBandsDown;
52 Eigen::Index nBandsUp;
53 };
54
65 NsgfCqtCommon(double sampleRate, Eigen::Index numSamples, double fraction,
66 double minFrequency, double maxFrequency, double refFrequency);
67
85 bool isValid() const { return valid && frameOk; }
86
93 double getFrameConditionNumber() const;
94
95 // Accessor methods for various parameters and computed data.
96 double getSampleRate() const { return fs; }
97 Eigen::Index getBlockSize() const { return nSamps; }
98 Eigen::Index getNumSamps() const { return nSamps; }
99 double getFraction() const { return frac; }
100 double getPpo() const { return 1.0 / frac; }
101 double getMinFreq() const { return fMin; }
102 double getMaxFreq() const { return fMax; }
103 double getRefFreq() const { return fRef; }
104 Eigen::Index getNumFreqs() const { return nFreqs; }
105 Eigen::Index getNumBands() const { return nBands; }
106 const Eigen::ArrayXd& getFrequencyAxis() const { return fax; }
107 const Eigen::ArrayXd& getBandAxis() const { return bax; }
108 const Eigen::ArrayXd& getDiagonalization() const { return d; }
109
110 protected:
123 static bool validate(double fs, Eigen::Index nSamps, double frac,
124 double fMin, double fMax, double fRef);
125
135 static BandInfo computeBandInfo(double frac, double fMin,
136 double fMax, double fRef);
137
149 bool checkFrameHealth() const;
150
153 static constexpr double dHealthTol = 1e-6;
154
157 static constexpr double th = 1e-6;
158
166 static constexpr Eigen::Index minAtomSupport = 4;
167
168 // Member variables for filterbank parameters and computed data.
169 // `valid` is declared first on purpose: member initialization follows
170 // declaration order, and the members below branch on it.
171 const bool valid;
172 bool frameOk = true;
173 const double fs;
174 const Eigen::Index nSamps;
175 const double frac;
176 const double fMin;
177 const double fMax;
178 const double fRef;
180 const Eigen::Index nBands;
181 const Eigen::Index nFreqs;
182 Eigen::ArrayXd bax;
183 Eigen::ArrayXd fax;
184 Eigen::ArrayXd d;
185 Eigen::ArrayXcd Xdft;
187};
188
197{
198 public:
209 NsgfCqtDense(double sampleRate, Eigen::Index numSamples, double fraction,
210 double minFrequency, double maxFrequency, double refFrequency);
211
218 void forward(const Eigen::ArrayXd& x, Eigen::ArrayXXcd& Xcq);
219
226 void inverse(const Eigen::ArrayXXcd& Xcq, Eigen::ArrayXd& x);
227
228 // Accessor methods for frame and dual frame.
229 const Eigen::ArrayXXd& getFrame() const { return g; }
230 const Eigen::ArrayXXd& getDualFrame() const { return gDual; }
231
232 private:
233 Eigen::ArrayXXd g;
234 Eigen::ArrayXXd gDual;
235 Eigen::ArrayXXcd Xmat;
236};
237
246{
247 public:
252 struct Span {
253 Eigen::Index i0 = 0;
254 Eigen::Index len = 0;
255 };
256
257 using Coefs = std::vector<Eigen::ArrayXcd>;
258 using Frame = std::vector<Eigen::ArrayXd>;
259 using SpanList = std::vector<Span>;
260
271 NsgfCqtSparse(double sampleRate, Eigen::Index nSamps, double fraction,
272 double minFrequency, double maxFrequency, double refFrequency);
273
280 void forward(const Eigen::ArrayXd& x, Coefs& Xcq);
281
288 void inverse(const Coefs& Xcq, Eigen::ArrayXd& x);
289
290 // Accessor methods for frame, dual frame, and band spans.
291 const Frame& getFrame() const { return g; }
292 const Eigen::ArrayXd& getAtom(Eigen::Index k) const { return g[k]; }
293 const Frame& getDualFrame() const { return gDual; }
294 const Eigen::ArrayXd& getDualAtom(Eigen::Index k) const { return gDual[k]; }
295 const Coefs& getPhaseCoefs() const { return phase; }
296 Span getBandSpan(Eigen::Index k) const { return idx[k]; }
297 Eigen::ArrayXd getFrequencyAxis(Eigen::Index k) const { return fax.segment(idx[k].i0, idx[k].len); }
298 Eigen::Index getLength(Eigen::Index k) const { return idx[k].len; };
299 double getCoeffRate(Eigen::Index k) const { return getSampleRate() * double(getLength(k)) / double(getBlockSize()); }
300
301 // Methods for retrieving coefficients.
302 Frame getRealCoefs() const;
303 Coefs getCoefs() const;
304 Coefs getValidCoefs() const;
305
306 private:
317 Span getIdx(const Eigen::ArrayXd& ii);
318
319 SpanList idx;
320 Frame g;
321 Frame gDual;
322 Coefs phase;
323 Eigen::ArrayXd scale;
324 Coefs Xcoefs;
325 std::vector<std::unique_ptr<DFT>> dfts;
326};
327
328} // namespace jsa::cicuetea
FFT wrapper (the DFT class) over the vDSP / MKL / FFTW / PFFFT backends.
The Wrapper class for other FFTs provided by different libraries.
Definition FFT.hpp:46
Eigen::ArrayXd fax
Frequency axis.
Definition CQT.hpp:183
const double fRef
Reference frequency (Hz).
Definition CQT.hpp:178
static constexpr double dHealthTol
Definition CQT.hpp:153
const double fs
Sampling rate (Hz).
Definition CQT.hpp:173
bool frameOk
Measured frame health; set by derived ctors.
Definition CQT.hpp:172
const bool valid
Structural validity: result of validate().
Definition CQT.hpp:171
static constexpr Eigen::Index minAtomSupport
Definition CQT.hpp:166
const double frac
Reciprocal of bands per octave.
Definition CQT.hpp:175
Eigen::ArrayXd d
Diagonalization array.
Definition CQT.hpp:184
Eigen::Index getNumSamps() const
Synonym of getBlockSize().
Definition CQT.hpp:98
const Eigen::Index nFreqs
Number of frequencies.
Definition CQT.hpp:181
Eigen::ArrayXd bax
Band axis.
Definition CQT.hpp:182
const double fMax
Maximum frequency (Hz).
Definition CQT.hpp:177
static constexpr double th
Definition CQT.hpp:157
static BandInfo computeBandInfo(double frac, double fMin, double fMax, double fRef)
Computes band information for the filterbank.
Definition CQT.cpp:21
bool checkFrameHealth() const
Measured frame-health check on the constructed frame operator.
Definition CQT.cpp:49
Eigen::Index getBlockSize() const
Synonym of getNumSamps().
Definition CQT.hpp:97
static bool validate(double fs, Eigen::Index nSamps, double frac, double fMin, double fMax, double fRef)
Checks the structural validity conditions for a configuration.
Definition CQT.cpp:34
double getFrameConditionNumber() const
Condition number of the painless frame operator, max(d)/min(d) up to Nyquist. Advisory: reconstructio...
Definition CQT.cpp:56
const Eigen::Index nBands
Total number of bands.
Definition CQT.hpp:180
Eigen::ArrayXcd Xdft
DFT of the signal.
Definition CQT.hpp:185
NsgfCqtCommon(double sampleRate, Eigen::Index numSamples, double fraction, double minFrequency, double maxFrequency, double refFrequency)
Constructor for NsgfCqtCommon.
Definition CQT.cpp:63
bool isValid() const
True when the object is usable: the configuration passed validate() and the constructed frame passed ...
Definition CQT.hpp:85
const Eigen::Index nSamps
Number of samples in a block (0 when invalid).
Definition CQT.hpp:174
DFT dft
Discrete Fourier Transform object.
Definition CQT.hpp:186
const BandInfo bandInfo
Band information.
Definition CQT.hpp:179
const double fMin
Minimum frequency (Hz).
Definition CQT.hpp:176
void forward(const Eigen::ArrayXd &x, Eigen::ArrayXXcd &Xcq)
Performs the forward NSGF-CQT transformation.
Definition CQT.cpp:125
void inverse(const Eigen::ArrayXXcd &Xcq, Eigen::ArrayXd &x)
Performs the inverse NSGF-CQT transformation.
Definition CQT.cpp:143
NsgfCqtDense(double sampleRate, Eigen::Index numSamples, double fraction, double minFrequency, double maxFrequency, double refFrequency)
Constructor for NsgfCqtDense.
Definition CQT.cpp:92
std::vector< Eigen::ArrayXd > Frame
Type alias for frame.
Definition CQT.hpp:258
NsgfCqtSparse(double sampleRate, Eigen::Index nSamps, double fraction, double minFrequency, double maxFrequency, double refFrequency)
Constructor for NsgfCqtSparse.
Definition CQT.cpp:163
std::vector< Span > SpanList
Type alias for span list.
Definition CQT.hpp:259
std::vector< Eigen::ArrayXcd > Coefs
Type alias for coefficients.
Definition CQT.hpp:257
void inverse(const Coefs &Xcq, Eigen::ArrayXd &x)
Performs the inverse NSGF-CQT transformation.
Definition CQT.cpp:239
void forward(const Eigen::ArrayXd &x, Coefs &Xcq)
Performs the forward NSGF-CQT transformation.
Definition CQT.cpp:220
Holds information about the number of bands in the filterbank.
Definition CQT.hpp:49
Eigen::Index nBandsDown
Number of bands below the reference frequency.
Definition CQT.hpp:51
Eigen::Index nBands
Total number of bands.
Definition CQT.hpp:50
Eigen::Index nBandsUp
Number of bands above the reference frequency.
Definition CQT.hpp:52
Represents a span of indices for a band.
Definition CQT.hpp:252
Eigen::Index len
Length of the span.
Definition CQT.hpp:254
Eigen::Index i0
Starting index of the span.
Definition CQT.hpp:253