93template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = Eigen::MatrixXd>
146 std::source_location::current(),
"The recording can only be shared before the analyzer starts");
164 [[nodiscard]]
size_t record(std::span<const double> channel)
168 const size_t windows = channel.size() < cfg.window_size
170 : (channel.size() - cfg.window_size) / cfg.hop_size + 1;
175 const std::vector<double> taper = cfg.taper ?
Kinesis::Discrete::hann(cfg.window_size) : std::vector<double> {};
178 { .length = cfg.window_size, .stride = cfg.hop_size, .offset = 0, .count =
static_cast<uint32_t
>(windows) }, taper);
200 return safe_any_cast_or_throw<SpectralAnalysis>(result);
218 [[nodiscard]] std::string
get_name()
const override
220 return "SpectralAnalyzer";
241 const std::vector<double> taper = cfg.taper ?
Kinesis::Discrete::hann(cfg.window_size) : std::vector<double> {};
242 const size_t rows =
context.descriptors();
243 const size_t bands = rows - 2;
247 analysis.
channels.resize(data_span.size());
249 std::vector<std::vector<double>> channel_values;
250 channel_values.reserve(data_span.size());
252 for (
size_t channel = 0; channel < data_span.size(); ++channel) {
253 const std::span<const double> samples { data_span[channel].data(), data_span[channel].size() };
254 const size_t windows = samples.size() < cfg.window_size
256 : (samples.size() - cfg.window_size) / cfg.hop_size + 1;
258 auto& result = analysis.
channels[channel];
260 const auto projected =
context.transform(samples,
261 { .length = cfg.window_size, .stride = cfg.hop_size, .offset = 0, .count =
static_cast<uint32_t
>(windows) },
263 fill_channel(result, projected, windows, rows, bands, cfg);
265 channel_values.push_back(result.values);
275 }
catch (
const std::exception& e) {
298 const auto fail = [](std::string_view message) {
300 std::source_location::current(),
"{}", message);
303 fail(
"Window size must be a power of two from 2 to 2048");
306 fail(
"Hop size must be greater than 0");
309 fail(
"At least two band edges are needed");
311 for (
size_t i = 0; i < cfg.
band_edges.size(); ++i) {
313 if (std::isnan(edge) || edge < 0.0 || edge > 1.0 || (i > 0 && edge <= cfg.
band_edges[i - 1])) {
314 fail(
"Band edges must ascend strictly within 0 to 1");
324 const size_t bins =
static_cast<size_t>(cfg.
window_size) / 2 + 1;
325 const size_t bands = cfg.
band_edges.size() - 1;
326 std::vector<double> weights((2 + bands) * bins, 0.0);
327 for (
size_t k = 0; k < bins; ++k) {
328 const double fraction =
static_cast<double>(k) /
static_cast<double>(bins - 1);
330 weights[bins + k] =
static_cast<double>(k);
331 for (
size_t b = 0; b < bands; ++b) {
332 const bool last = b + 1 == bands;
334 weights[(2 + b) * bins + k] = 1.0;
358 out.
values.resize(windows);
362 for (
size_t w = 0; w < windows; ++w) {
363 const double* row = projected.data() + w * rows;
365 const size_t start = w * cfg.
hop_size;
370 out.
values[w] = row[0] > 0.0 ? row[1] / row[0] : 0.0;
375 for (
size_t b = 0; b < bands; ++b) {
Modern, digital-first analyzer framework for Maya Flux.
#define MF_ERROR(comp, ctx,...)
#define MF_DESCRIBE(...)
Requests a generated member description for a struct at namespace scope.
Batched FFT of doubles on the GPU, exactly, with no fp64 on the device.
Discrete taper (window) coefficient generation and in-place application for MayaFlux::Kinesis.
void set(T value)
Stores a struct under its type identity.
bool is_initialized() const
Check if compiler is initialized.
void store_current_analysis(AnalysisResultType &&result) const
std::any get_current_analysis() const
Access cached analysis from last operation.
std::any analyze_data(const input_type &data)
User-facing analysis method - returns analysis results directly.
Template-flexible analyzer base with instance-defined I/O types.
const Config & config() const
void set_config(Config config)
Stores a config struct, whole, under its own type.
ConfigRefs stored_configs(Supports< Configs... >) const
The stored configs among those listed, in listed order.
output_type convert_result(std::vector< std::vector< double > > &result_data, DataStructureInfo &metadata)
Convert processed double data back to OutputType using metadata and optional callback.
static constexpr uint32_t result_binding
The binding that holds the result of a dispatch.
One FFT per window, as one dispatch of the FFT template.
Non-template base that owns all type-independent GPU dispatch logic.
static std::tuple< NumericChannels, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from Datum container or direct ComputeData with automatic container ha...
SpectralBuffer descriptor_buffer()
The device buffer of the descriptors of the latest record.
size_t record(std::span< const double > channel)
Transform the windows of one channel without waiting for the result.
Analyzer< InputType, OutputType > base_type
std::unique_ptr< FftContext > m_context
static float fitness(const input_type &)
One while a device is up, zero otherwise.
std::shared_ptr< GpuDispatchRecorder > m_recording
ConfigRefs configs() const override
The base view of the config structs this operation holds.
Datum< InputType > input_type
size_t m_resident_windows
void share_recording(std::shared_ptr< GpuDispatchRecorder > recording)
Append to a recording the caller shares with other work.
static std::vector< double > weights_for(const SpectralAnalyzerConfig &cfg)
Rows of weights: total, first moment, then one row per band.
std::string get_name() const override
Get operation name for debugging/introspection.
output_type analyze_implementation(const input_type &input) override
Pure virtual analysis implementation - derived classes implement this.
SpectralAnalysis analyze_spectrum(const input_type &data)
Type-safe spectral analysis.
AnalysisType get_analysis_type() const override
Gets the analysis type category for this analyzer.
SpectralAnalysis analyze_spectrum(const InputType &data)
static void fill_channel(ChannelSpectrum &out, const std::vector< double > &projected, size_t windows, size_t rows, size_t bands, const SpectralAnalyzerConfig &cfg)
Supports< SpectralAnalyzerConfig > supports
static constexpr Target target
SpectralAnalyzer(SpectralAnalyzerConfig config={})
Construct a spectral analyzer from its settings.
FftContext & context(const SpectralAnalyzerConfig &cfg) const
SpectralAnalysis get_spectral_analysis() const
Datum< OutputType > output_type
static void validate_config(const SpectralAnalyzerConfig &cfg)
Windowed spectral descriptors of every channel, run on the GPU.
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP).
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
std::vector< double > hann(size_t n)
Hann (raised cosine) taper coefficients.
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
AnalysisType
Categories of analysis operations for discovery and organization.
@ SPECTRAL
FFT, frequency domain analysis.
SpectralAnalyzer< std::shared_ptr< Kakshya::SignalSourceContainer >, Eigen::MatrixXd > ContainerSpectralAnalyzer
Container spectral analyzer: SignalContainer -> MatrixXd.
std::vector< std::reference_wrapper< const OperationConfig > > ConfigRefs
Borrowed views of the configs an operation holds.
SpectralMethod
The per-window value a SpectralAnalyzer reports as its output data.
@ CENTROID
Power weighted mean bin index, zero for a silent window.
@ TOTAL_POWER
Sum of the power spectrum over the bins 0 to window_size / 2.
@ FLUX
Sum over bands of the rise in band energy since the previous window, zero for the first.
SpectralAnalyzer< std::vector< Kakshya::DataVariant >, Eigen::MatrixXd > StandardSpectralAnalyzer
Standard spectral analyzer: DataVariant -> MatrixXd.
Stores a scoped enum in its underlying integer type.
uint32_t window_size
Analysis window length in samples.
uint32_t hop_size
Distance between successive windows in samples.
Settings every analyzer family accepts.
std::vector< std::pair< size_t, size_t > > window_positions
First and one past the last sample of each window.
std::vector< std::vector< double > > band_energies
Power in each band, per window.
std::vector< double > values
The value selected by the method.
Spectral descriptors of one channel, one entry per window.
Facets facets
Typed results that travel with the data, read by type.
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Input/Output container for computation pipeline data flow with structure preservation.
SpectralAnalyzerConfig config
Settings the analysis was produced under.
std::vector< ChannelSpectrum > channels
Analysis result of a SpectralAnalyzer.
bool taper
Multiply each window by a Hann taper before the transform.
Reflect::EnumByte< SpectralMethod > method
Value reported as the output data, as a SpectralMethod value.
std::vector< double > band_edges
Ascending band boundaries as fractions of the Nyquist frequency, 0 to 1.
Settings of a SpectralAnalyzer.
bool operator==(const ContextKey &) const =default
std::vector< double > band_edges
const GpuDispatchCore * context
Where the descriptors of the latest record are on the device.
The config structs an operation class accepts, declared as using supports.
A set of (device, runtime) pairs.