svcore  1.9
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 
19 #include "data/fft/FFTDataServer.h"
21 
22 #include <set>
23 #include <map>
24 
34 {
35  Q_OBJECT
36 
37 public:
58  FFTModel(const DenseTimeValueModel *model,
59  int channel,
60  WindowType windowType,
61  int windowSize,
62  int windowIncrement,
63  int fftSize,
64  bool polar,
66  int fillFromColumn = 0);
67  ~FFTModel();
68 
69  inline float getMagnitudeAt(int x, int y) {
70  return m_server->getMagnitudeAt(x << m_xshift, y << m_yshift);
71  }
72  inline float getNormalizedMagnitudeAt(int x, int y) {
74  }
75  inline float getMaximumMagnitudeAt(int x) {
77  }
78  inline float getPhaseAt(int x, int y) {
79  return m_server->getPhaseAt(x << m_xshift, y << m_yshift);
80  }
81  inline void getValuesAt(int x, int y, float &real, float &imaginary) {
82  m_server->getValuesAt(x << m_xshift, y << m_yshift, real, imaginary);
83  }
84  inline bool isColumnAvailable(int x) const {
85  return m_server->isColumnReady(x << m_xshift);
86  }
87 
88  inline bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) {
89  return m_server->getMagnitudesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
90  }
91  inline bool getNormalizedMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) {
92  return m_server->getNormalizedMagnitudesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
93  }
94  inline bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) {
95  return m_server->getPhasesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
96  }
97  inline bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) {
98  return m_server->getValuesAt(x << m_xshift, reals, imaginaries, minbin << m_yshift, count, getYRatio());
99  }
100 
101  inline int getFillExtent() const { return m_server->getFillExtent(); }
102 
103  // DenseThreeDimensionalModel and Model methods:
104  //
105  inline virtual int getWidth() const {
106  return m_server->getWidth() >> m_xshift;
107  }
108  inline virtual int getHeight() const {
109  // If there is no y-shift, the server's height (based on its
110  // fftsize/2 + 1) is correct. If there is a shift, then the
111  // server is using a larger fft size than we want, so we shift
112  // it right as many times as necessary, but then we need to
113  // re-add the "+1" part (because ((fftsize*2)/2 + 1) / 2 !=
114  // fftsize/2 + 1).
115  return (m_server->getHeight() >> m_yshift) + (m_yshift > 0 ? 1 : 0);
116  }
117  virtual float getValueAt(int x, int y) const {
118  return const_cast<FFTModel *>(this)->getMagnitudeAt(x, y);
119  }
120  virtual bool isOK() const {
121  return m_server && m_server->getModel();
122  }
123  virtual int getStartFrame() const {
124  return 0;
125  }
126  virtual int getEndFrame() const {
127  return getWidth() * getResolution() + getResolution();
128  }
129  virtual int getSampleRate() const;
130  virtual int getResolution() const {
131  return m_server->getWindowIncrement() << m_xshift;
132  }
133  virtual int getYBinCount() const {
134  return getHeight();
135  }
136  virtual float getMinimumLevel() const {
137  return 0.f; // Can't provide
138  }
139  virtual float getMaximumLevel() const {
140  return 1.f; // Can't provide
141  }
142  virtual Column getColumn(int x) const;
143  virtual QString getBinName(int n) const;
144 
145  virtual bool shouldUseLogValueScale() const {
146  return true; // Although obviously it's up to the user...
147  }
148 
154  virtual bool estimateStableFrequency(int x, int y, float &frequency);
155 
157  {
161  };
162 
163  typedef std::set<int> PeakLocationSet; // bin
164  typedef std::map<int, float> PeakSet; // bin -> freq
165 
170  virtual PeakLocationSet getPeaks(PeakPickType type, int x,
171  int ymin = 0, int ymax = 0);
172 
176  virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
177  int ymin = 0, int ymax = 0);
178 
179  virtual int getCompletion() const { return m_server->getFillCompletion(); }
180  virtual QString getError() const { return m_server->getError(); }
181 
182  virtual Model *clone() const;
183 
184  virtual void suspend() { m_server->suspend(); }
185  virtual void suspendWrites() { m_server->suspendWrites(); }
186  virtual void resume() { m_server->resume(); }
187 
188  QString getTypeName() const { return tr("FFT"); }
189 
190 public slots:
192 
193 private:
194  FFTModel(const FFTModel &); // not implemented
195  FFTModel &operator=(const FFTModel &); // not implemented
196 
198  int m_xshift;
199  int m_yshift;
200 
202  int, WindowType, int, int, int,
203  bool, StorageAdviser::Criteria, int);
204 
205  int getPeakPickWindowSize(PeakPickType type, int sampleRate,
206  int bin, float &percentile) const;
207 
208  int getYRatio() {
209  int ys = m_yshift;
210  int r = 1;
211  while (ys) { --ys; r <<= 1; }
212  return r;
213  }
214 };
215 
216 #endif
virtual int getWidth() const
Return the number of columns of bins in the model.
Definition: FFTModel.h:105
virtual int getSampleRate() const
Return the frame rate in frames per second.
Definition: FFTModel.cpp:163
virtual bool estimateStableFrequency(int x, int y, float &frequency)
Calculate an estimated frequency for a stable signal in this bin, using phase unwrapping.
Definition: FFTModel.cpp:208
int getFillExtent() const
Definition: FFTModel.h:101
bool getNormalizedMagnitudesAt(int x, float *values, int minbin=0, int count=0, int step=1)
FFTDataServer * getServer(const DenseTimeValueModel *, int, WindowType, int, int, int, bool, StorageAdviser::Criteria, int)
Definition: FFTModel.cpp:105
virtual int getStartFrame() const
Return the first audio frame spanned by the model.
Definition: FFTModel.h:123
QString getError() const
An implementation of DenseThreeDimensionalModel that makes FFT data derived from a DenseTimeValueMode...
Definition: FFTModel.h:33
float getMaximumMagnitudeAt(int x)
const DenseTimeValueModel * getModel() const
Definition: FFTDataServer.h:70
virtual void suspendWrites()
Definition: FFTModel.h:185
virtual float getMinimumLevel() const
Return the minimum permissible value in each bin.
Definition: FFTModel.h:136
float getMagnitudeAt(int x, int y)
bool getNormalizedMagnitudesAt(int x, float *values, int minbin=0, int count=0)
Definition: FFTModel.h:91
FFTModel & operator=(const FFTModel &)
Any bin exceeding its immediate neighbours.
Definition: FFTModel.h:159
float getMaximumMagnitudeAt(int x)
Definition: FFTModel.h:75
WindowType
Definition: Window.h:27
virtual int getYBinCount() const
Definition: FFTModel.h:133
virtual int getCompletion() const
Definition: FFTModel.h:179
int getWindowIncrement() const
Definition: FFTDataServer.h:74
float getNormalizedMagnitudeAt(int x, int y)
Definition: FFTModel.h:72
bool getMagnitudesAt(int x, float *values, int minbin=0, int count=0, int step=1)
virtual Model * clone() const
Return a copy of this model.
Definition: FFTModel.cpp:441
PeakPickType
Definition: FFTModel.h:156
bool getPhasesAt(int x, float *values, int minbin=0, int count=0)
Definition: FFTModel.h:94
bool getMagnitudesAt(int x, float *values, int minbin=0, int count=0)
Definition: FFTModel.h:88
void getValuesAt(int x, int y, float &real, float &imaginary)
Definition: FFTModel.h:81
float getMagnitudeAt(int x, int y)
Definition: FFTModel.h:69
float getPhaseAt(int x, int y)
Definition: FFTModel.h:78
virtual float getValueAt(int x, int y) const
Get the single data point from the n'th bin of the given column.
Definition: FFTModel.h:117
int getFillExtent() const
float getPhaseAt(int x, int y)
int m_xshift
Definition: FFTModel.h:198
FFTModel(const DenseTimeValueModel *model, int channel, WindowType windowType, int windowSize, int windowIncrement, int fftSize, bool polar, StorageAdviser::Criteria criteria=StorageAdviser::NoCriteria, int fillFromColumn=0)
Construct an FFT model derived from the given DenseTimeValueModel, with the given window parameters a...
Definition: FFTModel.cpp:31
bool getValuesAt(int x, float *reals, float *imaginaries, int minbin=0, int count=0)
Definition: FFTModel.h:97
int getHeight() const
Definition: FFTDataServer.h:79
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
bool isColumnAvailable(int x) const
Return true if there are data available for the given column.
Definition: FFTModel.h:84
int getPeakPickWindowSize(PeakPickType type, int sampleRate, int bin, float &percentile) const
Definition: FFTModel.cpp:376
virtual Column getColumn(int x) const
Get data from the given column of bin values.
Definition: FFTModel.cpp:169
bool isColumnReady(int x)
int getFillCompletion() const
virtual void resume()
Definition: FFTModel.h:186
virtual int getResolution() const
Return the number of sample frames covered by each column of bins.
Definition: FFTModel.h:130
QString getTypeName() const
Return the type of the model.
Definition: FFTModel.h:188
void getValuesAt(int x, int y, float &real, float &imaginary)
Base class for models containing dense two-dimensional data (value against time).
virtual bool isOK() const
Return true if the model was constructed successfully.
Definition: FFTModel.h:120
void sourceModelAboutToBeDeleted()
Definition: FFTModel.cpp:92
~FFTModel()
Definition: FFTModel.cpp:86
virtual QString getBinName(int n) const
Get the name of a given bin (i.e.
Definition: FFTModel.cpp:199
Peaks picked using sliding median window.
Definition: FFTModel.h:160
float getNormalizedMagnitudeAt(int x, int y)
virtual void suspend()
Definition: FFTModel.h:184
virtual int getHeight() const
Return the number of bins in each column.
Definition: FFTModel.h:108
virtual PeakSet getPeakFrequencies(PeakPickType type, int x, int ymin=0, int ymax=0)
Return locations and estimated stable frequencies of peak bins.
Definition: FFTModel.cpp:397
std::map< int, float > PeakSet
Definition: FFTModel.h:164
virtual float getMaximumLevel() const
Return the maximum permissible value in each bin.
Definition: FFTModel.h:139
int m_yshift
Definition: FFTModel.h:199
FFTDataServer * m_server
Definition: FFTModel.h:197
int getWidth() const
Definition: FFTDataServer.h:78
virtual int getEndFrame() const
Return the last audio frame spanned by the model.
Definition: FFTModel.h:126
virtual bool shouldUseLogValueScale() const
Estimate whether a logarithmic scale might be appropriate for the value scale.
Definition: FFTModel.h:145
int getYRatio()
Definition: FFTModel.h:208
bool getPhasesAt(int x, float *values, int minbin=0, int count=0, int step=1)
virtual PeakLocationSet getPeaks(PeakPickType type, int x, int ymin=0, int ymax=0)
Return locations of peak bins in the range [ymin,ymax].
Definition: FFTModel.cpp:250
std::set< int > PeakLocationSet
Definition: FFTModel.h:163
virtual QString getError() const
Definition: FFTModel.h:180