Skip to content

Gyro spectrum analysis fails on logs without gyroUnfilt data #126

@nerdCopter

Description

@nerdCopter

Problem

Many spectrum analysis graphs show "Data Unavailable" for older Betaflight logs (4.0.x, 4.3.x) that contain valid flight data.

Root Cause

The filter delay analysis in src/data_analysis/filter_delay.rs requires BOTH gyroADC (filtered) AND gyroUnfilt (unfiltered) data:

if let (Some(filtered), Some(unfiltered)) = (row.gyro[axis], row.gyro_unfilt[axis]) {
    filtered_data.push(filtered as f32);
    unfiltered_data.push(unfiltered as f32);
}

Note: The renderer already has fallback logic to use debug[0-2] as unfiltered gyro when gyroUnfilt[0-2] is not available (implemented in src/data_input/log_parser.rs). This works for many logs where pilots configured debug mode.

Problem occurs when BOTH sources are missing:

  • gyroUnfilt[0-2] - not available in older firmware ✗
  • debug[0-2] - pilot did not configure debug mode for this particular flight ✗

Historical Context

Older Betaflight (< 4.5): Unfiltered gyro logging depended on configuration:

  • Default: Only gyroADC[0-2] (filtered) was logged
  • Debug mode configured: Unfiltered gyro logged to debug[0-2] channels
    • Common debug modes: GYRO_SCALED, GYRO, GYRO_RAW
    • Many pilots configured this when tuning PIDs or filters
    • Some pilots left it off for normal flights (battery/performance/storage reasons)
  • Renderer already handles debug fallback - this works for many logs

Modern Betaflight (≥ 4.5): Unfiltered gyro is logged automatically to gyroUnfilt[0-2] fields by default, so this is not an issue for recent logs.

When Does The Problem Occur?

  1. Tuning flights: Usually have debug mode → Works fine (fallback to debug)
  2. Normal flights with debug disabled: No unfiltered data → Fails (this issue)
  3. Storage-constrained flights: Debug disabled to save space → Fails (this issue)

Evidence

Log: 4.0.2_BTFL_AIKON_BLACKBOX_LOG_20190817_222106.06.csv

  • CSV fields: Only has gyroADC[0-2]
  • Frames: 128,289 (legitimate flight)
  • Debug mode: Not configured for this flight
  • Renderer fallback: No debug[0-2] to fall back to
  • Renderer diagnostic:
    Roll axis: gyro=282354, unfilt=0, both=0, longest_continuous=0
    Axis 0 (Roll) extracted: 0 samples
    
  • Result: "Data Unavailable" for gyro spectrum plots

Current Fallback Logic

The renderer already implements debug channel fallback in src/data_input/log_parser.rs:

// Apply Fallback Logic for gyro_unfilt (debug[0-2] --> gyroUnfilt[0-2])
for axis in 0..crate::axis_names::AXIS_NAMES.len() {
    current_row_data.gyro_unfilt[axis] = match parsed_gyro_unfilt[axis] {
        Some(val) => Some(val),
        None => parsed_debug[axis],  // ← Fallback to debug
    };
}

This works well when debug mode was configured. The problem is spectrum analysis still requires unfiltered data even when neither source is available.

Impact

When both gyroUnfilt and debug[0-2] are missing:

  • Gyro spectrum analysis graphs fail → "Data Unavailable"
  • Gyro PSD (Power Spectral Density) plots fail
  • Gyro vs Unfiltered comparison plots fail
  • Filter delay analysis skipped

These are valid flight logs with usable gyroADC data, but certain analysis types cannot be performed.

Proposed Solutions

Option 1: Fallback to Filtered-Only Analysis (Recommended)

When both gyroUnfilt and debug[0-2] are unavailable, perform spectrum analysis on gyroADC alone:

  • Still useful for identifying resonance frequencies
  • Can show motor noise patterns
  • Can identify problematic frequency bands
  • Just can't show filtering effectiveness or calculate filter delay
  • Label plots clearly: "Filtered Gyro Only - Unfiltered data not available"

Option 2: User-Friendly Status Messages

Improve console output to distinguish between cases:

  • ✅ "Using debug channels as unfiltered gyro" (fallback working)
  • ✅ "Unfiltered gyro available from gyroUnfilt fields" (modern logs)
  • ⚠️ "Note: Neither gyroUnfilt nor debug channels available. Spectrum analysis shows filtered data only." (this issue)

Option 3: Configuration Flag

Add command-line flag: --allow-filtered-only-spectrums

  • Default: OFF (current behavior - skip plots when unfiltered missing)
  • When enabled: Generate filtered-only spectrum plots with clear labeling
  • Gives users control over completeness vs. information trade-off

Recommendation

Implement Option 1 (filtered-only analysis with clear labeling) + Option 2 (better status messages):

  1. Keep existing debug fallback (already works well)
  2. When both sources unavailable: Generate spectrum plots from filtered gyro only
  3. Add clear labels:
    • On plots: "[Filtered Gyro Only]" or "[No Unfiltered Reference]"
    • In legend: "(gyroADC - no unfiltered data available)"
  4. Improve console messages: Clearly state which data source is being used
  5. Skip comparison plots gracefully: Can't compare filtered vs unfiltered without unfiltered data

This approach:

  • ✅ Respects existing debug fallback (used by many tuning logs)
  • ✅ Maximizes value for logs without debug mode
  • ✅ Provides transparency about data limitations
  • ✅ Maintains current behavior for logs with debug configured

Files Affected

  • src/data_analysis/filter_delay.rs - Add filtered-only fallback mode
  • src/plot_functions/plot_gyro_spectrums.rs - Support filtered-only spectrum generation
  • src/plot_functions/plot_psd.rs - Support filtered-only PSD analysis
  • src/plot_functions/plot_gyro_vs_unfilt.rs - Skip gracefully with informative message
  • src/data_input/log_parser.rs - Add status reporting for data source detection

Test Cases

  1. Modern log (≥ 4.5): gyroUnfilt[0-2] present → Works (use gyroUnfilt)
  2. Tuning log with debug: debug[0-2] present → Works (existing fallback)
  3. Normal flight without debug: Only gyroADC[0-2]Needs fix (filtered-only mode)
    • Example: 4.0.2_BTFL_AIKON_BLACKBOX_LOG_20190817_222106.06.csv (282K frames)
  4. Mixed log file: Some logs with debug, some without → Should handle each appropriately

Summary

The renderer already handles the common case (debug mode configured) via debug channel fallback. This issue addresses the remaining case where pilots chose not to use debug mode for normal flights, leaving valuable filtered gyro data underutilized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions