Nsound  0.9.4
test_fft.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: test_fft.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2008-Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/NsoundAll.h>
30 
31 #include "Test.h"
32 
33 #include <stdlib.h>
34 #include <cmath>
35 #include <iostream>
36 #include <sstream>
37 
38 using namespace Nsound;
39 
40 using std::cerr;
41 using std::cout;
42 using std::endl;
43 using std::flush;
44 
45 static const char * THIS_FILE = "test_fft.cc";
46 
47 int main(int argc, char ** argv)
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 }
140 
141 
void plot(boolean show_fc=true, boolean show_phase=false)
unsigned int uint32
Definition: Nsound.h:153
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
Buffer ifft(const FFTChunkVector &input) const
Peforms an inverse FFT on each FFTChunk and concatenates the output.
Definition: FFTransform.cc:207
void plot(boolean show_fc=true, boolean show_phase=false)
void plot(boolean show_fc=true, boolean show_phase=false)
#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
void plot(boolean show_fc=true, boolean show_phase=false)
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
void plot(boolean show_fc=true, boolean show_phase=false)
static const char * THIS_FILE
Definition: test_fft.cc:45
A class for filtering audio in the frequecy domain.
void plot(boolean show_fc=true, boolean show_phase=false)
void plot(boolean show_fc=true, boolean show_phase=false)
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
Buffer fft(const Buffer &time_domain) const
Transforms the time_domain signal and calculates the FFT.
Definition: FFTransform.cc:50
void plot(boolean show_fc=true, boolean show_phase=false)
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
int main(int argc, char **argv)
Definition: test_fft.cc:47
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
DOXME.
Definition: Sine.h:43