Nsound  0.9.4
Functions
test_iir_ga.cc File Reference
#include <Nsound/NsoundAll.h>
#include <cmath>
#include <iostream>
#include <sstream>
Include dependency graph for test_iir_ga.cc:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 42 of file test_iir_ga.cc.

References Nsound::FilterIIR::designFrequencyResponse(), Nsound::Generator::drawLine(), Nsound::Plotter::figure(), Nsound::Filter::getFrequencyAxis(), Nsound::Filter::getFrequencyResponse(), Nsound::Buffer::getLength(), Nsound::Buffer::getMax(), Nsound::Filter::plot(), Nsound::Plotter::plot(), Nsound::Plotter::show(), sr, Nsound::Plotter::text(), Nsound::Plotter::title(), Nsound::Plotter::xlabel(), Nsound::Plotter::xlim(), Nsound::Plotter::ylabel(), and Nsound::Plotter::ylim().

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 
95  Buffer fr = f.getFrequencyResponse();
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 }
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
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
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
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
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
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