Files
Mac_mini_8micArray/PC_SoftWare/cpp/doa_cli.cpp
T
2026-09-29 22:41:29 +08:00

51 lines
1.9 KiB
C++

#include "doa_algorithms.hpp"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace esp32_doa;
int main(int argc, char **argv) {
if (argc < 3 || argc > 6) {
std::cerr << "usage: doa_cli <srp|normmusic> <matrix.txt> [sample_rate] [nfft]\n"
" doa_cli mvdr <matrix.txt> <angle> [sample_rate] [nfft]\n";
return 2;
}
try {
std::ifstream input(argv[2]);
if (!input) throw std::runtime_error("cannot open input matrix");
int samples = 0, channels = 0;
input >> samples >> channels;
Config config;
config.channels = channels;
const std::string command = argv[1];
if (command == "mvdr") {
if (argc < 4) throw std::runtime_error("mvdr requires an angle");
if (argc >= 5) config.sample_rate = std::stoi(argv[4]);
if (argc >= 6) config.nfft = std::stoi(argv[5]);
} else {
if (argc >= 4) config.sample_rate = std::stoi(argv[3]);
if (argc >= 5) config.nfft = std::stoi(argv[4]);
}
config.hop = config.nfft / 2;
std::vector<double> audio(static_cast<std::size_t>(samples) * channels);
for (double &value : audio)
if (!(input >> value)) throw std::runtime_error("matrix has too few values");
std::cout << std::setprecision(17);
if (command == "mvdr") {
const auto result = beamform(audio, samples, config, std::stod(argv[3]));
for (double value : result.audio) std::cout << value << '\n';
} else {
const auto result = estimate(audio, samples, config, command);
std::cout << "{\"angle_degrees\":" << result.angle_degrees
<< ",\"peak_sharpness\":" << result.peak_sharpness << "}\n";
}
} catch (const std::exception &error) {
std::cerr << "error: " << error.what() << '\n';
return 1;
}
return 0;
}