Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions src/modules/MajorPeaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,22 @@ MajorPeaks::~MajorPeaks()
delete[] output[MP_FREQ]; // free the array of frequencies
delete[] output[MP_AMP]; // free the array of amplitudes
delete[] output; // free the array managing the arrays of frequencies and amplitudes

// free temporary storage
delete[] outputFrequencies;
delete[] outputAmplitudes;
}

void MajorPeaks::resetPeaksArrays()
{
numPeaks = 0; // reset the number of peaks to zero

if (outputLength != windowSizeBy2 >> 1) {
delete[] outputFrequencies;
delete[] outputAmplitudes;
outputFrequencies = new float[windowSizeBy2 >> 1];
outputAmplitudes = new float[windowSizeBy2 >> 1];
}
outputLength = min(
MAX_PEAK_STORAGE,
windowSizeBy2 >> 1
);

// zero out the output arrays
for (int i = 0; i < windowSizeBy2 >> 1; i++) {
outputFrequencies[i] = 0;
outputAmplitudes[i] = 0;
for (int i = 0; i < outputLength; i++)
{
outputFrequencies[i] = 0.0f;
outputAmplitudes[i] = 0.0f;
}
}

Expand All @@ -72,16 +67,19 @@ void MajorPeaks::findPeaks()
// if the current bin is a peak, store its frequency and amplitude
if (windowData[i] > windowData[i - 1]
&& windowData[i] > windowData[i + 1]) {
// store the frequency of the peak
// the index is multiplied by freqRes to convert the bin number to a frequency value
// freqRes is the frequency width of each bin
outputFrequencies[numPeaks] = i * freqRes;

// store the amplitude of the peak
outputAmplitudes[numPeaks] = windowData[i];

// increment the number of peaks found to reflect the addition of this peak
numPeaks++;
if (numPeaks < outputLength)
{
// store the frequency of the peak
// the index is multiplied by freqRes to convert the bin number to a frequency value
// freqRes is the frequency width of each bin
outputFrequencies[numPeaks] = i * freqRes;

// store the amplitude of the peak
outputAmplitudes[numPeaks] = windowData[i];

// increment the number of peaks found to reflect the addition of this peak
numPeaks++;
}

if (debugMode & DEBUG_VERBOSE) {
Serial.printf(" - [%d, %03g]\n", i, windowData[i]);
Expand Down
8 changes: 4 additions & 4 deletions src/modules/MajorPeaks.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class MajorPeaks : public ModuleInterface<float**> {
int maxNumPeaks = 4; // default number of peaks to find
int numPeaks = 0; // number of peaks found this iteration

// temporary storage for calculating the output
int outputLength = windowSizeBy2 >> 1;
float* outputFrequencies = new float[windowSizeBy2 >> 1];
float* outputAmplitudes = new float[windowSizeBy2 >> 1];
static constexpr int MAX_PEAK_STORAGE = 1024;
int outputLength = MAX_PEAK_STORAGE;
float outputFrequencies[MAX_PEAK_STORAGE] = {0};
float outputAmplitudes[MAX_PEAK_STORAGE] = {0};

// reset peaks arrays
// this function is called at the beginning of each call to perform analysis
Expand Down