Skip to content

riccardostokker/ltspice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LTSpice Raw File Parser

A Rust library for parsing LTSpice .raw simulation output files.

Features

  • Parse binary .raw files from LTSpice simulations
  • Support for UTF-8 (Linux/macOS) and UTF-16 LE (Windows) encodings
  • Handle all simulation types:
    • Transient Analysis (.tran)
    • AC Analysis (.ac)
    • DC Sweep (.dc)
    • FFT Analysis
    • Noise Analysis (.noise)
    • Operating Point (.op)
  • Support for parametric (stepped) simulations
  • Access to both real and complex-valued data
  • Proper error handling with descriptive error types

Installation

Add this to your Cargo.toml:

[dependencies]
ltspice = "0.2"

Quick Start

use ltspice::Simulation;

fn main() -> Result<(), ltspice::LtspiceError> {
    // Load a simulation
    let sim = Simulation::from_file("circuit.raw")?;

    // Get basic info
    println!("Mode: {}", sim.mode());
    println!("Variables: {}", sim.stats().variables);
    println!("Points: {}", sim.stats().points);

    // Access time data (x-axis)
    if let Some(time) = sim.get_x(None) {
        println!("Time range: {} to {}",
            time.first().unwrap().real(),
            time.last().unwrap().real());
    }

    // Access a specific variable
    if let Some(data) = sim.get("V(out)", None) {
        println!("V(out) has {} points", data.len());
        println!("Max voltage: {:.3}V",
            data.iter().map(|v| v.real()).fold(f64::NEG_INFINITY, f64::max));
    }

    Ok(())
}

Working with AC Analysis

For AC simulations, data is stored as complex numbers:

use ltspice::Simulation;

let sim = Simulation::from_file("ac_analysis.raw")?;

if sim.is_complex() {
    if let Some(freq) = sim.get_x(None) {
        if let Some(vout) = sim.get("V(out)", None) {
            for (f, v) in freq.iter().zip(vout.iter()) {
                println!("f={:.1}Hz: {:.2}dB, {:.1}°",
                    f.real(), v.to_db(), v.phase_degrees());
            }
        }
    }
}

Stepped Simulations

For parametric sweeps, access data by step index:

use ltspice::Simulation;

let sim = Simulation::from_file("stepped.raw")?;

println!("Number of steps: {}", sim.step_count());

for step in 0..sim.step_count() {
    if let Some(data) = sim.get("V(out)", Some(step)) {
        let max = data.iter().map(|v| v.real()).fold(f64::NEG_INFINITY, f64::max);
        println!("Step {}: max V(out) = {:.3}V", step, max);
    }
}

Working with Variables

use ltspice::Simulation;

let sim = Simulation::from_file("circuit.raw")?;

// List all variables
for var in sim.variables() {
    println!("{}: {} ({})", var.index(), var.name(), var.class());
}

// Filter by type
let voltages: Vec<_> = sim.variables()
    .iter()
    .filter(|v| v.is_voltage())
    .collect();

println!("Found {} voltage signals", voltages.len());

Error Handling

The library uses a custom error type with descriptive messages:

use ltspice::{Simulation, LtspiceError};

match Simulation::from_file("circuit.raw") {
    Ok(sim) => println!("Loaded {} points", sim.stats().points),
    Err(LtspiceError::FileNotFound(path)) => {
        eprintln!("File not found: {:?}", path);
    }
    Err(LtspiceError::UnsupportedEncoding) => {
        eprintln!("Could not decode file encoding");
    }
    Err(e) => eprintln!("Error: {}", e),
}

Supported File Formats

Feature Status
Binary format Supported
ASCII format Planned
UTF-8 encoding Supported
UTF-16 LE encoding Supported
Single precision (float32) Supported
Double precision (float64) Supported
Complex values Supported
Stepped simulations Supported

License

MIT

About

A Rust Library to parse LTSpice files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages