Files
rustytorch/crates/specialized/rtx-neuro-realtime/src/lib.rs
T
2026-03-04 00:08:42 +00:00

97 lines
3.4 KiB
Rust

//! Real-Time GPU-Accelerated MEG/EEG Pipeline for Brain-Computer Interfaces
//!
//! This crate provides a sub-10ms latency pipeline for real-time source localization
//! from MEG/EEG sensor data, enabling closed-loop brain-computer interfaces.
//!
//! # Features
//!
//! - **GPU-accelerated filtering**: Batch FIR/IIR filtering on Metal/CUDA
//! - **GPU beamformer**: LCMV spatial filtering with zero-copy GPU operations
//! - **Streaming pipeline**: Ring buffer processing with configurable latency
//! - **BCI interface**: High-level API for brain-computer interface applications
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────┐ ┌──────────────┐ ┌─────────────┐ ┌──────────────┐
//! │ LSL Stream │───▶│ GPU Filter │───▶│GPU Beamform │───▶│ Source Est. │
//! │ (1kHz) │ │ (Metal/CU) │ │ (LCMV) │ │ (<10ms) │
//! └─────────────┘ └──────────────┘ └─────────────┘ └──────────────┘
//! │ │ │
//! └───────────────────┴───────────────────┘
//! GPU Memory (Zero-Copy)
//! ```
//!
//! # Latency Breakdown (Target)
//!
//! | Stage | CPU | GPU Target |
//! |-------|-----|------------|
//! | LSL receive | 1ms | 1ms |
//! | Bandpass filter | 5ms | 0.5ms |
//! | Beamformer | 20ms | 2ms |
//! | Source estimate | 10ms | 1ms |
//! | **Total** | **36ms** | **<5ms** |
//!
//! # Example
//!
//! ```rust,no_run
//! use rtx_neuro_realtime::{BciPipeline, BciConfig};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure pipeline for 1kHz EEG
//! let config = BciConfig {
//! sample_rate: 1000.0,
//! n_channels: 64,
//! buffer_duration_ms: 100.0,
//! filter_low: 8.0,
//! filter_high: 30.0,
//! ..Default::default()
//! };
//!
//! // Create pipeline
//! let mut pipeline = BciPipeline::new(config)?;
//!
//! // Set up source callback
//! pipeline.on_source_update(|signal| {
//! println!("Control signal: {}", signal.combined_signal);
//! });
//!
//! // Start processing
//! pipeline.start()?;
//!
//! // Push samples from your data source
//! // pipeline.push_sample(data, timestamp);
//!
//! Ok(())
//! }
//! ```
#![warn(missing_docs)]
pub mod bci;
pub mod error;
pub mod gpu_beamformer;
pub mod gpu_filter;
pub mod latency;
pub mod pipeline;
pub mod ring_buffer;
pub use bci::{BciConfig, BciPipeline, ControlSignal, SourceCallback};
pub use error::{RealtimeError, RealtimeResult};
pub use gpu_beamformer::{GpuBeamformer, GpuBeamformerConfig};
pub use gpu_filter::{FilterType, GpuFilter, GpuFilterConfig};
pub use latency::{LatencyMonitor, LatencyStage, LatencyStats};
pub use pipeline::{PipelineConfig, PipelineState, RealtimePipeline};
pub use ring_buffer::{RingBuffer, TimestampedSample};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_crate_compiles() {
// Smoke test - crate structure is sound
assert!(true);
}
}