annotate data/model/FFTModel.h @ 1804:343ef2a866a4

Implement missing TabularModel editing methods. Also made these pure in TabularModel, since almost all subclasses want them and (clearly) forgetting to implement them is a problem!
author Chris Cannam
date Mon, 14 Oct 2019 14:17:37 +0100
parents dd51797e528e
children c546429d4c2f
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@152 7 This file copyright 2006 Chris Cannam.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@1086 16 #ifndef FFT_MODEL_H
Chris@1086 17 #define FFT_MODEL_H
Chris@152 18
Chris@152 19 #include "DenseThreeDimensionalModel.h"
Chris@1090 20 #include "DenseTimeValueModel.h"
Chris@1090 21
Chris@1090 22 #include "base/Window.h"
Chris@152 23
Chris@1270 24 #include <bqfft/FFT.h>
Chris@1326 25 #include <bqvec/Allocators.h>
Chris@1091 26
Chris@275 27 #include <set>
Chris@1091 28 #include <vector>
Chris@1091 29 #include <complex>
Chris@275 30
Chris@254 31 /**
Chris@254 32 * An implementation of DenseThreeDimensionalModel that makes FFT data
Chris@387 33 * derived from a DenseTimeValueModel available as a generic data
Chris@1090 34 * grid.
Chris@254 35 */
Chris@152 36 class FFTModel : public DenseThreeDimensionalModel
Chris@152 37 {
Chris@247 38 Q_OBJECT
Chris@247 39
Chris@1092 40 //!!! threading requirements?
Chris@1092 41 //!!! doubles? since we're not caching much
Chris@1092 42
Chris@152 43 public:
Chris@254 44 /**
Chris@254 45 * Construct an FFT model derived from the given
Chris@254 46 * DenseTimeValueModel, with the given window parameters and FFT
Chris@254 47 * size (which may exceed the window size, for zero-padded FFTs).
Chris@254 48 *
Chris@254 49 * If the model has multiple channels use only the given channel,
Chris@254 50 * unless the channel is -1 in which case merge all available
Chris@254 51 * channels.
Chris@254 52 */
Chris@1744 53 FFTModel(ModelId model, // a DenseTimeValueModel
Chris@152 54 int channel,
Chris@152 55 WindowType windowType,
Chris@929 56 int windowSize,
Chris@929 57 int windowIncrement,
Chris@1090 58 int fftSize);
Chris@152 59 ~FFTModel();
Chris@152 60
Chris@152 61 // DenseThreeDimensionalModel and Model methods:
Chris@152 62 //
Chris@1790 63 bool isOK() const override;
Chris@1790 64 int getCompletion() const override;
Chris@1790 65
Chris@1580 66 int getWidth() const override;
Chris@1580 67 int getHeight() const override;
Chris@1790 68
Chris@1790 69 float getValueAt(int x, int y) const override {
Chris@1790 70 return getMagnitudeAt(x, y);
Chris@1790 71 }
Chris@1790 72 sv_frame_t getStartFrame() const override {
Chris@1790 73 return 0;
Chris@1790 74 }
Chris@1725 75 sv_frame_t getTrueEndFrame() const override {
Chris@1038 76 return sv_frame_t(getWidth()) * getResolution() + getResolution();
Chris@152 77 }
Chris@1790 78 sv_samplerate_t getSampleRate() const override {
Chris@1790 79 return m_sampleRate;
Chris@1790 80 }
Chris@1790 81 int getResolution() const override {
Chris@1790 82 return m_windowIncrement;
Chris@1790 83 }
Chris@1790 84
Chris@1580 85 float getMinimumLevel() const override { return 0.f; } // Can't provide
Chris@1580 86 float getMaximumLevel() const override { return 1.f; } // Can't provide
Chris@1790 87
Chris@1580 88 Column getColumn(int x) const override; // magnitudes
Chris@1790 89
Chris@1790 90 bool hasBinValues() const override {
Chris@1790 91 return true;
Chris@1790 92 }
Chris@1790 93 QString getBinValueUnit() const override {
Chris@1790 94 return "Hz";
Chris@1790 95 }
Chris@1790 96 bool shouldUseLogValueScale() const override {
Chris@1790 97 return true;
Chris@1790 98 }
Chris@1790 99 float getBinValue(int n) const override;
Chris@1580 100 QString getBinName(int n) const override;
Chris@1790 101
Chris@1679 102 QString toDelimitedDataString(QString, DataExportOptions,
Chris@1679 103 sv_frame_t, sv_frame_t) const override {
Chris@1679 104 return "";
Chris@1679 105 }
Chris@152 106
Chris@1090 107 // FFTModel methods:
Chris@1090 108 //
Chris@1790 109 QString getError() const { return m_error; }
Chris@1790 110
Chris@1090 111 int getChannel() const { return m_channel; }
Chris@1090 112 WindowType getWindowType() const { return m_windowType; }
Chris@1090 113 int getWindowSize() const { return m_windowSize; }
Chris@1090 114 int getWindowIncrement() const { return m_windowIncrement; }
Chris@1090 115 int getFFTSize() const { return m_fftSize; }
Chris@1200 116
Chris@1780 117 void setMaximumFrequency(double freq);
Chris@1780 118 double getMaximumFrequency() const { return m_maximumFrequency; }
Chris@1780 119
Chris@1200 120 //!!! review which of these are ever actually called
Chris@1090 121
Chris@1090 122 float getMagnitudeAt(int x, int y) const;
Chris@1090 123 float getMaximumMagnitudeAt(int x) const;
Chris@1790 124 Column getPhases(int x) const;
Chris@1090 125 float getPhaseAt(int x, int y) const;
Chris@1090 126 void getValuesAt(int x, int y, float &real, float &imaginary) const;
Chris@1090 127 bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
Chris@1090 128 bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
Chris@1090 129 bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
Chris@478 130
Chris@275 131 /**
Chris@275 132 * Calculate an estimated frequency for a stable signal in this
Chris@275 133 * bin, using phase unwrapping. This will be completely wrong if
Chris@275 134 * the signal is not stable here.
Chris@275 135 */
Chris@1045 136 virtual bool estimateStableFrequency(int x, int y, double &frequency);
Chris@275 137
Chris@275 138 enum PeakPickType
Chris@275 139 {
Chris@275 140 AllPeaks, /// Any bin exceeding its immediate neighbours
Chris@275 141 MajorPeaks, /// Peaks picked using sliding median window
Chris@275 142 MajorPitchAdaptivePeaks /// Bigger window for higher frequencies
Chris@275 143 };
Chris@275 144
Chris@929 145 typedef std::set<int> PeakLocationSet; // bin
Chris@1045 146 typedef std::map<int, double> PeakSet; // bin -> freq
Chris@275 147
Chris@275 148 /**
Chris@275 149 * Return locations of peak bins in the range [ymin,ymax]. If
Chris@275 150 * ymax is zero, getHeight()-1 will be used.
Chris@275 151 */
Chris@929 152 virtual PeakLocationSet getPeaks(PeakPickType type, int x,
Chris@1191 153 int ymin = 0, int ymax = 0) const;
Chris@275 154
Chris@275 155 /**
Chris@275 156 * Return locations and estimated stable frequencies of peak bins.
Chris@275 157 */
Chris@929 158 virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
Chris@1191 159 int ymin = 0, int ymax = 0) const;
Chris@273 160
Chris@1580 161 QString getTypeName() const override { return tr("FFT"); }
Chris@345 162
Chris@1744 163 private:
Chris@1744 164 FFTModel(const FFTModel &) =delete;
Chris@1744 165 FFTModel &operator=(const FFTModel &) =delete;
Chris@360 166
Chris@1744 167 const ModelId m_model; // a DenseTimeValueModel
Chris@1780 168 sv_samplerate_t m_sampleRate;
Chris@1090 169 int m_channel;
Chris@1090 170 WindowType m_windowType;
Chris@1090 171 int m_windowSize;
Chris@1090 172 int m_windowIncrement;
Chris@1090 173 int m_fftSize;
Chris@1090 174 Window<float> m_windower;
Chris@1270 175 mutable breakfastquay::FFT m_fft;
Chris@1780 176 double m_maximumFrequency;
Chris@1784 177 mutable QString m_error;
Chris@1090 178
Chris@1040 179 int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
Chris@1576 180 int bin, double &dist) const;
Chris@1091 181
Chris@1091 182 std::pair<sv_frame_t, sv_frame_t> getSourceSampleRange(int column) const {
Chris@1091 183 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(column);
Chris@1091 184 sv_frame_t endFrame = startFrame + m_windowSize;
Chris@1091 185 // Cols are centred on the audio sample (e.g. col 0 is centred at sample 0)
Chris@1091 186 startFrame -= m_windowSize / 2;
Chris@1091 187 endFrame -= m_windowSize / 2;
Chris@1091 188 return { startFrame, endFrame };
Chris@1091 189 }
Chris@1091 190
Chris@1326 191 typedef std::vector<float, breakfastquay::StlAllocator<float>> fvec;
Chris@1326 192 typedef std::vector<std::complex<float>,
Chris@1326 193 breakfastquay::StlAllocator<std::complex<float>>> cvec;
Chris@1780 194
Chris@1780 195 cvec getFFTColumn(int column) const;
Chris@1326 196 fvec getSourceSamples(int column) const;
Chris@1326 197 fvec getSourceData(std::pair<sv_frame_t, sv_frame_t>) const;
Chris@1326 198 fvec getSourceDataUncached(std::pair<sv_frame_t, sv_frame_t>) const;
Chris@1093 199
Chris@1094 200 struct SavedSourceData {
Chris@1094 201 std::pair<sv_frame_t, sv_frame_t> range;
Chris@1326 202 fvec data;
Chris@1094 203 };
Chris@1094 204 mutable SavedSourceData m_savedData;
Chris@1371 205
Chris@1093 206 struct SavedColumn {
Chris@1093 207 int n;
Chris@1326 208 cvec col;
Chris@1093 209 };
Chris@1371 210 mutable std::vector<SavedColumn> m_cached;
Chris@1371 211 mutable size_t m_cacheWriteIndex;
Chris@1093 212 size_t m_cacheSize;
Chris@152 213 };
Chris@152 214
Chris@152 215 #endif