Nsound  0.9.4
Macros | Functions
UnitTest.h File Reference

Go to the source code of this file.

Macros

#define TEST_HEADER
 
#define TEST_ERROR_HEADER
 
#define SUCCESS   " SUCCESS!"
 

Functions

void Buffer_UnitTest ()
 
void BufferResample_UnitTest ()
 
void DelayLine_UnitTest ()
 
void FFTransform_UnitTest ()
 
void FilterBandPassFIR_UnitTest ()
 
void FilterBandPassIIR_UnitTest ()
 
void FilterBandRejectFIR_UnitTest ()
 
void FilterBandRejectIIR_UnitTest ()
 
void FilterCombLowPassFeedback_UnitTest ()
 
void FilterDelay_UnitTest ()
 
void FilterHighPassFIR_UnitTest ()
 
void FilterHighPassIIR_UnitTest ()
 
void FilterLeastSquaresFIR_UnitTest ()
 
void FilterLowPassFIR_UnitTest ()
 
void FilterLowPassIIR_UnitTest ()
 
void FilterParametricEqualizer_UnitTest ()
 
void Generator_UnitTest ()
 
void Sine_UnitTest ()
 
void Triangle_UnitTest ()
 
void Wavefile_UnitTest ()
 

Macro Definition Documentation

#define TEST_HEADER
Value:
endl << THIS_FILE << ":"; cout.width(4); cout << __LINE__ \
<< ": "; cout.width(0); cout
#define THIS_FILE

Definition at line 34 of file UnitTest.h.

#define TEST_ERROR_HEADER
Value:
" FAILURE!" << endl << THIS_FILE << ":"; cerr.width(4); cerr << __LINE__ \
<< ": ***Error! "; cerr.width(0); cerr
#define THIS_FILE

Definition at line 38 of file UnitTest.h.

#define SUCCESS   " SUCCESS!"

Function Documentation

void Buffer_UnitTest ( )

Definition at line 48 of file Buffer_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), GAMMA, Nsound::Generator::generate(), Nsound::Buffer::getAbs(), Nsound::Buffer::getLength(), Nsound::Buffer::getMax(), Nsound::Buffer::getMin(), Nsound::Buffer::ones(), Nsound::Buffer::plot(), Nsound::circular_iterator::reset(), Nsound::Buffer::reverse(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Plotter::show(), Nsound::Buffer::smooth(), Nsound::Buffer::subbuffer(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, testBufferAdd(), and THIS_FILE.

Referenced by main().

49 {
50  cout << endl << THIS_FILE;
51 
52  Wavefile::setDefaultSampleSize(48);
53 
54  cout << TEST_HEADER << "Testing Buffer::operator<<(float64) ...";
55 
56  Buffer b1(29);
57 
58  for(float64 i = 0.0; i < 100.0; i += 1.1)
59  {
60  b1 << i;
61  }
62 
63  uint32 index = 0;
64  for(float64 i = 0.0; i < 100.0; i += 1.1)
65  {
66  if( ::fabs(b1[index++] - i) > GAMMA)
67  {
68  cerr << TEST_ERROR_HEADER
69  << "Output did not match expected values!"
70  << endl;
71 
72  exit(1);
73  }
74  }
75 
76  cout << SUCCESS;
77 
78  cout << TEST_HEADER << "Testing Buffer::Buffer(const Buffer &) ...";
79 
80  Buffer b2(b1);
81 
82  if(b1 != b2 || b2 != b1)
83  {
84  cerr << TEST_ERROR_HEADER
85  << "Output did not match expected values!"
86  << endl;
87 
88  Buffer diff(b1 - b2);
89  diff.plot("data - gold");
90  b1.plot("b1");
91  b2.plot("b2");
92 
93  Plotter::show();
94 
95  exit(1);
96  }
97 
98  cout << SUCCESS;
99 
100  cout << TEST_HEADER << "Testing Buffer::operator=(const Buffer &) ...";
101 
102  Buffer b3;
103 
104  b3 = b1;
105 
106  if(b3 != b1 || b1 != b3)
107  {
108  cerr << TEST_ERROR_HEADER
109  << "Output did not match expected values!"
110  << endl;
111 
112  Buffer diff(b3 - b1);
113  diff.plot("data - gold");
114  b3.plot("b3");
115  b1.plot("b1");
116 
117  Plotter::show();
118 
119  exit(1);
120  }
121 
122  cout << SUCCESS;
123 
124  cout << TEST_HEADER << "Testing Buffer::operator<<(const Buffer &) ...";
125 
126  Buffer b4(b3);
127 
128  b4 << b1;
129  b3 << b1;
130 
131  if(!(b4 == b3) || !(b3 == b4))
132  {
133  cerr << TEST_ERROR_HEADER
134  << "Output did not match expected values!"
135  << endl;
136 
137  Buffer diff(b4 - b3);
138  diff.plot("data - gold");
139  b4.plot("b4");
140  b3.plot("b3");
141 
142  Plotter::show();
143 
144  exit(1);
145  }
146 
147  cout << SUCCESS;
148 
149  cout << TEST_HEADER << "Testing Buffer::operator[](uint32) ...";
150 
151  uint32 size = b4.getLength();
152 
153  for(uint32 i = 0; i < size; ++i)
154  {
155  if(::fabs(b4[i] - b3[i]) > GAMMA)
156  {
157  cerr << TEST_ERROR_HEADER
158  << "Output did not match expected values!"
159  << endl;
160 
161  Buffer diff(b4 - b3);
162  diff.plot("data - gold");
163  b4.plot("b4");
164  b3.plot("b3");
165 
166  Plotter::show();
167 
168  exit(1);
169  }
170  }
171 
172  cout << SUCCESS;
173 
174  cout << TEST_HEADER << "Testing Buffer::operator+,-,*,/ ...";
175 
176  b4 = b3;
177 
178  if(1 + b4 * 2 != 2 * b3 + 1)
179  {
180  cerr << TEST_ERROR_HEADER
181  << "Output did not match expected values!"
182  << endl;
183 
184  exit(1);
185  }
186 
187  if(1 - b4 / 2 != -1 * b3 / 2 + 1)
188  {
189  cerr << TEST_ERROR_HEADER
190  << "Output did not match expected values!"
191  << endl;
192 
193  exit(1);
194  }
195 
196  if(2.0 / b4 != 4.0 * (0.5 / b4))
197  {
198  cerr << TEST_ERROR_HEADER
199  << "Output did not match expected values!"
200  << endl;
201 
202  exit(1);
203  }
204 
205  b3 = -1.0 * b4;
206 
207  b4 += b3;
208 
209  uint32 n_samples = b4.getLength();
210  for(uint32 i = 0; i < n_samples; ++i)
211  {
212  if(b4[i] != 0.0)
213  {
214  cerr << TEST_ERROR_HEADER
215  << "b4 += b3 _SHOULD_ be all zeros!"
216  << endl;
217  exit(1);
218  }
219  }
220 
221  cout << SUCCESS;
222 
223  cout << TEST_HEADER << "Testing Buffer::abs() ...";
224 
225  b4 = Buffer();
226 
227  if(b4.getLength() != 0)
228  {
229  cerr << TEST_ERROR_HEADER
230  << "b4.getLength() != 0"
231  << endl;
232  exit(1);
233  }
234 
235  float64 neg = -1.0;
236  float64 sum = 1.0;
237  for(uint32 i = 0; i < 100; ++i)
238  {
239  sum *= neg;
240  b4 << sum;
241  }
242 
243  b4.abs();
244 
245  float64 m = b4.getMin();
246  if(m < 0.0)
247  {
248  cerr << TEST_ERROR_HEADER
249  << "b4.abs() is broken"
250  << endl;
251  exit(1);
252  }
253 
254  cout << SUCCESS;
255 
256  cout << TEST_HEADER << "Testing Buffer::reverse() ...";
257 
258  b4 = Buffer();
259 
260  if(b4.getLength() != 0)
261  {
262  cerr << TEST_ERROR_HEADER
263  << "b4.getLength() != 0"
264  << endl;
265  exit(1);
266  }
267 
268  for(float64 f = 0.0; f < 100.0; f += 1.0)
269  {
270  b4 << f;
271  }
272 
273  b4.reverse();
274 
275  index = 0;
276  for(float64 f = 99.0; f >= 0.0; f -= 1.0)
277  {
278  if(::fabs(b4[index++] - f) > GAMMA)
279  {
280  cerr << TEST_ERROR_HEADER
281  << "b4.reverse() borken"
282  << endl;
283 
284  b4.plot("b4");
285 
286  Plotter::show();
287  exit(1);
288  }
289  }
290 
291  cout << SUCCESS;
292 
293  cout << TEST_HEADER << "Testing Buffer::subbuffer() ...";
294 
295  b3 = Buffer();
296 
297  uint32 chunk = 100;
298 
299  for(uint32 i = 0; i < chunk * 2 + 1; ++i)
300  {
301  b3 << i;
302  }
303 
304  uint32 test_size = 99;
305 
306  Buffer subbuf = b3.subbuffer(0, test_size);
307 
308  if (subbuf.getLength() != test_size)
309  {
310  cerr << TEST_ERROR_HEADER
311  << "subbuf.getLength() = "
312  << subbuf.getLength()
313  << " != "
314  << test_size
315  << endl;
316  exit(1);
317  }
318 
319  test_size = chunk;
320 
321  subbuf = b3.subbuffer(0,test_size);
322 
323  if (subbuf.getLength() != test_size)
324  {
325  cerr << TEST_ERROR_HEADER
326  << "subbuf.getLength() = "
327  << subbuf.getLength()
328  << " != "
329  << test_size
330  << endl;
331  exit(1);
332  }
333 
334  test_size = b3.getLength() + 1;
335 
336  subbuf = b3.subbuffer(0, test_size);
337 
338  if (subbuf.getLength() != b3.getLength())
339  {
340  cerr << TEST_ERROR_HEADER
341  << "subbuf.getLength() = "
342  << subbuf.getLength()
343  << " != "
344  << b3.getLength()
345  << endl;
346  exit(1);
347  }
348 
349  test_size = chunk + 10;
350 
351  for(uint32 i = 0; i < 10; ++i)
352  {
353  subbuf = b3.subbuffer(i, test_size);
354 
355  if (subbuf.getLength() != test_size)
356  {
357  cerr << TEST_ERROR_HEADER
358  << "subbuf.getLength() = "
359  << subbuf.getLength()
360  << " != "
361  << test_size
362  << endl;
363  exit(1);
364  }
365  }
366 
367  subbuf = b3.subbuffer(0,10);
368 
369  for(uint32 i = 0; i < 10; ++i)
370  {
371  if(static_cast<uint32>(subbuf[i]) != i)
372  {
373  cerr << TEST_ERROR_HEADER
374  << "b3.subbuffer() error!"
375  << endl;
376  exit(1);
377  }
378  }
379 
380  // Try with offset
381 
382  subbuf = b3.subbuffer(3,3);
383 
384  for(uint32 i = 0; i < 3; ++i)
385  {
386  if(static_cast<uint32>(subbuf[i]) != i + 3)
387  {
388  cerr << TEST_ERROR_HEADER
389  << "b3.subbuffer() error!"
390  << endl;
391  exit(1);
392  }
393  }
394 
395  // Try using default value for n_samples
396 
397  subbuf = b3.subbuffer(3);
398 
399  if(subbuf.getLength() != b3.getLength() - 3)
400  {
401  cerr << TEST_ERROR_HEADER
402  << "subbuf.getLength() = "
403  << subbuf.getLength()
404  << " != "
405  << b3.getLength() - 3
406  << endl;
407  exit(1);
408  }
409 
410  // Try using default value for n_samples
411 
412  subbuf = b3.subbuffer(0);
413 
414  if(subbuf.getLength() != b3.getLength())
415  {
416  cerr << TEST_ERROR_HEADER
417  << "subbuf.getLength() _SHOULD_ be 10, found "
418  << subbuf.getLength()
419  << endl;
420  exit(1);
421  }
422 
423  for(uint32 i = 0; i < 10; ++i)
424  {
425  if(static_cast<uint32>(subbuf[i]) != i)
426  {
427  cerr << TEST_ERROR_HEADER
428  << "b3.subbuffer() error!"
429  << endl;
430  exit(1);
431  }
432  }
433 
434  subbuf = b3.subbuffer(5,5);
435 
436  if(subbuf.getLength() != 5)
437  {
438  cerr << TEST_ERROR_HEADER
439  << "subbuf.getLength() = "
440  << subbuf.getLength()
441  << " != 5"
442  << endl;
443  exit(1);
444  }
445 
446  for(uint32 i = 0; i < 5; ++i)
447  {
448  if(static_cast<uint32>(subbuf[i]) != i + 5)
449  {
450  cerr << TEST_ERROR_HEADER
451  << "subbuf["
452  << i
453  << "] = "
454  << subbuf[i]
455  << " != "
456  << i + 5
457  << endl;
458  exit(1);
459  }
460  }
461 
462  cout << SUCCESS;
463 
464  cout << TEST_HEADER << "Testing Buffer::smooth() ...";
465 
466  Sine sine(100);
467 
468  b4 << "gold/Filter_noise.wav";
469 
470  uint32 b4_length = b4.getLength();
471  float64 b4_max = b4.getMax();
472 
473  b4.smooth(1, 16);
474 
475  if(b4.getLength() != b4_length)
476  {
477  cerr << TEST_ERROR_HEADER
478  << "b4.smooth() error!"
479  << endl;
480  exit(1);
481  }
482 
483  if(b4.getMax() > b4_max)
484  {
485  cerr << TEST_ERROR_HEADER
486  << "b4.smooth() error!"
487  << endl;
488  exit(1);
489  }
490 
491  cout << SUCCESS;
492 
493  cout << TEST_HEADER << "Testing Buffer::pow() ...";
494 
495  b4 = 2 * Buffer::ones(5);
496 
497  b4 ^= 2.0;
498 
499  for(uint32 i = 0; i < 5; ++i)
500  {
501  if(b4[i] != 4.0)
502  {
503  cerr << TEST_ERROR_HEADER
504  << "Buffer::pow() error!"
505  << " 4.0 != " << b4[i]
506  << endl;
507  exit(1);
508  }
509  }
510 
511  b4 ^= 0.50;
512 
513  for(uint32 i = 0; i < 5; ++i)
514  {
515  if(b4[i] != 2.0)
516  {
517  cerr << TEST_ERROR_HEADER
518  << "Buffer::pow() error!"
519  << " 2.0 != " << b4[i]
520  << endl;
521  exit(1);
522  }
523  }
524 
525  b4 = (b4 ^ 2.0) + (b4 ^ 2.0);
526 
527  for(uint32 i = 0; i < 5; ++i)
528  {
529  if(b4[i] != 8.0)
530  {
531  cerr << TEST_ERROR_HEADER
532  << "Buffer::pow() error!"
533  << " 8.0 != " << b4[i]
534  << endl;
535  exit(1);
536  }
537  }
538 
539  cout << SUCCESS;
540 
541  cout << TEST_HEADER << "Testing Buffer operators with different length ...";
542 
543  Buffer b5(100);
544  b5 = sine.drawLine(1.0, 2.0, 2.0);
545 
546  Buffer b6(100);
547  b6 = sine.drawLine(2.0, 3.0, 3.0);
548 
549  Buffer result = b5 * b6;
550 
551  if(result.getLength() != b5.getLength())
552  {
553  cerr << TEST_ERROR_HEADER
554  << "result.getLength() != b5.getLength(), "
555  << result.getLength()
556  << " != "
557  << b5.getLength()
558  << endl;
559  exit(1);
560  }
561 
562  for(uint32 i = 0; i < result.getLength(); ++i)
563  {
564  if(result[i] != 6.0)
565  {
566  cerr << TEST_ERROR_HEADER
567  << "result["
568  << i
569  << "] != 6.0"
570  << endl;
571  exit(1);
572  }
573  }
574 
575  result = b6 * b5;
576 
577  if(result.getLength() != b6.getLength())
578  {
579  cerr << TEST_ERROR_HEADER
580  << "result.getLength() != b6.getLength(), "
581  << result.getLength()
582  << " != "
583  << b6.getLength()
584  << endl;
585  exit(1);
586  }
587 
588  for(uint32 i = 0; i < b5.getLength(); ++i)
589  {
590  if(result[i] != 6.0)
591  {
592  cerr << TEST_ERROR_HEADER
593  << "result["
594  << i
595  << "] != 6.0"
596  << endl;
597  exit(1);
598  }
599  }
600 
601  for(uint32 i = b5.getLength(); i < result.getLength(); ++i)
602  {
603  if(result[i] != 3.0)
604  {
605  cerr << TEST_ERROR_HEADER
606  << "result["
607  << i
608  << "] != 3.0"
609  << endl;
610  exit(1);
611  }
612  }
613 
614  cout << SUCCESS;
615 
616  cout << TEST_HEADER << "Testing Buffer::add() ...";
617 
618  testBufferAdd();
619 
620  cout << SUCCESS;
621 
622  cout << TEST_HEADER << "Testing Buffer::circular_iterator ...";
623 
624  b5 = sine.drawLine(1.0, 1.0, 100.0);
625 
626  Buffer::circular_iterator itor = b5.cbegin();
627 
628  for(uint32 i = 0; i < b5.getLength(); ++i)
629  {
630  ++itor;
631  }
632 
633  if(itor != b5.cbegin())
634  {
635  cerr << TEST_ERROR_HEADER
636  << "circular_iterator::operator++ is broken!"
637  << endl;
638  exit(1);
639  }
640 
641  itor.reset();
642 
643  for(uint32 i = 0; i < b5.getLength(); ++i)
644  {
645  --itor;
646  }
647 
648  if(itor != b5.cbegin())
649  {
650  cerr << TEST_ERROR_HEADER
651  << "circular_iterator::operator-- is broken!"
652  << endl;
653  exit(1);
654  }
655 
656  itor.reset();
657 
658  for(uint32 i = 0; i < 3; ++i)
659  {
660  ++itor;
661  }
662 
663  if(itor != (b5.cbegin() + 3))
664  {
665  cerr << TEST_ERROR_HEADER
666  << "circular_iterator::operator++ is broken!"
667  << endl;
668  exit(1);
669  }
670 
671  itor.reset();
672 
673  for(uint32 i = 0; i < 3; ++i)
674  {
675  --itor;
676  }
677 
678  if(itor != (b5.cbegin() - 3))
679  {
680  cerr << TEST_ERROR_HEADER
681  << "circular_iterator::operator-- is broken!"
682  << endl;
683  exit(1);
684  }
685 
686  cout << SUCCESS;
687 
688  cout << TEST_HEADER << "Testing Buffer advanced operators ...";
689 
690  Buffer b7 = sine.generate(1.0, 2.0);
691 
692  Buffer data = b7;
693 
694  data(data > 0.5) = 0.5;
695  data(data < -0.5) = -0.5;
696 
697 //~ // Create gold file
698 //~ data >> "gold/Buffer_out1.wav";
699 
700  // Read in gold
701  Buffer gold("gold/Buffer_out1.wav");
702 
703  Buffer diff(gold - data);
704 
705  if(diff.getAbs().getMax() > GAMMA)
706  {
707  cerr << TEST_ERROR_HEADER
708  << "Output did not match expected values!"
709  << endl;
710 
711  diff.plot("gold - data");
712  data.plot("data");
713  gold.plot("gold");
714 
715  Plotter::show();
716 
717  exit(1);
718  }
719 
720  data = b7;
721 
722  BooleanVector bv1 = data > 0.5;
723  BooleanVector bv2 = data < -0.5;
724 
725  data(bv1) *= 0.1;
726  data(bv2) *= 0.1;
727 
728  data(bv1) += 0.45;
729  data(bv2) -= 0.45;
730 
731 //~ // Create gold file
732 //~ data >> "gold/Buffer_out2.wav";
733 
734  // Read in gold
735  gold = Buffer("gold/Buffer_out2.wav");
736 
737  diff = gold - data;
738 
739  if(diff.getAbs().getMax() > GAMMA)
740  {
741  cerr << TEST_ERROR_HEADER
742  << "Output did not match expected values!"
743  << endl;
744 
745  diff.plot("gold - data");
746  data.plot("data");
747  gold.plot("gold");
748 
749  Plotter::show();
750 
751  exit(1);
752  }
753 
754  cout << SUCCESS << endl;
755 }
Buffer subbuffer(uint32 start_index, uint32 n_samples=0) const
Slice the Buffer.
Definition: Buffer.cc:2073
unsigned int uint32
Definition: Nsound.h:153
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
A circulator iterator for class Buffer.
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define TEST_ERROR_HEADER
Definition: Test.h:49
#define SUCCESS
Definition: UnitTest.h:42
void testBufferAdd()
A Buffer for storing audio samples.
Definition: Buffer.h:60
std::vector< boolean > BooleanVector
static const char * THIS_FILE
DOXME.
Definition: Sine.h:43
static const float64 GAMMA
void BufferResample_UnitTest ( )

Definition at line 52 of file BufferResample_UnitTest.cc.

References GAMMA, Nsound::Generator::generate(), Nsound::Buffer::getAbs(), Nsound::Buffer::getLength(), Nsound::Buffer::getMax(), Nsound::Buffer::getResample(), Nsound::Buffer::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

53 {
54  cout << endl << THIS_FILE;
55 
56  Wavefile::setDefaultSampleSize(64);
57  Wavefile::setIEEEFloat(true);
58 
59  static const uint32 LM[4] = { 2,3,5,7 };
60 
61  Sine sin(150);
62 
63  Buffer input = sin.generate(1.0, 3.0);
64 
65  cout << TEST_HEADER << "Testing Buffer:getResample(L,M) ...";
66 
67  Buffer data;
68  Buffer gold;
69  Buffer diff;
70 
71  std::stringstream ss;
72 
73  for(uint32 i = 1; i <= 4; ++i)
74  {
75 
76  std::string gold_filename;
77 
78  ss.str("");
79  ss << "gold/BufferResample_out_" << i << "_" << LM[i-1] << ".wav";
80 
81  data = input.getResample(i, LM[i-1]);
82 
83  // Create gold file
84 //~ data >> ss.str().c_str();
85 
86  gold = Buffer(ss.str().c_str());
87 
88  diff = data - gold;
89 
90  if(gold.getLength() != data.getLength() ||
91  diff.getAbs().getMax() > GAMMA)
92  {
93  cerr << TEST_ERROR_HEADER
94  << "Output did not match gold file!"
95  << endl;
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  // Reverse L & M
107  ss.str("");
108  ss << "gold/BufferResample_out_" << LM[i-1] << "_" << i << ".wav";
109 
110  data = input.getResample(LM[i-1], i);
111 
112  // Create gold file
113 //~ data >> ss.str().c_str();
114 
115  gold = Buffer(ss.str().c_str());
116 
117  diff = data - gold;
118 
119  if(gold.getLength() != data.getLength() ||
120  diff.getAbs().getMax() > GAMMA)
121  {
122  cerr << TEST_ERROR_HEADER
123  << "Output did not match gold file!"
124  << endl;
125 
126  diff.plot("data - gold");
127  data.plot("data");
128  gold.plot("gold");
129 
130  Plotter::show();
131 
132  exit(1);
133  }
134 
135  }
136 
137  cout << SUCCESS << endl;
138 }
unsigned int uint32
Definition: Nsound.h:153
#define GAMMA
#define THIS_FILE
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
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define TEST_ERROR_HEADER
Definition: Test.h:49
#define SUCCESS
Definition: UnitTest.h:42
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getResample(float64 factor, const uint32 N=10, float64 beta=5.0) const
Resamples a copy of this Buffer using discrete-time resampling.
Definition: Buffer.cc:1607
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
DOXME.
Definition: Sine.h:43
void DelayLine_UnitTest ( )

Definition at line 55 of file DelayLine_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), GAMMA, Nsound::Generator::generate(), Nsound::Buffer::getMax(), Nsound::Buffer::plot(), Nsound::DelayLine::read(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, THIS_FILE, and Nsound::DelayLine::write().

Referenced by main().

56 {
57  cout << endl << THIS_FILE;
58 
59  Wavefile::setDefaultSampleSize(64);
60  Wavefile::setIEEEFloat(true);
61 
62  DelayLine dl(1.0, 10.0);
63  Sine sine(100.0);
64 
65  Buffer input;
66  Buffer data;
67  Buffer gold;
68  Buffer diff;
69 
70  cout << TEST_HEADER << "Testing DelayLine::write() & read() ...";
71 
72  input = sine.generate(1.0, 2.0) * sine.drawLine(1.0, 1.0, 0.0);
73 
74  for(uint32 i = 0; i < 100; ++i)
75  {
76  dl.write(input[i]);
77  data << dl.read();
78  }
79 
80  // Create Gold files
81 //~ data >> "gold/DelayLine_out1.wav";
82 
83  gold = Buffer("gold/DelayLine_out1.wav");
84 
85  diff = data - gold;
86  diff.abs();
87 
88  if(diff.getMax() > GAMMA)
89  {
90  cerr << TEST_ERROR_HEADER
91  << "Output did not match gold file!"
92  << endl;
93 
94  diff.plot("data - gold");
95  data.plot("data");
96  gold.plot("gold");
97 
98  Plotter::show();
99 
100  exit(1);
101  }
102 
103  cout << SUCCESS << endl;
104 }
unsigned int uint32
Definition: Nsound.h:153
static const float64 GAMMA
static const char * THIS_FILE
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
std::istream & read(std::istream &stream_in)
Constructs a Buffer from seralized data in the inputstream.
Definition: Buffer.cc:1945
std::ostream & write(std::ostream &out) const
Serializes the Buffer to output stream, no endian checks.
Definition: Buffer.cc:1922
#define TEST_ERROR_HEADER
Definition: Test.h:49
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
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
DOXME.
Definition: Sine.h:43
void FFTransform_UnitTest ( )

Definition at line 53 of file FFTransform_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::buzz(), Nsound::FFTransform::fft(), GAMMA, Nsound::Buffer::getAbs(), Nsound::FFTChunk::getImaginary(), Nsound::Buffer::getLength(), Nsound::Buffer::getMax(), Nsound::FFTChunk::getReal(), Nsound::FFTransform::ifft(), M_PI, Nsound::Buffer::plot(), Nsound::FFTChunk::setCartesian(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::FFTChunk::setPolar(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

54 {
55  cout << endl << THIS_FILE;
56 
57  Wavefile::setDefaultSampleSize(64);
58  Wavefile::setIEEEFloat(true);
59 
60  Cosine cos(512);
61 
62  Buffer input = cos.buzz(1.0, 8.0, 2.0, 0.0);
63 
64  FFTransform fft(512);
65 
66  FFTChunkVector vec;
67 
68  Buffer gold;
69 
70  cout << TEST_HEADER << "Testing FFTransform::fft(), ifft() ..." << flush;
71 
72  vec = fft.fft(input, 128, 0);
73 
74  if(vec.size() != 512 / 128)
75  {
76  cerr << TEST_ERROR_HEADER
77  << "Output did not match!"
78  << endl
79  << "vec.size() = " << vec.size() << " != " << 512 / 128
80  << endl;
81 
82  exit(1);
83  }
84 
85  Buffer data = fft.ifft(vec);
86 
87 //~ input.plot("input");
88 //~ data.plot("fft,ifft");
89 
90  if(data.getLength() != input.getLength())
91  {
92  cerr << TEST_ERROR_HEADER
93  << "Output did not match!"
94  << endl
95  << "result.getLength() = " << data.getLength() << " != "
96  << input.getLength()
97  << endl;
98 
99  exit(1);
100  }
101 
102  // Create gold file
103 //~ data >> "gold/FFTransform_out1.wav";
104 
105  gold = Buffer("gold/FFTransform_out1.wav");
106 
107  Buffer diff = data - gold;
108 
109  if(diff.getAbs().getMax() > GAMMA)
110  {
111  cerr << TEST_ERROR_HEADER
112  << "Output did not match gold file!"
113  << endl;
114 
115  diff.plot("input - gold");
116  data.plot("data");
117  gold.plot("gold");
118 
119  Plotter::show();
120  exit(1);
121  }
122 
123  cout << SUCCESS;
124 
125  cout << TEST_HEADER << "Testing FFTChunk::getReal(), getImaginary() ..." << flush;
126 
127  vec = fft.fft(input, 128, 0);
128 
129  Buffer real = vec[0].getReal();
130  Buffer img = vec[0].getImaginary();
131 
132  // Create gold files.
133 //~ real >> "gold/FFTransform_out2.wav";
134 //~ img >> "gold/FFTransform_out3.wav";
135 
136  gold = Buffer("gold/FFTransform_out2.wav");
137 
138  diff = real - gold;
139  diff.abs();
140  if(diff.getMax() > GAMMA)
141  {
142  cerr << TEST_ERROR_HEADER
143  << "Output did not match gold file!"
144  << endl;
145 
146  diff.plot("real - gold");
147  real.plot("real");
148  gold.plot("gold");
149 
150  Plotter::show();
151  exit(1);
152  }
153 
154  gold = Buffer("gold/FFTransform_out3.wav");
155 
156  diff = img - gold;
157  diff.abs();
158 
159  if(diff.getMax() > GAMMA)
160  {
161  cerr << TEST_ERROR_HEADER
162  << "Output did not match gold file!"
163  << endl;
164 
165  diff.plot("img - gold");
166  img.plot("img");
167  gold.plot("gold");
168 
169  Plotter::show();
170  exit(1);
171  }
172 
173  cout << SUCCESS;
174 
175  cout << TEST_HEADER << "Testing FFTChunk::getMagnitude(), getPhase() ..." << flush;
176 
177  vec = fft.fft(input, 128, 0);
178 
179  Buffer mag = vec[0].getMagnitude();
180  Buffer phase = vec[0].getPhase();
181 
182  // Create gold files.
183 //~ mag >> "gold/FFTransform_out4.wav";
184 //~ phase >> "gold/FFTransform_out5.wav";
185 
186  gold = Buffer("gold/FFTransform_out4.wav");
187 
188  diff = mag - gold;
189 
190  if(diff.getAbs().getMax() > GAMMA)
191  {
192  cerr << TEST_ERROR_HEADER
193  << "Output did not match gold file!"
194  << endl;
195 
196  diff.plot("data - gold");
197  mag.plot("data");
198  gold.plot("gold");
199 
200  Plotter::show();
201  exit(1);
202  }
203 
204  gold = Buffer("gold/FFTransform_out5.wav");
205 
206  diff = phase - gold;
207 
208  diff(diff > 2.0 * M_PI - 0.01) = 0.0;
209  diff(diff < 2.0 * M_PI - 0.01) = 0.0;
210 
211  if(diff.getAbs().getMax() > GAMMA)
212  {
213  cerr << TEST_ERROR_HEADER
214  << "Output did not match gold file!"
215  << endl;
216 
217  diff.plot("phase - gold");
218  phase.plot("phase");
219  gold.plot("gold");
220 
221  Plotter::show();
222  exit(1);
223  }
224 
225  cout << SUCCESS;
226 
227  cout << TEST_HEADER << "Testing FFTChunk::toCartesian(), toPolar() ..." << flush;
228 
229  vec[0].toPolar();
230 
231  mag = vec[0].getMagnitude();
232  phase = vec[0].getPhase();
233 
234  gold = Buffer("gold/FFTransform_out4.wav");
235 
236  diff = mag - gold;
237 
238  if(diff.getAbs().getMax() > GAMMA)
239  {
240  cerr << TEST_ERROR_HEADER
241  << "Output did not match gold file!"
242  << endl;
243 
244  diff.plot("data - gold");
245  mag.plot("data");
246  gold.plot("gold");
247 
248  Plotter::show();
249  exit(1);
250  }
251 
252  gold = Buffer("gold/FFTransform_out5.wav");
253 
254  diff = phase - gold;
255  diff.abs();
256  if(diff.getMax() > 2*M_PI)
257  {
258  cerr << TEST_ERROR_HEADER
259  << "Output did not match gold file!"
260  << endl;
261 
262  diff.plot("phase - gold");
263  phase.plot("phase");
264  gold.plot("gold");
265 
266  Plotter::show();
267  exit(1);
268  }
269 
270  cout << SUCCESS;
271 
272  cout << TEST_HEADER << "Testing FFTChunk::setCartesian(), setPolar() ..." << flush;
273 
274  vec = fft.fft(input, 512, 0);
275 
276  mag = vec[0].getMagnitude();
277  phase = vec[0].getPhase();
278 
279  FFTChunk orig(vec[0]);
280  FFTChunk chunk(512, 512);
281 
282  chunk.setPolar(mag, phase);
283 
284  vec[0] = chunk;
285 
286  data = fft.ifft(vec);
287 
288  diff = data - input;
289 
290  if(diff.getAbs().getMax() > 0.005)
291  {
292  cerr << TEST_ERROR_HEADER
293  << "Output did not match gold file!"
294  << endl;
295 
296  diff.plot("data - gold");
297  data.plot("data");
298  input.plot("gold");
299 
300  Plotter::show();
301  exit(1);
302  }
303 
304  chunk.setCartesian(orig.getReal(), orig.getImaginary());
305 
306  vec[0] = chunk;
307 
308  data = fft.ifft(vec);
309 
310  diff = data - input;
311 
312  if(diff.getAbs().getMax() > 2*GAMMA)
313  {
314  cerr << TEST_ERROR_HEADER
315  << "Output did not match gold file!"
316  << endl;
317 
318  diff.plot("data - gold");
319  data.plot("data");
320  input.plot("gold");
321 
322  Plotter::show();
323  exit(1);
324  }
325 
326  cout << SUCCESS;
327 
328  cout << TEST_HEADER << "Testing FFTChunk::getFrequencyAxis() ..." << flush;
329 
330  data = vec[0].getFrequencyAxis();
331 
332 //~ // Create gold files.
333 //~ data >> "gold/FFTransform_out6.wav";
334 
335  gold = Buffer("gold/FFTransform_out6.wav");
336 
337  diff = data - gold;
338 
339  if(diff.getAbs().getMax() > 1e-16)
340  {
341  cerr << TEST_ERROR_HEADER
342  << "Output did not match gold file!"
343  << endl;
344 
345  diff.plot("data - gold");
346  data.plot("data");
347  gold.plot("gold");
348 
349  Plotter::show();
350  exit(1);
351  }
352 
353  cout << SUCCESS << endl;
354 }
Results of performing an FFT are stored in this class.
Definition: FFTChunk.h:49
#define M_PI
Definition: Nsound.h:121
DOXME.
Definition: Cosine.h:44
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
static const char * THIS_FILE
Buffer getAbs() const
Modifies a copy of the Buffer by making any negative value positive.
Definition: Buffer.h:174
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define TEST_ERROR_HEADER
Definition: Test.h:49
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
#define SUCCESS
Definition: UnitTest.h:42
A Buffer for storing audio samples.
Definition: Buffer.h:60
static const float64 GAMMA
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
std::vector< FFTChunk > FFTChunkVector
Definition: FFTChunk.h:119
void FilterBandPassFIR_UnitTest ( )
void FilterBandPassIIR_UnitTest ( )
void FilterBandRejectFIR_UnitTest ( )
void FilterBandRejectIIR_UnitTest ( )
void FilterCombLowPassFeedback_UnitTest ( )

Definition at line 55 of file FilterCombLowPassFeedback_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), Nsound::FilterCombLowPassFeedback::filter(), GAMMA, Nsound::Buffer::getMax(), Nsound::Buffer::normalize(), Nsound::Buffer::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

56 {
57  cout << endl << THIS_FILE;
58 
59  Wavefile::setDefaultSampleSize(48);
60 
61  FilterCombLowPassFeedback f(100.0, 0.1, 0.66, 12.0);
62 
63  // Create some noise!
64  Sine sine(100);
65 
66  Buffer noise;
67 
68  noise << "gold/Filter_noise.wav";
69 
70  cout << TEST_HEADER << "Testing FilterCombLowPassFeedback::filter(input) ...";
71 
72  Buffer data;
73 
74  data = f.filter(noise);
75  data.normalize();
76 
77  // Create the gold file
78 //~ data >> "gold/FilterCombLowPassFeedback_out1.wav";
79 
80  Buffer gold;
81 
82  gold << "gold/FilterCombLowPassFeedback_out1.wav";
83 
84  Buffer diff = data - gold;
85 
86  Buffer abs_diff(diff);
87  abs_diff.abs();
88 
89  if(abs_diff.getMax() > GAMMA)
90  {
91  cerr << TEST_ERROR_HEADER
92  << "Output did not match gold file!"
93  << endl;
94 
95  diff.plot("data - gold");
96  data.plot("data");
97  gold.plot("gold");
98 
99  Plotter::show();
100 
101  exit(1);
102  }
103 
104  cout << SUCCESS;
105 
106  cout << TEST_HEADER << "Testing FilterCombLowPassFeedback::filter(input, freqs) ...";
107 
108  Buffer freqs = sine.drawLine(1.0, 6.0, 50.0);
109 
110  data = f.filter(noise, freqs);
111  data.normalize();
112 
113  // Create the gold file
114 //~ data >> "gold/FilterCombLowPassFeedback_out2.wav";
115 
116  gold = Buffer();
117 
118  gold << "gold/FilterCombLowPassFeedback_out2.wav";
119 
120  diff = data - gold;
121 
122  abs_diff = diff;
123  abs_diff.abs();
124 
125  if(abs_diff.getMax() > GAMMA)
126  {
127  cerr << TEST_ERROR_HEADER
128  << "Output did not match gold file!"
129  << endl;
130 
131  diff.plot("data - gold");
132  data.plot("data");
133  gold.plot("gold");
134 
135  Plotter::show();
136 
137  exit(1);
138  }
139 
140 //~ cout << SUCCESS;
141 
142 
143  cout << SUCCESS << endl;
144 
145 }
static const char * THIS_FILE
static const float64 GAMMA
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
#define TEST_ERROR_HEADER
Definition: Test.h:49
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
Definition: Buffer.cc:1064
void abs()
Modifies the Buffer by making any negative value positive.
Definition: Buffer.cc:119
#define SUCCESS
Definition: UnitTest.h:42
A class for filtering audio in the frequecy domain.
A Buffer for storing audio samples.
Definition: Buffer.h:60
DOXME.
Definition: Sine.h:43
void FilterDelay_UnitTest ( )

Definition at line 52 of file FilterDelay_UnitTest.cc.

References Nsound::FilterDelay::filter(), GAMMA, Nsound::Generator::generate(), Nsound::AudioStream::getAbs(), Nsound::AudioStream::getMax(), Nsound::AudioStream::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Plotter::show(), Nsound::Generator::silence(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

53 {
54  cout << endl << THIS_FILE;
55 
56  Wavefile::setDefaultSampleSize(48);
57 
58  FilterDelay f(100.0, 3.0);
59 
60  Sine sine(100);
61 
62  AudioStream input(100, 1);
63 
64  input << sine.generate(0.25, 4.0)
65  << sine.silence(0.75);
66 
67  cout << TEST_HEADER << "Testing FilterDelay::filter(input) ...";
68 
69  AudioStream data = f.filter(input, 0.333);
70 
71  // Create the gold file
72 //~ data >> "gold/FilterDelay_out1.wav";
73 
74  AudioStream gold;
75 
76  gold << "gold/FilterDelay_out1.wav";
77 
78  AudioStream diff = data - gold;
79 
80  if(diff.getAbs().getMax() > GAMMA)
81  {
82  cerr << TEST_ERROR_HEADER
83  << "Output did not match gold file!"
84  << endl;
85 
86  diff.plot("data - gold");
87  data.plot("data");
88  gold.plot("gold");
89 
90  Plotter::show();
91 
92  exit(1);
93  }
94 
95 
96  data = f.filter(input, 0.666);
97 
98  // Create the gold file
99 //~ data >> "gold/FilterDelay_out2.wav";
100 
101  gold = AudioStream("gold/FilterDelay_out2.wav");
102 
103  diff = data - gold;
104 
105  if(diff.getAbs().getMax() > GAMMA)
106  {
107  cerr << TEST_ERROR_HEADER
108  << "Output did not match gold file!"
109  << endl;
110 
111  diff.plot("data - gold");
112  data.plot("data");
113  gold.plot("gold");
114 
115  Plotter::show();
116 
117  exit(1);
118  }
119 
120 
121  FilterDelay f2(1.0, 5.0);
122 
123  input = AudioStream(1,1);
124 
125  input << 1.0 << 0.0 << 0.0 << 0.0 << 0.0
126  << 0.0 << 0.0 << 0.0 << 0.0 << 0.0;
127 
128  data = f2.filter(input, 60.0);
129 
130  // Create the gold file
131 //~ data >> "gold/FilterDelay_out3.wav";
132 
133  gold = AudioStream("gold/FilterDelay_out3.wav");
134 
135  diff = data - gold;
136 
137  if(diff.getAbs().getMax() > GAMMA)
138  {
139  cerr << TEST_ERROR_HEADER
140  << "Output did not match gold file!"
141  << endl;
142 
143  diff.plot("data - gold");
144  data.plot("data");
145  gold.plot("gold");
146 
147  Plotter::show();
148 
149  exit(1);
150  }
151 
152 
153  cout << SUCCESS << endl;
154 }
#define TEST_HEADER
Definition: Test.h:45
static const char * THIS_FILE
float64 getMax() const
Returns the maximum sample value in the Audiostream.
Definition: AudioStream.cc:225
#define TEST_ERROR_HEADER
Definition: Test.h:49
void plot(const std::string &title="AudioStream") const
Definition: AudioStream.cc:661
#define SUCCESS
Definition: UnitTest.h:42
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
static const float64 GAMMA
AudioStream getAbs() const
Modifies the AudioStream by making any negative value positive.
Definition: AudioStream.h:81
DOXME.
Definition: Sine.h:43
void FilterHighPassFIR_UnitTest ( )
void FilterHighPassIIR_UnitTest ( )
void FilterLeastSquaresFIR_UnitTest ( )

Definition at line 54 of file FilterLeastSquaresFIR_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::FilterLeastSquaresFIR::filter(), GAMMA, Nsound::Buffer::getAbs(), Nsound::Buffer::getdB(), Nsound::Filter::getFrequencyResponse(), Nsound::Buffer::getMax(), Nsound::FilterLeastSquaresFIR::makeKernel(), Nsound::Buffer::plot(), Nsound::RECTANGULAR, Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::FilterLeastSquaresFIR::setWindow(), Nsound::Plotter::show(), sr, SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

55 {
56  cout << endl << THIS_FILE;
57 
58  Wavefile::setDefaultSampleSize(64);
59  Wavefile::setIEEEFloat(true);
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 
209  f.setWindow(RECTANGULAR);
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 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
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
A FIR filter that is defined as the least square error to the desired requency response.
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
void FilterLowPassFIR_UnitTest ( )
void FilterLowPassIIR_UnitTest ( )
void FilterParametricEqualizer_UnitTest ( )

Definition at line 52 of file FilterParametricEqualizer_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), Nsound::FilterParametricEqualizer::filter(), GAMMA, Nsound::Buffer::getMax(), Nsound::FilterParametricEqualizer::HIGH_SHELF, Nsound::FilterParametricEqualizer::LOW_SHELF, Nsound::FilterParametricEqualizer::PEAKING, Nsound::Buffer::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

53 {
54  cout << endl << THIS_FILE;
55 
56  Wavefile::setDefaultSampleSize(64);
57  Wavefile::setIEEEFloat(true);
58 
59  Sine sine(100);
60 
62  FilterParametricEqualizer::PEAKING,
63  100,
64  12,
65  0.2,
66  4.0);
67 
69  FilterParametricEqualizer::LOW_SHELF,
70  100,
71  12,
72  0.2,
73  4.0);
74 
76  FilterParametricEqualizer::HIGH_SHELF,
77  100,
78  12,
79  0.2,
80  4.0);
81 
82  Buffer freqs = sine.drawLine(5.0, 24, 6);
83 
84  static const std::string titles[3] =
85  {
86  "Testing Peaking filter ...",
87  "Testing Low Shelf filter ...",
88  "Testing High Shelf filter ..."
89  };
90 
91  static const std::string types[3] =
92  {
93  "peaking",
94  "low",
95  "high"
96  };
97 
98  static const std::string gold_filenames[3] =
99  {
100  "gold/FilterParametricEqualizer_out_peaking.wav",
101  "gold/FilterParametricEqualizer_out_low.wav",
102  "gold/FilterParametricEqualizer_out_high.wav"
103  };
104 
105  FilterParametricEqualizer * filters[3] =
106  {
107  &peaking,
108  &low,
109  &high
110  };
111 
112  Buffer noise;
113  Buffer data;
114  Buffer gold;
115  Buffer diff;
116 
117  noise << "gold/Filter_noise.wav";
118  noise << "gold/Filter_noise.wav";
119 
120  for(int i = 0; i < 3; ++i)
121  {
122  cout << TEST_HEADER << titles[i];
123 
124  data = filters[i]->filter(noise, freqs);
125 
126  // Create gold file
127 //~ data >> gold_filenames[i].c_str();
128 
129  gold = Buffer(gold_filenames[i]);
130 
131  diff = data - gold;
132  diff.abs();
133 
134  if(diff.getMax() > GAMMA)
135  {
136  cerr << TEST_ERROR_HEADER
137  << "Output did not match gold file!"
138  << endl;
139 
140  diff.plot("data - gold");
141  data.plot("data");
142  gold.plot("gold");
143 
144  Plotter::show();
145 
146  exit(1);
147  }
148 
149  cout << SUCCESS;
150 
151  }
152 
153  cout << endl;
154 }
Implementation of Zoelzer's parametric equalizer filters, with some modifications by the author...
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
#define TEST_ERROR_HEADER
Definition: Test.h:49
AudioStream filter(const AudioStream &x)
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
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
DOXME.
Definition: Sine.h:43
void Generator_UnitTest ( )

Definition at line 52 of file Generator_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::buzz(), Nsound::Generator::drawLine(), Nsound::Generator::drawSine(), Nsound::Generator::drawSine2(), GAMMA, Nsound::Generator::generate(), Nsound::Buffer::getMax(), Nsound::Plotter::grid(), Nsound::Plotter::legend(), Nsound::Plotter::plot(), Nsound::Buffer::plot(), Nsound::Generator::setChorus(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Generator::setSeed(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

53 {
54  cout << endl << THIS_FILE;
55 
56  Wavefile::setDefaultSampleSize(32);
57  Wavefile::setIEEEFloat(true);
58 
59  Generator gen(100);
60  Cosine cos(100);
61 
62  // Test 7 Hz wave
63  cout << TEST_HEADER << "Testing Generator::drawLine(1.0, 1.0, 0.0) ...";
64 
65  Buffer data = gen.drawLine(1.0, 1.0, 0.0);
66 
67  // Create the gold file
68 //~ data >> "gold/Generator_line1.wav";
69 
70  Buffer gold;
71 
72  gold << "gold/Generator_line1.wav";
73 
74  Buffer diff = data - gold;
75 
76  Buffer abs_diff(diff);
77  abs_diff.abs();
78 
79  if(abs_diff.getMax() > GAMMA)
80  {
81  cerr << TEST_ERROR_HEADER
82  << "Output did not match gold file!"
83  << endl;
84 
85  data.plot("data");
86  gold.plot("gold");
87  diff.plot("data - gold");
88  Plotter::show();
89 
90  exit(1);
91  }
92 
93  cout << SUCCESS;
94 
95  // Test 3.5 Hz wave
96  cout << TEST_HEADER << "Testing Generator::drawLine(1.26, -0.26, 0.26) ...";
97 
98  data = gen.drawLine(1.26, -0.26, 0.26);
99 
100  // Create the gold file
101 //~ data >> "gold/Generator_line2.wav";
102 
103  gold = Buffer();
104 
105  gold << "gold/Generator_line2.wav";
106 
107  diff = data - gold;
108 
109  abs_diff = diff;
110  abs_diff.abs();
111 
112  if(abs_diff.getMax() > GAMMA)
113  {
114  cerr << TEST_ERROR_HEADER
115  << "Output did not match gold file!"
116  << endl;
117 
118  data.plot("data");
119  gold.plot("gold");
120  diff.plot("data - gold");
121  Plotter::show();
122 
123  exit(1);
124  }
125 
126  cout << SUCCESS;
127 
128 
129  // Test 3.5 Hz wave
130  cout << TEST_HEADER << "Testing Generator::buzz(1.0, 4.0, 3, 0.0) ...";
131 
132  data = cos.buzz(1.0, 4.0, 3, 0.0);
133 
134  // Create the gold file
135 //~ data >> "gold/Generator_buzz1.wav";
136 
137  gold = Buffer();
138 
139  gold << "gold/Generator_buzz1.wav";
140 
141  diff = data - gold;
142 
143  abs_diff = diff;
144  abs_diff.abs();
145 
146  if(abs_diff.getMax() > GAMMA)
147  {
148  cerr << TEST_ERROR_HEADER
149  << "Output did not match gold file!"
150  << endl;
151 
152  data.plot("data");
153  gold.plot("gold");
154  diff.plot("data - gold");
155  Plotter::show();
156 
157  exit(1);
158  }
159 
160  cout << SUCCESS;
161 
162 
163  // Test 3.5 Hz wave
164  cout << TEST_HEADER << "Testing Generator::buzz(1.0, 4.0, 3, 0.5) ...";
165 
166  data = cos.buzz(1.0, 4.0, 3, 0.5);
167 
168  // Create the gold file
169 //~ data >> "gold/Generator_buzz2.wav";
170 
171  gold = Buffer();
172 
173  gold << "gold/Generator_buzz2.wav";
174 
175  diff = data - gold;
176 
177  abs_diff = diff;
178  abs_diff.abs();
179 
180  if(abs_diff.getMax() > GAMMA)
181  {
182  cerr << TEST_ERROR_HEADER
183  << "Output did not match gold file!"
184  << endl;
185 
186  data.plot("data");
187  gold.plot("gold");
188  diff.plot("data - gold");
189  Plotter::show();
190 
191  exit(1);
192  }
193 
194  cout << SUCCESS;
195 
196  // Test 3.5 Hz wave
197  cout << TEST_HEADER << "Testing Generator::buzz(1.0, 4.0, 4, 0.5) ...";
198 
199  data = cos.buzz(1.0, 4.0, 4, 0.5);
200 
201  // Create the gold file
202 //~ data >> "gold/Generator_buzz3.wav";
203 
204  gold = Buffer();
205 
206  gold << "gold/Generator_buzz3.wav";
207 
208  diff = data - gold;
209 
210  abs_diff = diff;
211  abs_diff.abs();
212 
213  if(abs_diff.getMax() > GAMMA)
214  {
215  cerr << TEST_ERROR_HEADER
216  << "Output did not match gold file!"
217  << endl;
218 
219  data.plot("data");
220  gold.plot("gold");
221  diff.plot("data - gold");
222  Plotter::show();
223 
224  exit(1);
225  }
226 
227  cout << SUCCESS;
228 
229  // Test drawSine
230  cout << TEST_HEADER << "Testing Generator::drawSine(1.0, 10.0) ...";
231 
232  gen = Generator(600.0);
233 
234  data = Buffer();
235  data << gen.drawSine(1.0, 10.0);
236 
237  // Create the gold file
238 //~ data >> "gold/Generator_sine1.wav";
239 
240  gold = Buffer("gold/Generator_sine1.wav");
241 
242  diff = data - gold;
243 
244  abs_diff = diff;
245  abs_diff.abs();
246 
247  if(abs_diff.getMax() > GAMMA)
248  {
249  cerr << TEST_ERROR_HEADER
250  << "Output did not match gold file!"
251  << endl;
252 
253  data.plot("data");
254  gold.plot("gold");
255  diff.plot("data - gold");
256  Plotter::show();
257 
258  exit(1);
259  }
260 
261  cout << SUCCESS;
262 
263  // Test drawSine
264  cout << TEST_HEADER << "Testing Generator::drawSine(1.0, freqs) ...";
265 
266  data = Buffer();
267 
268  Buffer freqs(gen.drawLine(1.0, 1.0, 10.0));
269 
270  data << gen.drawSine(1.0, freqs);
271 
272  // Create the gold file
273 //~ data >> "gold/Generator_sine2.wav";
274 
275  gold = Buffer("gold/Generator_sine2.wav");
276 
277  diff = data - gold;
278 
279  abs_diff = diff;
280  abs_diff.abs();
281 
282  if(abs_diff.getMax() > GAMMA)
283  {
284  cerr << TEST_ERROR_HEADER
285  << "Output did not match gold file!"
286  << endl;
287 
288  data.plot("data");
289  gold.plot("gold");
290  diff.plot("data - gold");
291  Plotter::show();
292 
293  exit(1);
294  }
295 
296  cout << SUCCESS;
297 
298  // Test drawSine
299  cout << TEST_HEADER << "Testing Generator::drawSine2(1.0, 3.0, 0.5) ...";
300 
301  data = Buffer();
302 
303  data << gen.drawSine2(1.0, 3.0, 0.5);
304 
305  // Create the gold file
306 //~ data >> "gold/Generator_sine3.wav";
307 
308  gold = Buffer("gold/Generator_sine3.wav");
309 
310  diff = data - gold;
311 
312  abs_diff = diff;
313  abs_diff.abs();
314 
315  if(abs_diff.getMax() > GAMMA)
316  {
317  cerr << TEST_ERROR_HEADER
318  << "Output did not match gold file!"
319  << endl;
320 
321  data.plot("data");
322  gold.plot("gold");
323  diff.plot("data - gold");
324  Plotter::show();
325 
326  exit(1);
327  }
328 
329  cout << SUCCESS;
330 
331  // Test drawSine
332  cout << TEST_HEADER << "Testing Generator::drawSine2(1.0, 3.0, phase) ...";
333 
334  data = Buffer();
335 
336  Buffer phase;
337  phase << gen.drawLine(1.0, 0.0, 1.0);
338 
339  data << gen.drawSine2(1.0, 3.0, phase);
340 
341  // Create the gold file
342 //~ data >> "gold/Generator_sine4.wav";
343 
344  gold = Buffer("gold/Generator_sine4.wav");
345 
346  diff = data - gold;
347 
348  abs_diff = diff;
349  abs_diff.abs();
350 
351  if(abs_diff.getMax() > GAMMA)
352  {
353  cerr << TEST_ERROR_HEADER
354  << "Output did not match gold file!"
355  << endl;
356 
357  data.plot("data");
358  gold.plot("gold");
359  diff.plot("data - gold");
360  Plotter::show();
361 
362  exit(1);
363  }
364 
365  cout << SUCCESS;
366 
367  // Test drawSine
368  cout << TEST_HEADER << "Testing Generator::drawSine2(1.0, freqs, 0.5) ...";
369 
370  data = Buffer();
371  freqs = Buffer();
372 
373  freqs << gen.drawLine(1.0, 0.0, 10.0);
374 
375  data << gen.drawSine2(1.0, freqs, 0.5);
376 
377  // Create the gold file
378 //~ data >> "gold/Generator_sine5.wav";
379 
380  gold = Buffer("gold/Generator_sine5.wav");
381 
382  diff = data - gold;
383 
384  abs_diff = diff;
385  abs_diff.abs();
386 
387  if(abs_diff.getMax() > GAMMA)
388  {
389  cerr << TEST_ERROR_HEADER
390  << "Output did not match gold file!"
391  << endl;
392 
393  data.plot("data");
394  gold.plot("gold");
395  diff.plot("data - gold");
396  Plotter::show();
397 
398  exit(1);
399  }
400 
401  cout << SUCCESS;
402 
403  // Test drawSine
404  cout << TEST_HEADER << "Testing Generator::drawSine2(1.0, freqs, phase) ...";
405 
406  data = Buffer();
407 
408  data << gen.drawSine2(1.0, freqs, phase);
409 
410  // Create the gold file
411 //~ data >> "gold/Generator_sine6.wav";
412 
413  gold = Buffer("gold/Generator_sine6.wav");
414 
415  diff = data - gold;
416 
417  abs_diff = diff;
418  abs_diff.abs();
419 
420  if(abs_diff.getMax() > GAMMA)
421  {
422  cerr << TEST_ERROR_HEADER
423  << "Output did not match gold file!"
424  << endl;
425 
426  data.plot("data");
427  gold.plot("gold");
428  diff.plot("data - gold");
429  Plotter::show();
430 
431  exit(1);
432  }
433 
434  cout << SUCCESS;
435 
436  // Test chorus
437  cout << TEST_HEADER << "Testing Generator::generate(1.0, freq) with chorus ...";
438 
439  data = Buffer();
440 
441  cos.setSeed(457745);
442 
443  cos.setChorus(2, 0.50);
444 
445  data << cos.generate(1.0, 5);
446 
447  // Create the gold file
448 //~ data >> "gold/Generator_chorus1.wav";
449 
450 //~ // Force a failure
451 //~ data[data.getLength() / 2] += data.getMax();
452 
453  gold = Buffer("gold/Generator_chorus1.wav");
454 
455  diff = data - gold;
456 
457  abs_diff = diff;
458  abs_diff.abs();
459 
460  if(abs_diff.getMax() > GAMMA)
461  {
462  cerr << TEST_ERROR_HEADER
463  << "Output did not match gold file!"
464  << endl;
465 
466  Plotter pylab;
467  pylab.plot(data, "r", "label='data'");
468  pylab.plot(gold, "b", "label='gold', lw=2");
469  pylab.grid(true);
470  pylab.legend();
471 
472  pylab.show();
473 
474  exit(1);
475  }
476 
477  cout << SUCCESS;
478 
479 
480  // Finish
481  cout << endl;
482 
483  Plotter::show();
484 }
void grid(boolean flag)
Sets the grid state.
Definition: Plotter.cc:496
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
static const char * THIS_FILE
DOXME.
Definition: Cosine.h:44
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
void legend(const std::string &kwargs="")
Shows the legend on the current axes.
Definition: Plotter.cc:745
#define TEST_ERROR_HEADER
Definition: Test.h:49
static const float64 GAMMA
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
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
void Sine_UnitTest ( )

Definition at line 51 of file Sine_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), GAMMA, Nsound::Generator::generate(), Nsound::Generator::generate2(), Nsound::Buffer::getMax(), Nsound::Buffer::plot(), Nsound::Generator::setChorus(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Generator::setSeed(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

52 {
53  cout << endl << THIS_FILE;
54 
55  Wavefile::setDefaultSampleSize(64);
56  Wavefile::setIEEEFloat(true);
57 
58  Sine sine(600);
59 
60  Buffer freqs;
61  Buffer phase;
62 
63  freqs << sine.drawLine(1.0, 0.0, 10.0);
64  phase << sine.drawLine(1.0, 0.0, 1.0);
65 
66  cout << TEST_HEADER << "Testing Sine::generate(1.0, 3.0) ...";
67 
68  Buffer data = sine.generate(1.0, 3.0);
69 
70  // Create the gold file
71 //~ data >> "gold/Sine_1.wav";
72 
73  Buffer gold;
74 
75  gold << "gold/Sine_1.wav";
76 
77  Buffer diff = data - gold;
78 
79  Buffer abs_diff(diff);
80  abs_diff.abs();
81 
82  if(abs_diff.getMax() > GAMMA)
83  {
84  cerr << TEST_ERROR_HEADER
85  << "Output did not match gold file!"
86  << endl;
87 
88  diff.plot("data - gold");
89  data.plot("data");
90  gold.plot("gold");
91 
92  Plotter::show();
93 
94  exit(1);
95  }
96 
97  cout << SUCCESS;
98 
99  // Test 3.5 Hz wave
100  cout << TEST_HEADER << "Testing Sine::generate(1.0, freqs) ...";
101 
102  data = sine.generate(1.0, freqs);
103 
104  // Create the gold file
105 //~ data >> "gold/Sine_2.wav";
106 
107  gold = Buffer();
108 
109  gold << "gold/Sine_2.wav";
110 
111  diff = data - gold;
112 
113  abs_diff = diff;
114  abs_diff.abs();
115 
116  if(abs_diff.getMax() > GAMMA)
117  {
118  cerr << TEST_ERROR_HEADER
119  << "Output did not match gold file!"
120  << endl;
121 
122  diff.plot("data - gold");
123  data.plot("data");
124  gold.plot("gold");
125 
126  Plotter::show();
127 
128  exit(1);
129  }
130 
131  cout << SUCCESS;
132 
133  // Test dynamic
134  cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, 0.5) ...";
135 
136  data = sine.generate2(1.0, 3.0, 0.5);
137 
138  // Create the gold file
139 //~ data >> "gold/Sine_3.wav";
140 
141  gold = Buffer();
142 
143  gold << "gold/Sine_3.wav";
144 
145  diff = data - gold;
146 
147  abs_diff = diff;
148  abs_diff.abs();
149 
150  if(abs_diff.getMax() > GAMMA)
151  {
152  cerr << TEST_ERROR_HEADER
153  << "Output did not match gold file!"
154  << endl;
155 
156  diff.plot("data - gold");
157  data.plot("data");
158  gold.plot("gold");
159 
160  Plotter::show();
161 
162  exit(1);
163  }
164 
165  cout << SUCCESS;
166 
167  // Test dynamic
168  cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, phase) ...";
169 
170  data = sine.generate2(1.0, 3.0, phase);
171 
172  // Create the gold file
173 //~ data >> "gold/Sine_4.wav";
174 
175  gold = Buffer();
176 
177  gold << "gold/Sine_4.wav";
178 
179  diff = data - gold;
180 
181  abs_diff = diff;
182  abs_diff.abs();
183 
184  if(abs_diff.getMax() > GAMMA)
185  {
186  cerr << TEST_ERROR_HEADER
187  << "Output did not match gold file!"
188  << endl;
189 
190  diff.plot("data - gold");
191  data.plot("data");
192  gold.plot("gold");
193 
194  Plotter::show();
195 
196  exit(1);
197  }
198 
199  cout << SUCCESS;
200 
201  // Test dynamic
202  cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, 0.5) ...";
203 
204  data = sine.generate2(1.0, freqs, 0.5);
205 
206  // Create the gold file
207 //~ data >> "gold/Sine_5.wav";
208 
209  gold = Buffer();
210 
211  gold << "gold/Sine_5.wav";
212 
213  diff = data - gold;
214 
215  abs_diff = diff;
216  abs_diff.abs();
217 
218  if(abs_diff.getMax() > GAMMA)
219  {
220  cerr << TEST_ERROR_HEADER
221  << "Output did not match gold file!"
222  << endl;
223 
224  diff.plot("data - gold");
225  data.plot("data");
226  gold.plot("gold");
227 
228  Plotter::show();
229 
230  exit(1);
231  }
232 
233  cout << SUCCESS;
234 
235  // Test dynamic
236  cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, phase) ...";
237 
238  data = sine.generate2(1.0, freqs, phase);
239 
240  // Create the gold file
241 //~ data >> "gold/Sine_6.wav";
242 
243  gold = Buffer();
244 
245  gold << "gold/Sine_6.wav";
246 
247  diff = data - gold;
248 
249  abs_diff = diff;
250  abs_diff.abs();
251 
252  if(abs_diff.getMax() > GAMMA)
253  {
254  cerr << TEST_ERROR_HEADER
255  << "Output did not match gold file!"
256  << endl;
257 
258  diff.plot("data - gold");
259  data.plot("data");
260  gold.plot("gold");
261 
262  Plotter::show();
263 
264  exit(1);
265  }
266 
267  cout << SUCCESS;
268 
269  // Test dynamic
270  cout << TEST_HEADER << "Testing Sine::setChorus(5,0.10) ...";
271 
272  sine.setSeed(6846513); // Some random seed I typed in.
273 
274  sine.setChorus(5, 0.10);
275 
276  data = sine.generate2(1.0, 2*freqs, phase);
277 
278  // Create the gold file
279 //~ data >> "gold/Sine_7.wav";
280 
281  gold = Buffer();
282 
283  gold << "gold/Sine_7.wav";
284 
285  diff = data - gold;
286 
287  abs_diff = diff;
288  abs_diff.abs();
289 
290  if(abs_diff.getMax() > GAMMA)
291  {
292  cerr << TEST_ERROR_HEADER
293  << "Output did not match gold file!"
294  << endl;
295 
296  diff.plot("data - gold");
297  data.plot("data");
298  gold.plot("gold");
299 
300  Plotter::show();
301 
302  exit(1);
303  }
304 
305  cout << SUCCESS << endl;
306 }
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
static const char * THIS_FILE
#define TEST_ERROR_HEADER
Definition: Test.h:49
static const float64 GAMMA
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
DOXME.
Definition: Sine.h:43
void Triangle_UnitTest ( )

Definition at line 51 of file Triangle_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), GAMMA, Nsound::Generator::generate(), Nsound::Buffer::getMax(), Nsound::Buffer::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

52 {
53  cout << endl << THIS_FILE;
54 
55  Wavefile::setDefaultSampleSize(64);
56  Wavefile::setIEEEFloat(true);
57 
58  Triangle tri(100);
59 
60  Buffer data;
61  Buffer gold;
62  Buffer diff;
63 
64  // Test 7 Hz wave
65  cout << TEST_HEADER << "Testing Triangle::generate(1.01, 2.0) ...";
66 
67  data = tri.generate(1.01, 2.0);
68 
69  // Create the gold file
70 //~ data >> "gold/Triangle_2Hz.wav";
71 
72  gold = Buffer("gold/Triangle_2Hz.wav");
73 
74  diff = data - gold;
75 
76  diff.abs();
77 
78  if(diff.getMax() > GAMMA)
79  {
80  cerr << TEST_ERROR_HEADER
81  << "Output did not match gold file!"
82  << endl;
83 
84  diff.plot("data - gold");
85  data.plot("data");
86  gold.plot("gold");
87 
88  Plotter::show();
89 
90  exit(1);
91  }
92 
93  cout << SUCCESS;
94 
95  // Test 3.5 Hz wave
96  cout << TEST_HEADER << "Testing Triangle::generate(1.01, 1.0) ...";
97 
98  data = tri.generate(1.01, 1.0);
99 
100  // Create the gold file
101 //~ data >> "gold/Triangle_1.0Hz.wav";
102 
103  gold = Buffer("gold/Triangle_1.0Hz.wav");
104 
105  diff = data - gold;
106 
107  diff.abs();
108 
109  if(diff.getMax() > GAMMA)
110  {
111  cerr << TEST_ERROR_HEADER
112  << "Output did not match gold file!"
113  << endl;
114 
115  diff.plot("data - gold");
116  data.plot("data");
117  gold.plot("gold");
118 
119  Plotter::show();
120 
121  exit(1);
122  }
123 
124  cout << SUCCESS;
125 
126  // Test dynamic
127  cout << TEST_HEADER << "Testing Triangle::generate(1.01, frequencies) ...";
128 
129  Buffer freqs = tri.drawLine(1.01, 1.0, 5.0);
130 
131  data = tri.generate(1.01, freqs);
132 
133  // Create the gold file
134 //~ data >> "gold/Triangle_1_to_5Hz.wav";
135 
136  gold = Buffer("gold/Triangle_1_to_5Hz.wav");
137 
138  diff = data - gold;
139 
140  diff.abs();
141 
142  if(diff.getMax() > GAMMA)
143  {
144  cerr << TEST_ERROR_HEADER
145  << "Output did not match gold file!"
146  << endl;
147 
148  diff.plot("data - gold");
149  data.plot("data");
150  gold.plot("gold");
151 
152  Plotter::show();
153 
154  exit(1);
155  }
156 
157  Plotter::show();
158 
159  cout << SUCCESS << endl;
160 }
Triangle generator.
Definition: Triangle.h:48
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
#define TEST_ERROR_HEADER
Definition: Test.h:49
static const char * THIS_FILE
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
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
static const float64 GAMMA
void Wavefile_UnitTest ( )

Definition at line 54 of file Wavefile_UnitTest.cc.

References Nsound::AudioStream::abs(), GAMMA, Nsound::Generator::generate(), Nsound::AudioStream::plot(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

55 {
56  cout << endl << THIS_FILE;
57 
58  Wavefile::setDefaultSampleSize(48);
59 
60  Sine sin(100);
61 
62  AudioStream data1(100, 3);
63 
64  data1 << sin.generate(1.0, 13.0);
65 
66  data1[1] *= 0.666666666666666;
67  data1[2] *= 0.333333333333333;
68 
69  // Write out 10 channel 13 Hz wave.
70  cout << TEST_HEADER << "Testing Wavefile::write(48-bit), read(48-bit) ..." << flush;
71 
72  data1 >> "test_wavefile.wav";
73 
74  AudioStream data(100, 3);
75 
76  data << "test_wavefile.wav";
77 
78  AudioStream gold(100, 3);
79 
80  // Create gold file
81 //~ data >> "gold/Wavefile_out1.wav";
82 
83  gold << "gold/Wavefile_out1.wav";
84 
85  AudioStream diff = data - gold;
86 
87  AudioStream abs_diff(diff);
88  abs_diff.abs();
89 
90  for(int32 i = 0; i < 3; ++i)
91  {
92  if(abs_diff[i].getMax() > GAMMA)
93  {
94  cerr << TEST_ERROR_HEADER
95  << "Output did not match gold file!"
96  << endl;
97 
98  diff[i].plot("data - gold");
99  data[i].plot("data");
100  gold[i].plot("gold");
101 
102  Plotter::show();
103 
104  exit(1);
105  }
106  }
107 
108  cout << SUCCESS;
109 
110  // Write out 10 channel 13 Hz wave again but with 24 bits per sample
111  cout << TEST_HEADER << "Testing Wavefile::write(24-bit), read(24-bit) ..." << flush;
112 
113  Wavefile::setDefaultSampleSize(24);
114 
115  data1 >> "test_wavefile2.wav";
116 
117  data = AudioStream(100, 3);
118  data << "test_wavefile2.wav";
119 
120  // Create gold file
121 //~ data >> "gold/Wavefile_out2.wav";
122 
123  gold = AudioStream(100, 3);
124  gold << "gold/Wavefile_out2.wav";
125 
126  diff = data - gold;
127 
128  abs_diff = diff;
129  abs_diff.abs();
130 
131  for(int32 i = 0; i < 1; ++i)
132  {
133  if(abs_diff[i].getMax() > 0.5 * GAMMA)
134  {
135  cerr << TEST_ERROR_HEADER
136  << "Output did not match gold file!"
137  << endl;
138 
139  diff[i].plot("data - gold");
140  data[i].plot("data");
141  gold[i].plot("gold");
142 
143  Plotter::show();
144 
145  exit(1);
146  }
147  }
148 
149  cout << SUCCESS << endl;
150 }
static const float64 GAMMA
#define TEST_HEADER
Definition: Test.h:45
#define TEST_ERROR_HEADER
Definition: Test.h:49
void plot(const std::string &title="AudioStream") const
Definition: AudioStream.cc:661
#define SUCCESS
Definition: UnitTest.h:42
static const char * THIS_FILE
signed int int32
Definition: Nsound.h:142
void abs()
This method calls abs on all buffers held in the stream.
Definition: AudioStream.cc:114
DOXME.
Definition: Sine.h:43