You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino音频分析:如何获取特定频率峰值的持续时长

Solution for Measuring Target Frequency Peak Duration

Hey there! Let's figure out how to track how long that 2500-2800Hz peak stays active in your Arduino audio analysis code. Your current setup already does a great job detecting the frequency range—we just need to tweak the timing logic to capture its duration reliably.

Step 1: Update State Tracking Variables

First, add these global variables to track the frequency state and timing:

// State tracking for peak duration
bool isTargetPeakActive = false;
unsigned long peakStartMillis = 0;
int noPeakCounter = 0;
const int NO_PEAK_THRESHOLD = 3; // Adjust based on how tolerant you want to be to noise
  • isTargetPeakActive: Flags whether we're currently detecting the target frequency range
  • peakStartMillis: Stores the timestamp when the peak first appears
  • noPeakCounter: Helps filter out temporary noise (avoids ending the timer for a single missed peak)

Step 2: Modify Peak Detection Logic in loop()

Replace your existing peak checking code with this updated logic:

double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);

// Check if we're in the target frequency range
if (peak > 2500 && peak < 2800) {
  noPeakCounter = 0; // Reset noise counter
  if (!isTargetPeakActive) {
    // Start timing when the peak first appears
    isTargetPeakActive = true;
    peakStartMillis = millis();
    Serial.print("Target peak started at: ");
    Serial.println(peakStartMillis);
    lightTwo(); // Optional: Visual feedback when peak starts
  }
} else {
  if (isTargetPeakActive) {
    noPeakCounter++;
    // Only end the timer if we've missed the peak multiple times (filter noise)
    if (noPeakCounter >= NO_PEAK_THRESHOLD) {
      unsigned long peakDuration = millis() - peakStartMillis;
      Serial.print("Target peak duration: ");
      Serial.print(peakDuration);
      Serial.println(" ms");
      // Reset state variables
      isTargetPeakActive = false;
      peakStartMillis = 0;
      noPeakCounter = 0;
      lightOne(); // Optional: Visual feedback when peak ends
    }
  }
}

This logic:

  • Starts timing the moment the target peak is first detected
  • Ignores temporary dips in the peak (using noPeakCounter) to avoid false duration readings
  • Calculates and prints the duration once the peak is consistently gone

Step 3: Optional Tweaks for Better Accuracy

  • Adjust NO_PEAK_THRESHOLD: If you're getting false duration triggers, increase this number. If you need faster response to peak ends, decrease it.
  • Modify SAMPLES or SAMPLING_FREQUENCY: Higher sample counts give better frequency resolution but longer processing time. Balance this based on how quickly you need to detect peak starts/ends.

Useful References to Deepen Your Understanding

  • ArduinoFFT Library Docs: Learn more about the MajorPeak function, windowing techniques, and FFT parameters—this will help you refine peak detection accuracy.
  • PDM Audio Input Guide: Explore how sample buffer size and sampling rate affect the continuity of your audio capture, ensuring you don't miss segments of the peak.
  • Short-Time Fourier Transform (STFT) Basics: This concept extends standard FFT to track frequency changes over time, which is fundamental to measuring duration of specific frequency events.

内容的提问来源于stack exchange,提问作者Cholas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:42:36