Nsound  0.9.4
Functions | Variables
test_fft.cc File Reference
#include <Nsound/NsoundAll.h>
#include "Test.h"
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <sstream>
Include dependency graph for test_fft.cc:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Variables

static const char * THIS_FILE = "test_fft.cc"
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 47 of file test_fft.cc.

References Nsound::Buffer::abs(), Nsound::FFTransform::fft(), Nsound::Generator::generate(), Nsound::Buffer::getMax(), Nsound::FFTransform::ifft(), Nsound::FilterHighPassIIR::plot(), Nsound::FilterLowPassIIR::plot(), Nsound::FilterLowPassFIR::plot(), Nsound::FilterHighPassFIR::plot(), Nsound::FilterBandPassFIR::plot(), Nsound::FilterBandRejectFIR::plot(), Nsound::FilterBandPassIIR::plot(), Nsound::FilterBandRejectIIR::plot(), SAMPLE_RATE, Nsound::Plotter::show(), TEST_ERROR_HEADER, TEST_HEADER, Nsound::Tic(), Nsound::Toc(), Nsound::Plotter::xlim(), and Nsound::Plotter::ylim().

48 {
49  uint32 SAMPLE_RATE = 44100;
50 
51  Sine sine(SAMPLE_RATE);
52 
53  Buffer input = sine.generate(2.0, 4000.0)
54  + sine.generate(2.0, 8000.0)
55  + sine.generate(2.0, 12000.0);
56 
57  FFTransform fft(SAMPLE_RATE);
58 
59  Tic();
60  cout << TEST_HEADER
61  << "FFTransform::fft(), FFTransform::ifft() ... " << flush;
62 
63  FFTChunkVector vec = fft.fft(input, 2048);
64 
65  // Call the inverse fft.
66  Buffer ifft = fft.ifft(vec);
67 
68  Buffer diff = input - ifft;
69 
70  diff.abs();
71 
72  if(diff.getMax() > 0.00005)
73  {
74  cerr << TEST_ERROR_HEADER
75  << "fft() - ifft() > 0.00005, max(diff) = "
76  << diff.getMax()
77  << endl;
78  exit(1);
79  }
80 
81  cout << Toc() << " seconds: SUCCESS" << endl;
82 
83  float64 fl = 440;
84  float64 fh = fl * 2;
85 
86  FilterLowPassFIR f1(SAMPLE_RATE, 512, fl);
87  FilterHighPassFIR f2(SAMPLE_RATE, 512, fl);
88  FilterBandPassFIR f3(SAMPLE_RATE, 512, fl, fh);
89  FilterBandRejectFIR f4(SAMPLE_RATE, 512, fl, fh);
90 
91  FilterLowPassIIR f11(SAMPLE_RATE, 6, fl, 0.01);
92  FilterHighPassIIR f22(SAMPLE_RATE, 6, fl, 0.01);
93  FilterBandPassIIR f33(SAMPLE_RATE, 6, fl, fh, 0.01);
94  FilterBandRejectIIR f44(SAMPLE_RATE, 6, fl, fh, 0.01);
95 
96  Tic();
97  cout << TEST_HEADER
98  << "Plotting Frequecy Responses of Filters ... " << flush;
99 
100  Plotter pylab;
101 
102  f4.plot();
103  pylab.xlim(0, fh * 2);
104  pylab.ylim(-40, 5);
105 
106  f44.plot();
107  pylab.xlim(0, fh * 2);
108  pylab.ylim(-40, 5);
109 
110  f3.plot();
111  pylab.xlim(0,fh * 2);
112  pylab.ylim(-40,5);
113 
114  f33.plot();
115  pylab.xlim(0,fh * 2);
116  pylab.ylim(-40,5);
117 
118  f2.plot();
119  pylab.xlim(0,fh * 2);
120  pylab.ylim(-40,5);
121 
122  f22.plot();
123  pylab.xlim(0,fh * 2);
124  pylab.ylim(-40,5);
125 
126  f1.plot();
127  pylab.xlim(0,fh * 2);
128  pylab.ylim(-40,5);
129 
130  f11.plot();
131  pylab.xlim(0,fh * 2);
132  pylab.ylim(-40,5);
133 
134  cout << Toc() << " seconds" << endl << flush;
135 
136  Plotter::show();
137 
138  return 0;
139 }
unsigned int uint32
Definition: Nsound.h:153
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
#define TEST_HEADER
Definition: Test.h:45
void xlim(const float64 &xmin, const float64 &xmax)
Sets the limit for the x & y axis.
Definition: Plotter.cc:389
double float64
Definition: Nsound.h:146
A class for filtering audio in the frequecy domain.
uint64 SAMPLE_RATE
Definition: example3.cc:25
#define TEST_ERROR_HEADER
Definition: Test.h:49
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
A class for filtering audio in the frequecy domain.
void abs()
Modifies the Buffer by making any negative value positive.
Definition: Buffer.cc:119
A Class that performes the Fast Fouier Transfrom on a Buffer.
Definition: FFTransform.h:57
A Buffer for storing audio samples.
Definition: Buffer.h:60
Nsound::float64 Toc()
Definition: TicToc.cc:42
void Tic()
Definition: TicToc.cc:37
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
DOXME.
Definition: Sine.h:43

Variable Documentation

const char* THIS_FILE = "test_fft.cc"
static

Definition at line 45 of file test_fft.cc.