Nsound  0.9.4
test_iir_ga.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: test_iir_ga.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2009 to 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 <cmath>
32 #include <iostream>
33 #include <sstream>
34 
35 using namespace Nsound;
36 
37 using std::cerr;
38 using std::cout;
39 using std::endl;
40 using std::flush;
41 
42 int main(int argc, char ** argv)
43 {
44  Plotter pylab;
45 
46  float64 sr = 44100.0;
47 
48  FilterIIR f(sr, 15);
49 
50  Generator g(256);
51 
52  Buffer ref_response;
53 
54  // custom response
55  ref_response << g.drawLine(0.25, 0.0, 0.8)
56  << g.drawLine(0.25, 0.8, 0.8)
57  << g.drawLine(0.15, 0.8, 1.4)
58  << g.drawLine(0.15, 1.4, 1.4)
59  << g.drawLine(0.20, 1.4, 0.0);
60 
61  // Some round off error will cause f_response
62  while( ref_response.getLength() < 256 ) ref_response << 0.0;
63 
64  Buffer ref_freq_axis = g.drawLine(1.0, 0.0, sr/2.0);
65 
66 //~ // Band Pass
67 //~ f_response << g.drawLine(0.2, 0.0, 0.0)
68 //~ << g.drawLine(0.2, 1.0, 1.0)
69 //~ << g.drawLine(0.6, 0.0, 0.0);
70 
71  pylab.plot(ref_freq_axis, ref_response);
72  pylab.title("desired frequency response");
73 
74  Buffer evolution = f.designFrequencyResponse(ref_response, 0.001, 10000);
75 
76  cout << "f = " << endl << f << endl;
77 
78  pylab.figure();
79  pylab.plot(evolution);
80 //~ pylab.ylim(0.0, 1.0);
81 
82  pylab.xlabel("Generation");
83  pylab.ylabel("Error");
84 
85  int32 n_generations = evolution.getLength();
86  float64 error = evolution[n_generations - 1];
87 
88  char buffer[1024];
89  sprintf(buffer, "%5.2f RMS error in %d generations",
90  error,
91  n_generations);
92 
93  pylab.title(std::string(buffer));
94 
96  Buffer faxis = f.getFrequencyAxis();
97 
98  pylab.figure();
99  pylab.plot(ref_freq_axis, ref_response);
100  pylab.plot(faxis, fr);
101  pylab.xlabel("Frequency Hz");
102  pylab.ylabel("Frequency Response");
103 
104  pylab.ylim(0.0, ref_response.getMax() * 1.10);
105 
106  sprintf(buffer, "Generation %06d", n_generations);
107 
108  pylab.title(std::string(buffer));
109 
110  pylab.xlim(0.0, faxis[fr.getLength() - 1]);
111 
112  sprintf(buffer, "Error = %.3f RMS", error);
113 
114  pylab.text(
115  faxis[static_cast<uint32>(faxis.getLength() * 0.5)],
116  0.25,
117  std::string(buffer));
118 
119  f.plot();
120 
121  Plotter::show();
122 
123 
124  return 0;
125 }
126 
127 
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
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
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
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Buffer designFrequencyResponse(const Buffer &frequency_response, const float64 &max_error=0.01, const int32 max_iterations=1000)
Designs a filter kernel using a genetic algorithm that trys to match the provided frequency response...
Definition: FilterIIR.cc:127
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
Buffer getFrequencyAxis(const uint32 n_fft=8192)
Definition: Filter.cc:185
void text(const float64 &x, const float64 &y, const std::string &text, const std::string &kwargs="")
Adds text at the x, y data point.
Definition: Plotter.cc:1092
int main(int argc, char **argv)
Definition: test_iir_ga.cc:42
void plot(boolean show_phase=false)
Definition: Filter.cc:262
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer getFrequencyResponse(const uint32 n_fft=8192)
Definition: Filter.cc:210
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
float64 sr
Definition: example3.cc:24