Run a full logarithmic frequency sweep and collect per-frame diagnostics.
| 54 | |
| 55 | // Run a full logarithmic frequency sweep and collect per-frame diagnostics. |
| 56 | fl::vector<SweepFrame> runContinuousSweep(int numFrames, int bands, float fmin, |
| 57 | float fmax, int sampleRate, |
| 58 | int samplesPerFrame, |
| 59 | fl::audio::fft::Mode mode) { |
| 60 | fl::audio::fft::Args args(samplesPerFrame, bands, fmin, fmax, sampleRate, mode); |
| 61 | fl::audio::fft::Impl fft(args); |
| 62 | |
| 63 | fl::vector<SweepFrame> frames; |
| 64 | frames.reserve(numFrames); |
| 65 | |
| 66 | float logRatio = fl::logf(fmax / fmin); |
| 67 | |
| 68 | for (int f = 0; f < numFrames; ++f) { |
| 69 | float t = static_cast<float>(f) / static_cast<float>(numFrames - 1); |
| 70 | float freq = fmin * fl::expf(logRatio * t); |
| 71 | |
| 72 | auto samples = makeSine(freq, samplesPerFrame, static_cast<float>(sampleRate)); |
| 73 | fl::audio::fft::Bins bins(bands); |
| 74 | fft.run(samples, &bins); |
| 75 | |
| 76 | SweepFrame frame; |
| 77 | frame.frequency = freq; |
| 78 | frame.peakBin = 0; |
| 79 | frame.peakEnergy = 0.0f; |
| 80 | frame.totalEnergy = 0.0f; |
| 81 | frame.activeBins = 0; |
| 82 | |
| 83 | for (int i = 0; i < bands; ++i) { |
| 84 | float e = bins.raw()[i]; |
| 85 | frame.totalEnergy += e; |
| 86 | if (e > frame.peakEnergy) { |
| 87 | frame.peakEnergy = e; |
| 88 | frame.peakBin = i; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | float threshold = frame.peakEnergy * 0.1f; |
| 93 | for (int i = 0; i < bands; ++i) { |
| 94 | if (bins.raw()[i] > threshold) { |
| 95 | frame.activeBins++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | frame.concentration = (frame.totalEnergy > 0.0f) |
| 100 | ? frame.peakEnergy / frame.totalEnergy |
| 101 | : 0.0f; |
| 102 | frame.expectedBin = bins.freqToBin(freq); |
| 103 | frames.push_back(frame); |
| 104 | } |
| 105 | |
| 106 | return frames; |
| 107 | } |
| 108 | |
| 109 | int countNonMonotonicSteps(fl::span<const SweepFrame> frames) { |
| 110 | int count = 0; |