annotate dsp/transforms/FFT.h @ 170:8c86761a5533

Fix overrun in reading inverse complex-to-real FFT input (contrary to docs)
author Chris Cannam
date Fri, 09 May 2014 14:35:46 +0100
parents a586888bc06c
children 857ca50ca25f
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 QM DSP Library
cannam@0 5
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
cannam@0 7 */
cannam@0 8
cannam@0 9 #ifndef FFT_H
cannam@0 10 #define FFT_H
cannam@0 11
cannam@0 12 class FFT
cannam@0 13 {
cannam@0 14 public:
Chris@114 15 /**
Chris@114 16 * Construct an FFT object to carry out complex-to-complex
Chris@129 17 * transforms of size nsamples. nsamples does not have to be a
Chris@129 18 * power of two.
Chris@114 19 */
Chris@114 20 FFT(int nsamples);
cannam@64 21 ~FFT();
cannam@0 22
Chris@114 23 /**
Chris@114 24 * Carry out a forward or inverse transform (depending on the
Chris@114 25 * value of inverse) of size nsamples, where nsamples is the value
Chris@114 26 * provided to the constructor above.
Chris@114 27 *
Chris@114 28 * realIn and (where present) imagIn should contain nsamples each,
Chris@114 29 * and realOut and imagOut should point to enough space to receive
Chris@114 30 * nsamples each.
Chris@114 31 *
Chris@114 32 * imagIn may be NULL if the signal is real, but the other
Chris@114 33 * pointers must be valid.
Chris@114 34 *
Chris@114 35 * The inverse transform is scaled by 1/nsamples.
Chris@114 36 */
cannam@64 37 void process(bool inverse,
cannam@64 38 const double *realIn, const double *imagIn,
cannam@64 39 double *realOut, double *imagOut);
cannam@64 40
cannam@64 41 private:
Chris@129 42 class D;
Chris@129 43 D *m_d;
cannam@0 44 };
cannam@0 45
cannam@64 46 class FFTReal
cannam@64 47 {
cannam@64 48 public:
Chris@114 49 /**
Chris@114 50 * Construct an FFT object to carry out real-to-complex transforms
Chris@129 51 * of size nsamples. nsamples does not have to be a power of two,
Chris@131 52 * but it does have to be even. (Use the complex-complex FFT above
Chris@131 53 * if you need an odd FFT size. This constructor will throw
Chris@131 54 * std::invalid_argument if nsamples is odd.)
Chris@114 55 */
Chris@114 56 FFTReal(int nsamples);
cannam@64 57 ~FFTReal();
cannam@64 58
Chris@114 59 /**
Chris@114 60 * Carry out a forward real-to-complex transform of size nsamples,
Chris@114 61 * where nsamples is the value provided to the constructor above.
Chris@114 62 *
Chris@114 63 * realIn, realOut, and imagOut must point to (enough space for)
Chris@114 64 * nsamples values. For consistency with the FFT class above, and
Chris@114 65 * compatibility with existing code, the conjugate half of the
Chris@114 66 * output is returned even though it is redundant.
Chris@114 67 */
Chris@114 68 void forward(const double *realIn,
cannam@64 69 double *realOut, double *imagOut);
cannam@64 70
Chris@114 71 /**
Chris@131 72 * Carry out a forward real-to-complex transform of size nsamples,
Chris@131 73 * where nsamples is the value provided to the constructor
Chris@131 74 * above. Return only the magnitudes of the complex output values.
Chris@131 75 *
Chris@131 76 * realIn and magOut must point to (enough space for) nsamples
Chris@131 77 * values. For consistency with the FFT class above, and
Chris@131 78 * compatibility with existing code, the conjugate half of the
Chris@131 79 * output is returned even though it is redundant.
Chris@131 80 */
Chris@131 81 void forwardMagnitude(const double *realIn, double *magOut);
Chris@131 82
Chris@131 83 /**
Chris@114 84 * Carry out an inverse real transform (i.e. complex-to-real) of
Chris@114 85 * size nsamples, where nsamples is the value provided to the
Chris@114 86 * constructor above.
Chris@114 87 *
Chris@114 88 * realIn and imagIn should point to at least nsamples/2+1 values;
Chris@114 89 * if more are provided, only the first nsamples/2+1 values of
Chris@114 90 * each will be used (the conjugate half will always be deduced
Chris@114 91 * from the first nsamples/2+1 rather than being read from the
Chris@114 92 * input data). realOut should point to enough space to receive
Chris@114 93 * nsamples values.
Chris@114 94 *
Chris@114 95 * The inverse transform is scaled by 1/nsamples.
Chris@114 96 */
Chris@114 97 void inverse(const double *realIn, const double *imagIn,
Chris@114 98 double *realOut);
Chris@114 99
cannam@64 100 private:
Chris@129 101 class D;
Chris@129 102 D *m_d;
cannam@64 103 };
cannam@64 104
cannam@0 105 #endif