Skip to content

Commit

Permalink
Restrict FFT_Magnitude and FFT_MajorPeak detection to the set freq range
Browse files Browse the repository at this point in the history
  • Loading branch information
Victoare committed Mar 19, 2024
1 parent 84b322f commit e2c10d0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
15 changes: 1 addition & 14 deletions source/WledSRServer/Audio/AudioProcessor/FFT/FFTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ internal class FFTData : Context
public double[] Values { get; set; } = Array.Empty<double>();
public double[] Frequencies { get; set; } = Array.Empty<double>();

public double PeakValue { get; set; }
public double PeakFrequency { get; set; }

public int[] GetIndexesByFreq(double freqLow, double freqHigh)
=> Frequencies.Select((freq, idx) => new { freq, idx })
.Where(f => f.freq >= freqLow && f.freq <= freqHigh)
Expand Down Expand Up @@ -50,20 +47,10 @@ public override bool Process()
var complexData = FftSharp.FFT.Forward(values);
var positiveOnly = true; // using only half of the spectrum

_fft.Values = FftSharp.FFT.Magnitude(complexData, positiveOnly); // WLED based on Magnitude (Scaling appliend in bucketizer)
_fft.Values = FftSharp.FFT.Magnitude(complexData, positiveOnly); // WLED based on Magnitude (Scaling appliend in bucketizer)
// _fft.Values = FftSharp.FFT.Power(complexData, positiveOnly); // value[i] = 20 * Math.Log10(value[i])

_fft.Frequencies = FftSharp.FFT.FrequencyScale(_fft.Values.Length, _sampleRate, positiveOnly);

for (int i = 0; i < _fft.Values.Length; i++)
{
if (_fft.Values[i] > _fft.PeakValue)
{
_fft.PeakValue = _fft.Values[i];
_fft.PeakFrequency = _fft.Frequencies[i];
}
}

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public override bool Process()
{
// Debug.WriteLine($"FreqBands: {string.Join(";", _fft.Frequencies.Select(f => f.ToString()))}");

var freqIndexes = _fft.GetIndexesByFreq(_freqPoints[0], _freqPoints[_freqPoints.Length - 1]);
var peakIndex = freqIndexes.MaxBy(idx => _fft.Values[idx]);
_buckets.PeakValue = _fft.Values[peakIndex];
_buckets.PeakFrequency = _fft.Frequencies[peakIndex];

for (var bucket = 0; bucket < _bucketCount; bucket++)
{
var values = _fft.GetValuesByFreq(_freqPoints[bucket], _freqPoints[bucket + 1]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace WledSRServer.Audio.AudioProcessor.FFTBuckets
using FftSharp;

namespace WledSRServer.Audio.AudioProcessor.FFTBuckets
{
internal class FFTBucketData : Context
{
Expand All @@ -12,5 +14,8 @@ public struct Bucket
}

public Bucket[] Values { get; set; } = Array.Empty<Bucket>();

public double PeakValue { get; set; }
public double PeakFrequency { get; set; }
}
}
4 changes: 2 additions & 2 deletions source/WledSRServer/Audio/AudioProcessor/Packet/SetPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public override bool Process()
_packet.SampleSmth = raw;
_packet.SamplePeak = (byte)(_beat.Detected ? 1 : 0);

_packet.FFT_Magnitude = (float)_fft.PeakValue;
_packet.FFT_MajorPeak = (float)_fft.PeakFrequency;
_packet.FFT_Magnitude = (float)_buckets.PeakValue;
_packet.FFT_MajorPeak = (float)_buckets.PeakFrequency;

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion source/WledSRServer/UserControls/BeatDetectorGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected override void OnPaint(PaintEventArgs e)

var indexes = fft.GetIndexesByFreq(dispFreqMin, dispFreqMax);

var scaleFFTValue = new Func<double, float>(v => this.Height - (float)(v / fft.PeakValue * this.Height));
var fftMaxValue = fft.Values.Max();
var scaleFFTValue = new Func<double, float>(v => this.Height - (float)(v / fftMaxValue * this.Height));

//e.Graphics.Clear(Color.FromKnownColor(KnownColor.Control));
if (beat.Detected)
Expand Down

0 comments on commit e2c10d0

Please sign in to comment.