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 rangepeakStartMillis: Stores the timestamp when the peak first appearsnoPeakCounter: 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
SAMPLESorSAMPLING_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
MajorPeakfunction, 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
相关产品推荐
相关产品推荐

