+
+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);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ parse_options(argc, argv);
+
+ make_lanczos_weight_table();
+ std::vector<float> pcm;
+ int sample_rate;
+ if (!read_audio_file(argv[optind], &pcm, &sample_rate)) {
+ exit(1);
+ }
+
+ if (do_crop) {
+ pcm = crop(pcm, crop_start, crop_end, sample_rate);
+ }
+
+ if (use_fir_filter) {
+ pcm = do_fir_filter(pcm, filter_coeff);
+ }
+
+ if (use_rc_filter) {
+ pcm = do_rc_filter(pcm, rc_filter_freq, sample_rate);
+ }
+
+ if (do_auto_level) {
+ pcm = level_samples(pcm, min_level, auto_level_freq, sample_rate);
+ if (output_leveled) {
+ FILE *fp = fopen("leveled.raw", "wb");
+ fwrite(pcm.data(), pcm.size() * sizeof(pcm[0]), 1, fp);
+ fclose(fp);
+ }
+ }
+
+#if 0
+ for (int i = 0; i < LEN; ++i) {
+ in[i] += rand() % 10000;
+ }
+#endif
+
+#if 0
+ for (int i = 0; i < LEN; ++i) {
+ printf("%d\n", in[i]);
+ }
+#endif
+
+ if (do_train) {
+ spsa_train(pcm, sample_rate);
+ exit(0);
+ }
+
+ std::vector<pulse> pulses = detect_pulses<false>(pcm, hysteresis_upper_limit, hysteresis_lower_limit, sample_rate);
+
+ double calibration_factor = 1.0;
+ if (do_calibrate) {
+ calibration_factor = calibrate(pulses);
+ }
+
+ if (output_cycles_plot) {
+ output_cycle_plot(pulses, calibration_factor);
+ }
+
+ output_tap(pulses, calibration_factor);
+}