Nsound  0.9.4
FilterLeastSquaresFIR_UnitTest.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterLeastSquaresFIR_UnitTest.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006 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/AudioStream.h>
30 #include <Nsound/Buffer.h>
32 #include <Nsound/Plotter.h>
33 #include <Nsound/Sine.h>
34 #include <Nsound/Wavefile.h>
35 
36 #include "UnitTest.h"
37 
38 #include <cmath>
39 #include <stdlib.h>
40 #include <iostream>
41 
42 using namespace Nsound;
43 
44 using std::cerr;
45 using std::cout;
46 using std::endl;
47 using std::flush;
48 
49 // The __FILE__ macro includes the path, I don't want the whole path.
50 #define THIS_FILE "FilterLeastSquaresFIR_UnitTest.cc"
51 
52 #define GAMMA 1.5e-14
53 
55 {
56  cout << endl << THIS_FILE;
57 
60 
61  float64 sr = 1000.0;
62 
63  // Create a low pass least squares FIR filter
64 
65  Buffer freqs;
66  Buffer amplitude;
67 
68  freqs << 0.0 << 100.0 << 100.0 << 500.0;
69  amplitude << 1.0 << 1.0 << 0.0 << 0.0;
70 
71  FilterLeastSquaresFIR f(sr, 1025, freqs, amplitude);
72 
73  Buffer noise("gold/Filter_noise.wav");
74 
75  cout << TEST_HEADER << "Testing FilterLeastSquaresFIR::filter(input) ...";
76 
77  Buffer data;
78 
79  data = f.filter(noise);
80 
81  // Create the gold file
82  //~ data >> "gold/FilterLeastSquaresFIR_out1.wav";
83 
84  Buffer gold("gold/FilterLeastSquaresFIR_out1.wav");
85 
86  Buffer diff = data - gold;
87 
88  diff.abs();
89 
90  if(diff.getMax() > GAMMA)
91  {
92  cerr << TEST_ERROR_HEADER
93  << "Output did not match gold file!"
94  << endl
95  << flush;
96 
97  diff.plot("data - gold");
98  data.plot("data");
99  gold.plot("gold");
100 
101  Plotter::show();
102 
103  exit(1);
104  }
105 
106  cout << SUCCESS;
107 
108  // Repeated to test that reset() is being called.
109  cout << TEST_HEADER << "Testing FilterLeastSquaresFIR::filter(input) ..."
110  << flush;
111 
112  data = f.filter(noise);
113 
114  diff = data - gold;
115  diff.abs();
116 
117  if(diff.getMax() > GAMMA)
118  {
119  cerr << TEST_ERROR_HEADER
120  << "Output did not match gold file!"
121  << endl;
122 
123  diff.plot("data - gold");
124  data.plot("data");
125  gold.plot("gold");
126 
127  Plotter::show();
128 
129  exit(1);
130  }
131 
132  cout << SUCCESS;
133 
134  // Create a high pass least squares FIR filter
135 
136  freqs = Buffer(4);
137  amplitude = Buffer(4);
138 
139  freqs << 0.0 << 100.0 << 100.0 << 500.0;
140  amplitude << 0.0 << 0.0 << 1.0 << 1.0;
141 
142  f.makeKernel(freqs, amplitude);
143 
144  cout << TEST_HEADER << "Testing FilterLeastSquaresFIR::filter(input) ...";
145 
146  data = f.filter(0.9 * noise);
147 
148  // Create the gold file
149  //~ data >> "gold/FilterLeastSquaresFIR_out2.wav";
150 
151  gold = Buffer("gold/FilterLeastSquaresFIR_out2.wav");
152 
153  diff = data - gold;
154  diff.abs();
155 
156  if(diff.getMax() > GAMMA)
157  {
158  cerr << TEST_ERROR_HEADER
159  << "Output did not match gold file!"
160  << endl;
161 
162  diff.plot("data - gold");
163  data.plot("data");
164  gold.plot("gold");
165 
166  Plotter::show();
167 
168  exit(1);
169  }
170 
171  cout << SUCCESS;
172 
173  // Repeated to test that reset() is being called.
174  cout << TEST_HEADER << "Testing FilterLeastSquaresFIR::filter(input) ...";
175 
176  data = f.filter(0.9 * noise);
177 
178  diff = data - gold;
179  diff.abs();
180 
181  if(diff.getMax() > GAMMA)
182  {
183  cerr << TEST_ERROR_HEADER
184  << "Output did not match gold file!"
185  << endl;
186 
187  diff.plot("data - gold");
188  data.plot("data");
189  gold.plot("gold");
190 
191  Plotter::show();
192 
193  exit(1);
194  }
195 
196  cout << SUCCESS;
197 
198  // Repeated to test that reset() is being called.
199  cout << TEST_HEADER << "Testing FilterLeastSquaresFIR::setWindow() ...";
200 
201  freqs = Buffer(4);
202  amplitude = Buffer(4);
203 
204  freqs << 0.0 << 30.0 << 30.0 << 500.0;
205  amplitude << 1.0 << 1.0 << 0.0 << 0.0;
206 
207  f = FilterLeastSquaresFIR(sr, 32, freqs, amplitude);
208 
210 
211  data = f.getFrequencyResponse().getdB();
212 
213 //~ // Create gold file
214 //~ cout << "SAVING GOLD FILE: gold/FilterLeastSquaresFIR_out3.wav" << endl;
215 //~ data >> "gold/FilterLeastSquaresFIR_out3.wav";
216 
217  gold = Buffer("gold/FilterLeastSquaresFIR_out3.wav");
218 
219  diff = data - gold;
220 
221  float64 max_diff = diff.getAbs().getMax();
222 
223  if(max_diff > 1e-9) // gamma is changed here
224  {
225  cerr << TEST_ERROR_HEADER
226  << "Output did not match gold file!"
227  << endl;
228 
229  diff.plot("data - gold");
230  data.plot("data");
231  gold.plot("gold");
232 
233  Plotter::show();
234 
235  exit(1);
236  }
237 
238  cout << SUCCESS << endl;
239 }
void makeKernel(const Buffer &freq_axis, const Buffer &amplitude_axis)
void FilterLeastSquaresFIR_UnitTest()
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
AudioStream filter(const AudioStream &x)
void plot(const std::string &title="Buffer") const
Requires matplotlib. Creates a plot of this Buffer.
Definition: Buffer.cc:1551
#define TEST_HEADER
Definition: Test.h:45
Buffer getAbs() const
Modifies a copy of the Buffer by making any negative value positive.
Definition: Buffer.h:174
double float64
Definition: Nsound.h:146
#define TEST_ERROR_HEADER
Definition: Test.h:49
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
void abs()
Modifies the Buffer by making any negative value positive.
Definition: Buffer.cc:119
#define SUCCESS
Definition: UnitTest.h:42
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getFrequencyResponse(const uint32 n_fft=8192)
Definition: Filter.cc:210
A FIR filter that is defined as the least square error to the desired requency response.
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
Buffer getdB() const
Returns the Buffer in dB.
Definition: Buffer.h:487
float64 sr
Definition: example3.cc:24