Skip to content

Commit

Permalink
🐛 (micro): Correct rms_buffer access and fit logs to the use
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jan 19, 2022
1 parent 14b9d50 commit 0ba7eb3
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions spikes/lk_sensors_microphone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <cmath>
#include <deque>

#include "PinNames.h"

#include "drivers/BufferedSerial.h"
#include "drivers/I2C.h"
#include "platform/mbed_wait_api.h"
#include "rtos/ThisThread.h"

#include "HelloWorld.h"
Expand Down Expand Up @@ -48,20 +46,17 @@ class SMA
};

// RMS - Root Mean Square
auto RMS(std::array<int, 10> &data, int newvalue) -> int
auto RMS(std::deque<int> &data, int newvalue) -> int
{
int square = 0;
float mean = 0;
float root = 0;

for (auto i: data) {
square += data.at(i) * data.at(i);
data.at(i) = data.at(i + 1);
data.push_back(newvalue);
for (auto value: data) {
square += value * value;
}

data.at(data.size() - 1) = newvalue;

square += newvalue * newvalue;
data.pop_front();

mean = static_cast<float>(square) / static_cast<float>(data.size());

Expand All @@ -83,7 +78,7 @@ auto main() -> int

// RMS vs SMA value (un/comment to test)
// static auto sma_filter = SMA<5> {};
auto rms_buffer = std::array<int, 10> {};
auto rms_buffer = std::deque<int> {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

LKCoreMicrophone microphone(MCU_MIC_INPUT);

Expand All @@ -95,12 +90,13 @@ auto main() -> int
}

// RMS vs SMA value (un/comment to test)
// auto output = sma_filter(rawValue)
// auto output = sma_filter(rawValue);
auto output = RMS(rms_buffer, rawValue);

// TODO (@john_doe): print floats
log_info("%d", output);

wait_us(250);
if (rawValue > 650) {
log_info("Peak: %d | Mean: %d", rawValue, output);
rtos::ThisThread::sleep_for(6ms);
}
}
}

0 comments on commit 0ba7eb3

Please sign in to comment.