Nsound  0.9.4
Public Member Functions | Public Attributes | Protected Attributes | List of all members
Nsound::FFTChunk Class Reference

Results of performing an FFT are stored in this class. More...

#include <Nsound/FFTChunk.h>

Public Member Functions

 FFTChunk (uint32 size=32, uint32 sample_rate=44100, uint32 original_size=0)
 
 FFTChunk (const FFTChunk &copy)
 
 ~FFTChunk ()
 
Buffer getFrequencyAxis () const
 
Buffer getReal () const
 
Buffer getImaginary () const
 
Buffer getMagnitude () const
 
uint32 getOriginalSize () const
 
Buffer getPhase () const
 
boolean isPolar () const
 
FFTChunkoperator= (const FFTChunk &rhs)
 
void plot (const std::string &title="", boolean dB=true, boolean show_phase=false) const
 
void setCartesian (const Buffer &real, const Buffer &imaginary)
 Sets up an FFTChunk to use the provided real & imaginary. More...
 
void setPolar (const Buffer &magnitude, const Buffer &phase)
 Sets up an FFTChunk to use the provided magnitude & phase. More...
 
void toPolar ()
 convertes the real & imaginary unit to plor form: magnitude & phase More...
 
void toCartesian ()
 convertes the magnitude & phase to cartesian form: real & imaginary More...
 
uint32 getSampleRate () const
 

Public Attributes

Bufferreal_
 
Bufferimag_
 

Protected Attributes

uint32 sample_rate_
 
uint32 original_size_
 
boolean is_polar_
 

Detailed Description

Results of performing an FFT are stored in this class.

Definition at line 49 of file FFTChunk.h.

Constructor & Destructor Documentation

FFTChunk::FFTChunk ( uint32  size = 32,
uint32  sample_rate = 44100,
uint32  original_size = 0 
)

Definition at line 46 of file FFTChunk.cc.

References imag_, original_size_, and real_.

47  :
48  real_(NULL),
49  imag_(NULL),
50  sample_rate_(sample_rate),
51  original_size_(original_size),
52  is_polar_(false)
53 {
54  real_ = new Buffer(size);
55  imag_ = new Buffer(size);
56 
57  if(original_size_ == 0)
58  {
59  original_size_ = size;
60  }
61 }
Buffer * imag_
Definition: FFTChunk.h:106
uint32 original_size_
Definition: FFTChunk.h:113
boolean is_polar_
Definition: FFTChunk.h:114
uint32 sample_rate_
Definition: FFTChunk.h:108
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
FFTChunk::FFTChunk ( const FFTChunk copy)

Definition at line 65 of file FFTChunk.cc.

66  :
67  real_(new Buffer(copy.real_->getLength())),
68  imag_(new Buffer(copy.real_->getLength())),
71  is_polar_(copy.is_polar_)
72 {
73  // Call operator =
74  *this = copy;
75 }
Buffer * imag_
Definition: FFTChunk.h:106
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
uint32 original_size_
Definition: FFTChunk.h:113
boolean is_polar_
Definition: FFTChunk.h:114
uint32 sample_rate_
Definition: FFTChunk.h:108
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
FFTChunk::~FFTChunk ( )

Definition at line 79 of file FFTChunk.cc.

References imag_, and real_.

80 {
81  delete real_;
82  delete imag_;
83 }
Buffer * imag_
Definition: FFTChunk.h:106
Buffer * real_
Definition: FFTChunk.h:105

Member Function Documentation

Buffer FFTChunk::getFrequencyAxis ( ) const

Definition at line 109 of file FFTChunk.cc.

References Nsound::Generator::drawLine(), Nsound::Buffer::getLength(), real_, and sample_rate_.

Referenced by plot().

110 {
111  // Create the x axis based on Hz.
112 
113  uint32 chunk_size = real_->getLength();
114 
115  uint32 n_samples = chunk_size / 2 + 1;
116 
117  float64 x_step = static_cast<float64>(sample_rate_)
118  / static_cast<float64>(chunk_size);
119 
120  Generator gen(1);
121 
122  return gen.drawLine(n_samples, 0.0, x_step * n_samples);
123 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
uint32 sample_rate_
Definition: FFTChunk.h:108
Buffer * real_
Definition: FFTChunk.h:105
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
Buffer FFTChunk::getReal ( ) const

Definition at line 127 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), imag_, is_polar_, real_, and Nsound::Buffer::subbuffer().

Referenced by FFTransform_UnitTest().

128 {
129  if(!is_polar_)
130  {
131  return real_->subbuffer(0, real_->getLength() / 2);
132  }
133 
134  uint32 n_samples = real_->getLength();
135 
136  Buffer out(n_samples / 2);
137 
138  for(uint32 i = 0; i < n_samples / 2; ++i)
139  {
140  // mag * cos(phase)
141  out << (*real_)[i] * std::cos((*imag_)[i]);
142  }
143 
144  return out;
145 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
Buffer * imag_
Definition: FFTChunk.h:106
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer FFTChunk::getImaginary ( ) const

Definition at line 87 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), imag_, is_polar_, real_, and Nsound::Buffer::subbuffer().

Referenced by FFTransform_UnitTest().

88 {
89  if(!is_polar_)
90  {
91  return imag_->subbuffer(0, imag_->getLength() / 2);
92  }
93 
94  Buffer out;
95 
96  uint32 n_samples = real_->getLength();
97 
98  for(uint32 i = 0; i < n_samples / 2; ++i)
99  {
100  // mag * sin(phase)
101  out << (*real_)[i] * std::sin((*imag_)[i]);
102  }
103 
104  return out;
105 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
Buffer * imag_
Definition: FFTChunk.h:106
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer FFTChunk::getMagnitude ( ) const

Definition at line 149 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), is_polar_, real_, and Nsound::Buffer::subbuffer().

Referenced by plot().

150 {
151  uint32 n_samples = real_->getLength();
152 
153  if(is_polar_)
154  {
155  return real_->subbuffer(0, n_samples / 2 + 1);
156  }
157 
158  float64 x = 0.0;
159  float64 y = 0.0;
160 
161  Buffer out(n_samples / 2 + 1);
162 
163  for(uint32 i = 0; i < n_samples / 2 + 1; ++i)
164  {
165  x = (*real_)[i];
166  y = (*imag_)[i];
167  out << std::sqrt(x*x + y*y);
168  }
169 
170  return out;
171 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
uint32 Nsound::FFTChunk::getOriginalSize ( ) const
inline

Definition at line 72 of file FFTChunk.h.

References original_size_.

72 { return original_size_; };
uint32 original_size_
Definition: FFTChunk.h:113
Buffer FFTChunk::getPhase ( ) const

Definition at line 175 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), imag_, is_polar_, M_PI, real_, and Nsound::Buffer::subbuffer().

Referenced by plot().

176 {
177  if(is_polar_)
178  {
179  return imag_->subbuffer(0, imag_->getLength() / 2 + 1);
180  }
181 
182  uint32 n_samples = real_->getLength();
183 
184  float64 r = 0.0;
185  float64 i = 0.0;
186  float64 phase = 0.0;
187 
188  Buffer out(n_samples / 2 + 1);
189 
190  for(uint32 j = 0; j < n_samples / 2 + 1; ++j)
191  {
192  r = (*real_)[j];
193  i = (*imag_)[j];
194 
195  //if(r == 0.0) r = 1e-20;
196 
197  phase = std::atan(i/r);
198 
199  if(r < 0.0)
200  {
201  if(i < 0.0) phase -= M_PI;
202  else phase += M_PI;
203  }
204 
205  out << phase;
206  }
207 
208  return out;
209 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
#define M_PI
Definition: Nsound.h:121
Buffer * imag_
Definition: FFTChunk.h:106
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
boolean Nsound::FFTChunk::isPolar ( ) const
inline

Definition at line 78 of file FFTChunk.h.

References is_polar_.

Referenced by Nsound::FFTransform::ifft().

78 { return is_polar_; };
boolean is_polar_
Definition: FFTChunk.h:114
FFTChunk & FFTChunk::operator= ( const FFTChunk rhs)

Definition at line 214 of file FFTChunk.cc.

References imag_, is_polar_, original_size_, real_, and sample_rate_.

215 {
216  if(this == &rhs)
217  {
218  return *this;
219  }
220 
221  *real_ = *rhs.real_;
222  *imag_ = *rhs.imag_;
225  is_polar_ = rhs.is_polar_;
226 
227  return *this;
228 }
Buffer * imag_
Definition: FFTChunk.h:106
uint32 original_size_
Definition: FFTChunk.h:113
boolean is_polar_
Definition: FFTChunk.h:114
uint32 sample_rate_
Definition: FFTChunk.h:108
Buffer * real_
Definition: FFTChunk.h:105
void FFTChunk::plot ( const std::string &  title = "",
boolean  dB = true,
boolean  show_phase = false 
) const

Definition at line 232 of file FFTChunk.cc.

References Nsound::Buffer::dB(), Nsound::Plotter::figure(), getFrequencyAxis(), Nsound::Buffer::getLength(), getMagnitude(), getPhase(), Nsound::Plotter::plot(), real_, Nsound::Buffer::subbuffer(), Nsound::Plotter::subplot(), Nsound::Plotter::title(), Nsound::Plotter::xlabel(), and Nsound::Plotter::ylabel().

236 {
237  //~ // Create the x axis based on Hz.
238  //~
239  //~ uint32 chunk_size = real_->getLength();
240 
241  uint32 n_samples = real_->getLength() / 2 + 1;
242 
243  //~
244  //~ float64 x_step = (1.0 / (static_cast<float64>(chunk_size) / 2.0))
245  //~ * (static_cast<float64>(sample_rate_) / 2.0);
246  //~
247  //~ Buffer x;
248  //~
249  //~ float64 tx = 0.0;
250  //~
251  //~ for(uint32 i = 0; i < n_samples; ++i)
252  //~ {
253  //~ x << tx;
254  //~ tx += x_step;
255  //~ }
256 
257  Buffer x = getFrequencyAxis();
258 
259  // Calculate the magnitude for the signal:
260  //
261  // magnitude = sqrt(x*x + y*y);
262  //
263  Buffer magnitude = getMagnitude();
264 
265  Plotter pylab;
266 
267  pylab.figure();
268 
269  if(show_phase)
270  {
271  pylab.subplot(2, 1, 1);
272  }
273 
274  if(title.length() <= 0)
275  {
276  pylab.title("Magnitude");
277  }
278  else
279  {
280  pylab.title(title);
281  }
282 
283  if(dB)
284  {
285  magnitude.dB();
286  pylab.ylabel("Magnitude dB");
287  }
288  else
289  {
290  pylab.ylabel("Magnitude");
291  }
292 
293  pylab.plot(x,magnitude.subbuffer(0,n_samples));
294 
295  if(show_phase)
296  {
297  Buffer phase = getPhase().subbuffer(0,n_samples);
298 
299  pylab.subplot(2, 1, 2);
300  pylab.title("Phase");
301  pylab.plot(x,phase);
302  pylab.ylabel("Phase Radians");
303  }
304 
305  pylab.xlabel("Frequency Hz");
306 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
Buffer getFrequencyAxis() const
Definition: FFTChunk.cc:109
void dB()
Modifies the Buffer so each sample is converted to dB, 20 * log10(sample).
Definition: Buffer.cc:567
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer getMagnitude() const
Definition: FFTChunk.cc:149
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
Definition: Plotter.cc:1031
Buffer getPhase() const
Definition: FFTChunk.cc:175
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
void FFTChunk::setCartesian ( const Buffer real,
const Buffer imaginary 
)

Sets up an FFTChunk to use the provided real & imaginary.

Definition at line 310 of file FFTChunk.cc.

References imag_, is_polar_, and real_.

Referenced by FFTransform_UnitTest().

311 {
312  Buffer zeros = 0.0 * real;
313 
314  *real_ = 2.0 * real;
315  *real_ << zeros;
316 
317  *imag_ = 2.0 * img;
318  *imag_ << zeros;
319 
320  is_polar_ = false;
321 }
Buffer * imag_
Definition: FFTChunk.h:106
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
void FFTChunk::setPolar ( const Buffer magnitude,
const Buffer phase 
)

Sets up an FFTChunk to use the provided magnitude & phase.

Definition at line 325 of file FFTChunk.cc.

References imag_, is_polar_, and real_.

Referenced by FFTransform_UnitTest().

326 {
327  Buffer zeros = 0.0 * mag;
328 
329  *real_ = 2.0 * mag;
330  *real_ << zeros;
331 
332  *imag_ = 2.0 * phase;
333  *imag_ << zeros;
334 
335  is_polar_ = true;
336 }
Buffer * imag_
Definition: FFTChunk.h:106
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
A Buffer for storing audio samples.
Definition: Buffer.h:60
void FFTChunk::toPolar ( )

convertes the real & imaginary unit to plor form: magnitude & phase

Definition at line 362 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), is_polar_, M_PI, and real_.

363 {
364  if(is_polar_) return;
365 
366  uint32 n_samples = real_->getLength();
367 
368  float64 r = 0.0;
369  float64 i = 0.0;
370 
371  for(uint32 j = 0; j < n_samples; ++j)
372  {
373  r = (*real_)[j];
374  i = (*imag_)[j];
375 
376  (*real_)[j] = std::sqrt(r*r + i*i);
377  (*imag_)[j] = std::atan(i / r);
378 
379  if(i < 0.0 && r < 0.0) (*imag_)[j] -= M_PI;
380  if(i >= 0.0 && r < 0.0) (*imag_)[j] += M_PI;
381  }
382 
383  is_polar_ = true;
384 }
unsigned int uint32
Definition: Nsound.h:153
#define M_PI
Definition: Nsound.h:121
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
void FFTChunk::toCartesian ( )

convertes the magnitude & phase to cartesian form: real & imaginary

Definition at line 340 of file FFTChunk.cc.

References Nsound::Buffer::getLength(), is_polar_, and real_.

Referenced by Nsound::FFTransform::ifft().

341 {
342  if(!is_polar_) return;
343 
344  uint32 n_samples = real_->getLength();
345 
346  float64 m = 0.0;
347  float64 p = 0.0;
348 
349  for(uint32 j = 0; j < n_samples; ++j)
350  {
351  m = (*real_)[j];
352  p = (*imag_)[j];
353  (*real_)[j] = m * std::cos(p);
354  (*imag_)[j] = m * std::sin(p);
355  }
356 
357  is_polar_ = false;
358 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
boolean is_polar_
Definition: FFTChunk.h:114
Buffer * real_
Definition: FFTChunk.h:105
uint32 Nsound::FFTChunk::getSampleRate ( ) const
inline

Definition at line 108 of file FFTChunk.h.

108 {return sample_rate_;};
uint32 sample_rate_
Definition: FFTChunk.h:108

Member Data Documentation

Buffer* Nsound::FFTChunk::real_
Buffer* Nsound::FFTChunk::imag_
uint32 Nsound::FFTChunk::sample_rate_
protected

Definition at line 108 of file FFTChunk.h.

Referenced by getFrequencyAxis(), and operator=().

uint32 Nsound::FFTChunk::original_size_
protected

Definition at line 113 of file FFTChunk.h.

Referenced by FFTChunk(), getOriginalSize(), and operator=().

boolean Nsound::FFTChunk::is_polar_
protected

The documentation for this class was generated from the following files: