Skip to content

Commit

Permalink
Threshold detection on microphone
Browse files Browse the repository at this point in the history
  • Loading branch information
coelacant1 committed Jun 1, 2022
1 parent 535e4cf commit ae35d8b
Show file tree
Hide file tree
Showing 24 changed files with 8,689 additions and 8,635 deletions.
2 changes: 1 addition & 1 deletion .pio/build/project.checksum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
33fbac06369f5bf11a69f69fbfd44d9348159aec
dccaf109dbb3390bdd324df2834753b42018e721
Binary file modified .pio/build/teensy40/.sconsign39.dblite
Binary file not shown.
Binary file modified .pio/build/teensy40/firmware.elf
Binary file not shown.
17,130 changes: 8,565 additions & 8,565 deletions .pio/build/teensy40/firmware.hex

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion .pio/build/teensy40/idedata.json

This file was deleted.

Binary file modified .pio/build/teensy40/lib06a/libAdafruit Unified Sensor.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib0b5/libAdafruit APDS9960 Library.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib150/libSmartMatrix.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib16c/libOctoWS2811.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib178/libSerialTransfer.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib4f9/libWire.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib770/libSPI.a
Binary file not shown.
Binary file modified .pio/build/teensy40/lib93a/libAdafruit BusIO.a
Binary file not shown.
Binary file modified .pio/build/teensy40/libFrameworkArduino.a
Binary file not shown.
Binary file modified .pio/build/teensy40/liba6c/libAdafruit BNO055.a
Binary file not shown.
Binary file modified .pio/build/teensy40/src/main.cpp.o
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Animation/ProtogenKitFaceAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ProtogenKitFaceAnimation : public Animation<2> {
sA.Update();
sA.SetHueAngle(ratio * 360.0f * 4.0f);

float mouthMove = sA.GetCurrentValue();
float mouthMove = MicrophoneFourier::GetCurrentMagnitude();

if (isBooped){
Surprised();
Expand Down
33 changes: 33 additions & 0 deletions src/Filter/DerivativeFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <Arduino.h>
#include "KalmanFilter.h"
#include "MinFilter.h"
#include "..\Math\Mathematics.h"

class DerivativeFilter{
private:
KalmanFilter<10> output = KalmanFilter<10>(0.2f);
MinFilter<40> minFilter;
float previousReading = 0.0f;
float outputValue = 0.0f;

public:
DerivativeFilter(){}

float GetOutput(){
return outputValue;
}

float Filter(float value){
float amplitude = fabs(value - previousReading);
float normalized = output.Filter(amplitude);
float minimum = minFilter.Filter(normalized);

previousReading = value;
outputValue = Mathematics::Constrain(normalized - minimum, 0.0f, 1.0f);

return outputValue;
}

};
9 changes: 4 additions & 5 deletions src/Filter/FFTFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

class FFTFilter{
private:
KalmanFilter<10> mv = KalmanFilter<10>(0.1f);
MinFilter<40> minF = MinFilter<40>();
KalmanFilter<10> output = KalmanFilter<10>(0.1f);
KalmanFilter<5> output = KalmanFilter<5>(0.5f);
float previousReading = 0.0f;
float outputValue = 0.0f;

Expand All @@ -20,17 +19,17 @@ class FFTFilter{
return outputValue;
}

float Update(float value){
float Filter(float value){
float changeRate = value - previousReading;
float amplitude = fabs(changeRate);//mv.Filter(fabs(changeRate));
float minimum = minF.Filter(value);
float normalized = value - amplitude - minimum;
float truncate = normalized < 0 ? 0 : output.Filter(normalized);//output.Filter(normalized);
float truncate = normalized < 0 ? 0 : normalized;//output.Filter(normalized);

previousReading = value;
outputValue = Mathematics::Constrain(truncate, 0.0f, 1.0f);

return outputValue;
}

};
};
15 changes: 5 additions & 10 deletions src/Filter/KalmanFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ class KalmanFilter {
float values[memory];
uint8_t currentAmount = 0;

void ShiftArray(float* arr){
for(uint8_t i = 0; i < memory; i++){
arr[i] = arr[i + 1];
}

arr[memory - 1] = 0.0f;
}

public:
KalmanFilter() {
this->gain = 0.25f;
Expand All @@ -37,11 +29,14 @@ class KalmanFilter {
values[currentAmount++] = value;
}
else{
ShiftArray(values);//pop first
for(uint8_t i = 0; i < memory - 1; i++){
values[i] = values[i + 1];
}

values[memory - 1] = value;
}

for(int i = 0; i < currentAmount; i++){
for(uint8_t i = 0; i < currentAmount; i++){
sum += values[i];
}

Expand Down
43 changes: 19 additions & 24 deletions src/Filter/PeakDetection.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
#include <Arduino.h>
#include "..\Math\Mathematics.h"

template <size_t sampleSize>
class PeakDetection{
private:
const uint8_t sampleSize;
uint8_t lag;

float threshold;
float influence;
float* filData;
float* avg;
float* std;
float filData[sampleSize];
float avg[sampleSize];
float std[sampleSize];

void GetStdDev(uint8_t start, uint8_t length, float* data, float& avgRet, float& stdRet){
float average = 0.0f;
Expand All @@ -21,51 +21,43 @@ class PeakDetection{
for (uint8_t i = 0; i < length; i++){
average += data[start + i];
}


average /= length;
average /= (float)length;

for (uint8_t i = 0; i < length; i++){
stdDev += powf(data[start + i] - average, 2.0f);
}

avgRet = average;
stdRet = sqrtf(stdDev / length);
stdRet = sqrtf(stdDev / (float)length);
}

public:
PeakDetection(uint8_t sampleSize, uint8_t lag = 7, float threshold = 1.2f, float influence = 0.25f) : sampleSize(sampleSize) {
filData = new float[sampleSize];
avg = new float[sampleSize];
std = new float[sampleSize];

PeakDetection(uint8_t lag = 12, float threshold = 0.75f, float influence = 0.5f) {
this->lag = lag;
this->threshold = threshold;
this->influence = influence;

for(uint8_t i = 0; i < sampleSize; i++){
filData[i] = 0.0f;
avg[i] = 0.0f;
std[i] = 0.0f;
}
}

~PeakDetection(){
delete avg;
delete std;
}

void Calculate(float* data, float* peaks){
float average = 0.0f;
float stdDev = 0.0f;

for (uint8_t i = 0; i < sampleSize; i++){
filData[i] = 0.0f;
avg[i] = 0.0f;
std[i] = 0.0f;
}

GetStdDev(0, lag, data, avg[lag - 1], std[lag - 1]);
GetStdDev(0, lag, data, average, stdDev);

avg[lag - 1] = average;
std[lag - 1] = stdDev;

for(uint8_t i = lag; i < sampleSize - lag; i++){
if(fabs(data[i] - avg[i - 1]) > threshold * std[i - 1]){
if(data[i] > avg[i - 1] && data[i] > 0.05f) peaks[i] = 1.0f;
if(data[i] > avg[i - 1]) peaks[i] = 1.0f;
//else peaks[i] = -1.0f;

filData[i] = influence * data[i] + (1.0f - influence) * filData[i - 1];
Expand All @@ -76,6 +68,9 @@ class PeakDetection{
}

GetStdDev(i - lag + 1, i, data, avg[i], std[i]);

avg[i] = average;
std[i] = stdDev;
}
}
};
4 changes: 0 additions & 4 deletions src/Materials/Animated/SpectrumAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class SpectrumAnalyzer : public Material {
this->material = material;
}

float GetCurrentValue(){
return MicrophoneFourier::GetCurrentValue();
}

float* GetFourierData(){
if(bounce){
return bounceData;
Expand Down
Loading

1 comment on commit ae35d8b

@GR3Y-SCALE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man i need to catch up on how this all works lol. I am kinda lost since there is so many changes i cant keep up with how it overall functions your program.

Please sign in to comment.