FFTModel.h
Go to the documentation of this file.
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_MODEL_H
17 #define FFT_MODEL_H
18 
20 #include "DenseTimeValueModel.h"
21 
22 #include "base/Window.h"
23 
24 #include <bqfft/FFT.h>
25 #include <bqvec/Allocators.h>
26 
27 #include <set>
28 #include <vector>
29 #include <complex>
30 
37 {
38  Q_OBJECT
39 
42 
43 public:
53  FFTModel(ModelId model, // a DenseTimeValueModel
54  int channel,
55  WindowType windowType,
56  int windowSize,
57  int windowIncrement,
58  int fftSize);
59  ~FFTModel();
60 
61  // DenseThreeDimensionalModel and Model methods:
62  //
63  bool isOK() const override;
64  int getCompletion() const override;
65 
66  int getWidth() const override;
67  int getHeight() const override;
68 
69  float getValueAt(int x, int y) const override {
70  return getMagnitudeAt(x, y);
71  }
72  sv_frame_t getStartFrame() const override {
73  return 0;
74  }
75  sv_frame_t getTrueEndFrame() const override {
77  }
78  sv_samplerate_t getSampleRate() const override {
79  return m_sampleRate;
80  }
81  int getResolution() const override {
82  return m_windowIncrement;
83  }
84 
85  float getMinimumLevel() const override { return 0.f; } // Can't provide
86  float getMaximumLevel() const override { return 1.f; } // Can't provide
87 
88  Column getColumn(int x) const override; // magnitudes
89 
90  bool hasBinValues() const override {
91  return true;
92  }
93  QString getBinValueUnit() const override {
94  return "Hz";
95  }
96  bool shouldUseLogValueScale() const override {
97  return true;
98  }
99  float getBinValue(int n) const override;
100  QString getBinName(int n) const override;
101 
102  QVector<QString>
104  return {};
105  }
106 
107  QVector<QVector<QString>>
109  return {};
110  }
111 
112  // FFTModel methods:
113  //
114  QString getError() const { return m_error; }
115 
116  int getChannel() const { return m_channel; }
118  int getWindowSize() const { return m_windowSize; }
119  int getWindowIncrement() const { return m_windowIncrement; }
120  int getFFTSize() const { return m_fftSize; }
121 
122  void setMaximumFrequency(double freq);
123  double getMaximumFrequency() const { return m_maximumFrequency; }
124 
126 
127  float getMagnitudeAt(int x, int y) const;
128  float getMaximumMagnitudeAt(int x) const;
129  Column getPhases(int x) const;
130  float getPhaseAt(int x, int y) const;
131  void getValuesAt(int x, int y, float &real, float &imaginary) const;
132  bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
133  bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
134  bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
135 
141  virtual bool estimateStableFrequency(int x, int y, double &frequency);
142 
144  {
148  };
149 
150  typedef std::set<int> PeakLocationSet; // bin
151  typedef std::map<int, double> PeakSet; // bin -> freq
152 
157  virtual PeakLocationSet getPeaks(PeakPickType type, int x,
158  int ymin = 0, int ymax = 0) const;
159 
163  virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
164  int ymin = 0, int ymax = 0) const;
165 
166  QString getTypeName() const override { return tr("FFT"); }
167 
168 private:
169  FFTModel(const FFTModel &) =delete;
170  FFTModel &operator=(const FFTModel &) =delete;
171 
172  const ModelId m_model; // a DenseTimeValueModel
180  mutable breakfastquay::FFT m_fft;
182  mutable QString m_error;
183 
185  int bin, double &dist) const;
186 
187  std::pair<sv_frame_t, sv_frame_t> getSourceSampleRange(int column) const {
188  sv_frame_t startFrame = m_windowIncrement * sv_frame_t(column);
189  sv_frame_t endFrame = startFrame + m_windowSize;
190  // Cols are centred on the audio sample (e.g. col 0 is centred at sample 0)
191  startFrame -= m_windowSize / 2;
192  endFrame -= m_windowSize / 2;
193  return { startFrame, endFrame };
194  }
195 
196  const complexvec_t &getFFTColumn(int column) const;
197  floatvec_t getSourceSamples(int column) const;
198  floatvec_t getSourceData(std::pair<sv_frame_t, sv_frame_t>) const;
199  floatvec_t getSourceDataUncached(std::pair<sv_frame_t, sv_frame_t>) const;
200 
202  std::pair<sv_frame_t, sv_frame_t> range;
204  };
206 
207  struct SavedColumn {
208  int n;
210  };
211  mutable std::vector<SavedColumn> m_cached;
212  mutable size_t m_cacheWriteIndex;
213  size_t m_cacheSize;
214 
215  void clearCaches();
216 };
217 
218 #endif
Column getPhases(int x) const
Definition: FFTModel.cpp:170
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
QVector< QVector< QString > > toStringExportRows(DataExportOptions, sv_frame_t, sv_frame_t) const override
Emit events starting within the given range as string rows ready for conversion to an e...
Definition: FFTModel.h:108
sv_samplerate_t getSampleRate() const override
Return the frame rate in frames per second.
Definition: FFTModel.h:78
void clearCaches()
Definition: FFTModel.cpp:82
bool isOK() const override
Return true if the model was constructed successfully.
Definition: FFTModel.cpp:93
size_t m_cacheWriteIndex
Definition: FFTModel.h:212
int getWindowSize() const
Definition: FFTModel.h:118
void setMaximumFrequency(double freq)
Definition: FFTModel.cpp:119
int getFFTSize() const
Definition: FFTModel.h:120
QString getTypeName() const override
Return the type of the model.
Definition: FFTModel.h:166
std::pair< sv_frame_t, sv_frame_t > getSourceSampleRange(int column) const
Definition: FFTModel.h:187
void getValuesAt(int x, int y, float &real, float &imaginary) const
Definition: FFTModel.cpp:211
WindowType m_windowType
Definition: FFTModel.h:175
Model::Id ModelId
Definition: Model.h:344
complexvec_t col
Definition: FFTModel.h:209
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
int getWindowIncrement() const
Definition: FFTModel.h:119
float getMagnitudeAt(int x, int y) const
!! review which of these are ever actually called
Definition: FFTModel.cpp:182
int m_windowIncrement
Definition: FFTModel.h:177
floatvec_t getSourceData(std::pair< sv_frame_t, sv_frame_t >) const
Definition: FFTModel.cpp:287
float getMaximumLevel() const override
Return the maximum permissible value in each bin.
Definition: FFTModel.h:86
int getResolution() const override
Return the number of sample frames covered by each column of bins.
Definition: FFTModel.h:81
float getMinimumLevel() const override
Return the minimum permissible value in each bin.
Definition: FFTModel.h:85
virtual bool estimateStableFrequency(int x, int y, double &frequency)
Calculate an estimated frequency for a stable signal in this bin, using phase unwrapping.
Definition: FFTModel.cpp:424
An implementation of DenseThreeDimensionalModel that makes FFT data derived from a DenseTimeValueMode...
Definition: FFTModel.h:36
bool hasBinValues() const override
Return true if the bins have values as well as names.
Definition: FFTModel.h:90
virtual PeakLocationSet getPeaks(PeakPickType type, int x, int ymin=0, int ymax=0) const
Return locations of peak bins in the range [ymin,ymax].
Definition: FFTModel.cpp:461
WindowType getWindowType() const
Definition: FFTModel.h:117
breakfastquay::FFT m_fft
Definition: FFTModel.h:180
QString getBinValueUnit() const override
Obtain the name of the unit of the values returned from getBinValue(), if any.
Definition: FFTModel.h:93
const complexvec_t & getFFTColumn(int column) const
Definition: FFTModel.cpp:382
Any bin exceeding its immediate neighbours.
Definition: FFTModel.h:146
WindowType
Definition: Window.h:30
double m_maximumFrequency
Definition: FFTModel.h:181
std::vector< float, breakfastquay::StlAllocator< float > > floatvec_t
Definition: BaseTypes.h:53
PeakPickType
Definition: FFTModel.h:143
bool getMagnitudesAt(int x, float *values, int minbin=0, int count=0) const
Definition: FFTModel.cpp:224
Window< float > m_windower
Definition: FFTModel.h:179
int getCompletion() const override
Return an estimated percentage value showing how far through any background operation used to calcula...
Definition: FFTModel.cpp:108
std::pair< sv_frame_t, sv_frame_t > range
Definition: FFTModel.h:202
virtual PeakSet getPeakFrequencies(PeakPickType type, int x, int ymin=0, int ymax=0) const
Return locations and estimated stable frequencies of peak bins.
Definition: FFTModel.cpp:625
QString getBinName(int n) const override
Get the name of a given bin (i.e.
Definition: FFTModel.cpp:148
bool shouldUseLogValueScale() const override
Estimate whether a logarithmic scale might be appropriate for the value scale.
Definition: FFTModel.h:96
QString m_error
Definition: FFTModel.h:182
QString getError() const
Definition: FFTModel.h:114
size_t m_cacheSize
Definition: FFTModel.h:213
std::map< int, double > PeakSet
Definition: FFTModel.h:151
sv_frame_t getStartFrame() const override
Return the first audio frame spanned by the model.
Definition: FFTModel.h:72
sv_samplerate_t m_sampleRate
Definition: FFTModel.h:173
float getMaximumMagnitudeAt(int x) const
Definition: FFTModel.cpp:192
int m_windowSize
Definition: FFTModel.h:176
FFTModel & operator=(const FFTModel &)=delete
int getHeight() const override
Return the number of bins in each column.
Definition: FFTModel.cpp:135
int getChannel() const
Definition: FFTModel.h:116
double getMaximumFrequency() const
Definition: FFTModel.h:123
~FFTModel()
Definition: FFTModel.cpp:77
Peaks picked using sliding median window.
Definition: FFTModel.h:147
QVector< QString > getStringExportHeaders(DataExportOptions) const override
Return a label for each column that would be written by toStringExportRows.
Definition: FFTModel.h:103
FFTModel(ModelId model, int channel, WindowType windowType, int windowSize, int windowIncrement, int fftSize)
!! threading requirements? !! doubles? since we&#39;re not caching much
Definition: FFTModel.cpp:35
floatvec_t getSourceDataUncached(std::pair< sv_frame_t, sv_frame_t >) const
Definition: FFTModel.cpp:334
bool getPhasesAt(int x, float *values, int minbin=0, int count=0) const
Definition: FFTModel.cpp:237
Column getColumn(int x) const override
Get data from the given column of bin values.
Definition: FFTModel.cpp:160
float getPhaseAt(int x, int y) const
Definition: FFTModel.cpp:204
float getValueAt(int x, int y) const override
Get the single data point from the n&#39;th bin of the given column.
Definition: FFTModel.h:69
const ModelId m_model
Definition: FFTModel.h:172
int m_channel
Definition: FFTModel.h:174
std::vector< SavedColumn > m_cached
Definition: FFTModel.h:211
std::vector< std::complex< float >, breakfastquay::StlAllocator< std::complex< float > > > complexvec_t
Definition: BaseTypes.h:56
SavedSourceData m_savedData
Definition: FFTModel.h:205
int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate, int bin, double &dist) const
Definition: FFTModel.cpp:581
int getWidth() const override
Return the number of columns of bins in the model.
Definition: FFTModel.cpp:126
float getBinValue(int n) const override
Return the value of bin n, if any.
Definition: FFTModel.cpp:154
sv_frame_t getTrueEndFrame() const override
Return the audio frame at the end of the model.
Definition: FFTModel.h:75
floatvec_t getSourceSamples(int column) const
Definition: FFTModel.cpp:262
int m_fftSize
Definition: FFTModel.h:178
std::set< int > PeakLocationSet
Definition: FFTModel.h:150
int DataExportOptions