+template<bool fast>
+std::vector<pulse> detect_pulses(const std::vector<float> &pcm, float hysteresis_upper_limit, float hysteresis_lower_limit, int sample_rate)
+{
+ std::vector<pulse> pulses;
+
+ // Find the flanks.
+ enum State { START, ABOVE, BELOW } state = START;
+ double last_downflank = -1;
+ for (unsigned i = 0; i < pcm.size(); ++i) {
+ if (pcm[i] > hysteresis_upper_limit) {
+ state = ABOVE;
+ } else if (pcm[i] < hysteresis_lower_limit) {
+ if (state == ABOVE) {
+ // down-flank!
+ double t = find_crossing<fast>(pcm, i - 1, hysteresis_lower_limit) * (1.0 / sample_rate) + crop_start;
+ if (last_downflank > 0) {
+ pulse p;
+ p.time = t;
+ p.len = t - last_downflank;
+ pulses.push_back(p);
+ }
+ last_downflank = t;
+ }
+ state = BELOW;
+ }
+ }
+ return pulses;
+}
+
+void output_cycle_plot(const std::vector<pulse> &pulses, double calibration_factor)
+{
+ FILE *fp = fopen("cycles.plot", "w");
+ for (unsigned i = 0; i < pulses.size(); ++i) {
+ double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
+ fprintf(fp, "%f %f\n", pulses[i].time, cycles);
+ }
+ fclose(fp);
+}
+
+std::pair<int, double> find_closest_point(double x, const std::vector<float> &points)
+{
+ int best_point = 0;
+ double best_dist = (x - points[0]) * (x - points[0]);
+ for (unsigned j = 1; j < train_snap_points.size(); ++j) {
+ double dist = (x - points[j]) * (x - points[j]);
+ if (dist < best_dist) {
+ best_point = j;
+ best_dist = dist;
+ }
+ }
+ return std::make_pair(best_point, best_dist);
+}
+
+float eval_badness(const std::vector<pulse>& pulses, double calibration_factor)
+{
+ double sum_badness = 0.0;
+ for (unsigned i = 0; i < pulses.size(); ++i) {
+ double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
+ if (cycles > 2000.0) cycles = 2000.0; // Don't make pauses arbitrarily bad.
+ std::pair<int, double> selected_point_and_sq_dist = find_closest_point(cycles, train_snap_points);
+ sum_badness += selected_point_and_sq_dist.second;
+ }
+ return sqrt(sum_badness / (pulses.size() - 1));
+}
+
+void find_kmeans(const std::vector<pulse> &pulses, double calibration_factor, const std::vector<float> &initial_centers)
+{
+ std::vector<float> last_centers = initial_centers;
+ std::vector<float> sums;
+ std::vector<float> num;
+ sums.resize(initial_centers.size());
+ num.resize(initial_centers.size());
+ for ( ;; ) {
+ for (unsigned i = 0; i < initial_centers.size(); ++i) {
+ sums[i] = 0.0f;
+ num[i] = 0;
+ }
+ for (unsigned i = 0; i < pulses.size(); ++i) {
+ double cycles = pulses[i].len * calibration_factor * C64_FREQUENCY;
+ // Ignore heavy outliers, which are almost always long pauses.
+ if (cycles > 2000.0) {
+ continue;
+ }
+ std::pair<int, double> selected_point_and_sq_dist = find_closest_point(cycles, last_centers);
+ int p = selected_point_and_sq_dist.first;
+ sums[p] += cycles;
+ ++num[p];
+ }
+ bool any_moved = false;
+ for (unsigned i = 0; i < initial_centers.size(); ++i) {
+ if (num[i] == 0) {
+ fprintf(stderr, "K-means broke down, can't output new reference training points\n");
+ return;
+ }
+ float new_center = sums[i] / num[i];
+ if (fabs(new_center - last_centers[i]) > 1e-3) {
+ any_moved = true;
+ }
+ last_centers[i] = new_center;
+ }
+ if (!any_moved) {
+ break;
+ }
+ }
+ fprintf(stderr, "New reference training points:");
+ for (unsigned i = 0; i < last_centers.size(); ++i) {
+ fprintf(stderr, " %.3f", last_centers[i]);
+ }
+ fprintf(stderr, "\n");
+}
+
+void spsa_train(const std::vector<float> &pcm, int sample_rate)
+{
+ float vals[NUM_SPSA_VALS] = { hysteresis_upper_limit, hysteresis_lower_limit, 1.0f }; // The rest is filled with 0.
+
+ float start_c = INITIAL_C;
+ double best_badness = HUGE_VAL;
+
+ for (int n = 1; n < NUM_ITER; ++n) {
+ float a = INITIAL_A * pow(n + A, -ALPHA);
+ float c = start_c * pow(n, -GAMMA);
+
+ // find a random perturbation
+ float p[NUM_SPSA_VALS];
+ float vals1[NUM_SPSA_VALS], vals2[NUM_SPSA_VALS];
+ for (int i = 0; i < NUM_SPSA_VALS; ++i) {
+ p[i] = (rand() % 2) ? 1.0 : -1.0;
+ vals1[i] = std::max(std::min(vals[i] - c * p[i], 1.0f), -1.0f);
+ vals2[i] = std::max(std::min(vals[i] + c * p[i], 1.0f), -1.0f);
+ }
+
+ std::vector<pulse> pulses1 = detect_pulses<true>(do_fir_filter(pcm, vals1 + 2), vals1[0], vals1[1], sample_rate);
+ std::vector<pulse> pulses2 = detect_pulses<true>(do_fir_filter(pcm, vals2 + 2), vals2[0], vals2[1], sample_rate);
+ float badness1 = eval_badness(pulses1, 1.0);
+ float badness2 = eval_badness(pulses2, 1.0);
+
+ // Find the gradient estimator
+ float g[NUM_SPSA_VALS];
+ for (int i = 0; i < NUM_SPSA_VALS; ++i) {
+ g[i] = (badness2 - badness1) / (2.0 * c * p[i]);
+ vals[i] -= a * g[i];
+ vals[i] = std::max(std::min(vals[i], 1.0f), -1.0f);
+ }
+ if (badness2 < badness1) {
+ std::swap(badness1, badness2);
+ std::swap(vals1, vals2);
+ std::swap(pulses1, pulses2);
+ }
+ if (badness1 < best_badness) {
+ fprintf(stderr, "\nNew best filter (badness=%f):", badness1);
+ for (int i = 0; i < NUM_FILTER_COEFF; ++i) {
+ fprintf(stderr, " %.5f", vals1[i + 2]);
+ }
+ fprintf(stderr, ", hysteresis limits = %f %f\n", vals1[0], vals1[1]);
+ best_badness = badness1;
+
+ find_kmeans(pulses1, 1.0, train_snap_points);
+
+ if (output_cycles_plot) {
+ output_cycle_plot(pulses1, 1.0);
+ }
+ }
+ fprintf(stderr, "%d ", n);
+ fflush(stderr);
+ }
+}
+