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.

84 lines
2.6KB

  1. #pragma once
  2. #include <set>
  3. #include <vector>
  4. #include <functional>
  5. #include "FFTData.h"
  6. #include "FFT.h"
  7. class Analyzer
  8. {
  9. public:
  10. Analyzer() = delete;
  11. class FPoint
  12. {
  13. public:
  14. FPoint(float f, float g) : freq(f), gainDb(g)
  15. {
  16. }
  17. float freq;
  18. float gainDb;
  19. };
  20. static std::vector<FPoint> getFeatures(const FFTDataCpx&, float sensitivityDb, float sampleRate, float minDb);
  21. static std::vector<FPoint> getPeaks(const FFTDataCpx&, float sampleRate, float minDb);
  22. static void getAndPrintFeatures(const FFTDataCpx&, float sensitivityDb, float sampleRate, float minDb);
  23. static void getAndPrintPeaks(const FFTDataCpx&, float sampleRate, float minDb);
  24. static void getAndPrintFreqOfInterest(const FFTDataCpx&, float sampleRate, const std::vector<double>& freqOfInterest);
  25. static int getMax(const FFTDataCpx&);
  26. static int getMaxExcluding(const FFTDataCpx&, std::set<int> exclusions);
  27. static int getMaxExcluding(const FFTDataCpx&, int exclusion);
  28. /**
  29. * 0 = low freq bin #
  30. * 1 = peak bin #
  31. * 2 = high bin#
  32. * dbAtten (typically -3
  33. */
  34. static std::tuple<int, int, int> getMaxAndShoulders(const FFTDataCpx&, float dbAtten);
  35. /**
  36. * 0 = low freq
  37. * 1 = peak freq
  38. * 2 = high freq
  39. * dbAtten (typically -3
  40. */
  41. static std::tuple<double, double, double> getMaxAndShouldersFreq(const FFTDataCpx&, float dbAtten, float sampleRate);
  42. /**
  43. * Calculates the frequency response of func
  44. * by calling it with a known test signal.
  45. */
  46. static void getFreqResponse(FFTDataCpx& out, std::function<float(float)> func);
  47. /**
  48. * Calculates the spectrum of func
  49. * by calling it can capturing its output
  50. */
  51. static void getSpectrum(FFTDataCpx& out, bool useWindow, std::function<float()> func);
  52. static float getSlope(const FFTDataCpx& response, float fTest, float sampleRate);
  53. static void generateSweep(float sampleRate, float* out, int numSamples, float minFreq, float maxFreq);
  54. /**
  55. * Adjusts desiredFreq to a frequency that is close, but is an exact division of
  56. * numSamples.
  57. */
  58. static double makeEvenPeriod(double desiredFreq, double sampleRate, int numSamples);
  59. static double hamming(int iSample, int totalSamples);
  60. /**
  61. * Assert that there is a single frequency in spectrum, and that it is close to
  62. * expectedFreq.
  63. *
  64. * In other words, check that the signal was a reasonably pure sin.
  65. */
  66. static void assertSingleFreq(const FFTDataCpx& spectrum, float expectedFreq, float sampleRate);
  67. };