Skip to content

Commit 58aafc0

Browse files
committed
spectrogram peak detection
1 parent 8fa9232 commit 58aafc0

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/Spectrogram/Spectrogram.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,33 @@ public List<double[]> GetFFTs()
232232
{
233233
return ffts;
234234
}
235+
236+
public (double freqHz, double magRms) GetPeak(bool latestFft = true)
237+
{
238+
if (ffts.Count == 0)
239+
return (double.NaN, double.NaN);
240+
241+
if (latestFft == false)
242+
throw new NotImplementedException("peak of mean of all FFTs not yet supported");
243+
244+
double[] freqs = ffts[ffts.Count - 1];
245+
246+
int peakIndex = 0;
247+
double peakMagnitude = 0;
248+
for (int i = 0; i < freqs.Length; i++)
249+
{
250+
if (freqs[i] > peakMagnitude)
251+
{
252+
peakMagnitude = freqs[i];
253+
peakIndex = i;
254+
}
255+
}
256+
257+
double maxFreq = SampleRate / 2;
258+
double peakFreqFrac = peakIndex / (double)freqs.Length;
259+
double peakFreqHz = maxFreq * peakFreqFrac;
260+
261+
return (peakFreqHz, peakMagnitude);
262+
}
235263
}
236264
}

0 commit comments

Comments
 (0)