You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4KB

  1. #include "asserts.h"
  2. #include "poly.h"
  3. #include "Analyzer.h"
  4. #include "SinOscillator.h"
  5. static void test0()
  6. {
  7. Poly<float, 11> poly;
  8. float x = poly.run(0);
  9. assertEQ(x, 0);
  10. x = poly.run(1);
  11. assertEQ(x, 0);
  12. poly.setGain(0, 1);
  13. x = poly.run(0);
  14. assertEQ(x, 0);
  15. }
  16. static void test1()
  17. {
  18. Poly<float, 11> poly;
  19. poly.setGain(0, 1);
  20. float x = poly.run(1);
  21. assertNE(x, 0);
  22. }
  23. static void testPureTerm(int term)
  24. {
  25. float desiredFreq = 500.0f;
  26. int numSamples = 16 * 1024;
  27. const float sampleRate = 44100.0f;
  28. double actualFreq = Analyzer::makeEvenPeriod(desiredFreq, sampleRate, numSamples);
  29. SinOscillatorParams<float> sinParams;
  30. SinOscillatorState<float> sinState;
  31. SinOscillator<float, false>::setFrequency(sinParams, float(actualFreq / sampleRate));
  32. Poly<double, 11> poly;
  33. poly.setGain(term, 1);
  34. FFTDataCpx spectrum(numSamples);
  35. Analyzer::getSpectrum(spectrum, false, [&sinState, &sinParams, &poly]() {
  36. float sin = SinOscillator<float, false>::run(sinState, sinParams);
  37. const float x = poly.run(sin);
  38. return x;
  39. });
  40. const int order = term + 1;
  41. Analyzer::assertSingleFreq(spectrum, float(actualFreq * order), sampleRate);
  42. }
  43. static void testTerms()
  44. {
  45. for (int i = 0; i < 10; ++i) {
  46. testPureTerm(i);
  47. }
  48. }
  49. void testPoly()
  50. {
  51. test0();
  52. test1();
  53. testTerms();
  54. }