Skip to content

Commit

Permalink
Saving and loading noise cal spectrum from flash
Browse files Browse the repository at this point in the history
  • Loading branch information
connornishijima committed Feb 6, 2024
1 parent 4f64eec commit 113cc49
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
1 change: 0 additions & 1 deletion data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@

<div class="page_title">CALIBRATION</div>
<div class="page_paragraph">
<!-- TODO: Starting noise cal should lock the UI until Emotiscope reports back as finished -->
<button onclick="start_noise_calibration();">REMOVE BACKGROUND NOISE</button>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/EMOTISCOPE_FIRMWARE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// Internal dependencies
#include "secrets.h" // ........... You don't get to have this, rename "YOUR_secrets.h" to use it
#include "global_defines.h" // .... Compile-time configuration
#include "types.h" // ............. typedefs for things like CRGBFs
#include "profiler.h" // .......... Developer tools, measures the execution of functions
#include "sliders.h" // ........... Handles sliders that appear in the web app
Expand Down
83 changes: 75 additions & 8 deletions src/configuration.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#define CONFIG_FILENAME "/configuration.bin"
#define NOISE_SPECTRUM_FILENAME "/noise_spectrum.bin"
#define MIN_SAVE_WAIT_MS (3 * 1000) // Values must stabilize for this many seconds to be written to FS

extern lightshow_mode lightshow_modes[];
extern PsychicWebSocketHandler websocket_handler;

config configuration = {
1.00, // .... brightness
8.00, // .... speed
0.00, // .... hue
0, // ....... current_mode
true, // .... mirror_mode
0.6, // ..... incandescent_filter
0.0, // ..... hue_range
1.00, // ... brightness
8.00, // ... speed
0.00, // ... hue
0, // ... current_mode
true, // ... mirror_mode
0.6, // ... incandescent_filter
0.0, // ... hue_range
};

float noise_spectrum[NUM_FREQS] = {0.0};

uint32_t last_save_request_ms = 0;
bool save_request_open = false;

Expand Down Expand Up @@ -62,7 +65,6 @@ void sync_configuration_to_client() {

// Save configuration to LittleFS
bool save_config() {
// TODO: Add noise cal (float[64]) to configuration struct
File file = LittleFS.open(CONFIG_FILENAME, FILE_WRITE);
if (!file) {
printf("Failed to open %s for writing!", CONFIG_FILENAME);
Expand All @@ -77,7 +79,25 @@ bool save_config() {
}
}
file.close();
return true;
}

// Save noise spectrum to LittleFS
bool save_noise_spectrum() {
File file = LittleFS.open(NOISE_SPECTRUM_FILENAME, FILE_WRITE);
if (!file) {
printf("Failed to open %s for writing!", NOISE_SPECTRUM_FILENAME);
return false;
}
else {
const uint8_t* ptr = (const uint8_t*)&noise_spectrum;

// Iterate over the size of noise_spectrum and write each byte to the file
for (size_t i = 0; i < sizeof(float) * NUM_FREQS; i++) {
file.write(ptr[i]);
}
}
file.close();
return true;
}

Expand Down Expand Up @@ -110,7 +130,39 @@ bool load_config() {
}
}
file.close(); // Close the file after reading
return true;
}

// Load noise_spectrum from LittleFS
bool load_noise_spectrum() {
// Open the file for reading
File file = LittleFS.open(NOISE_SPECTRUM_FILENAME, FILE_READ);
if (!file) {
printf("Failed to open %s for reading!\n", NOISE_SPECTRUM_FILENAME);
return false;
}
else {
// Ensure the noise_spectrum array is sized properly
if (file.size() != sizeof(float) * NUM_FREQS) {
printf("Noise spectrum size does not match expected size! (%lu != %lu)\n", file.size(), sizeof(float) * NUM_FREQS);
// TODO: When files don't match their expected size on load, just delete them and start fresh
file.close();
return false;
}

uint8_t* ptr = (uint8_t*)&noise_spectrum; // Pointer to the noise_spectrum array

// Read the file content into the noise_spectrum array
for (size_t i = 0; i < sizeof(float) * NUM_FREQS; i++) {
int byte = file.read(); // Read a byte
if (byte == -1) { // Check for read error or end of file
printf("Error reading %s!\n", NOISE_SPECTRUM_FILENAME);
break;
}
ptr[i] = (uint8_t)byte; // Store the byte in the noise_spectrum array
}
}
file.close(); // Close the file after reading
return true;
}

Expand All @@ -125,6 +177,7 @@ void sync_configuration_to_file_system(uint32_t t_now_ms) {
if ((t_now_ms - last_save_request_ms) >= MIN_SAVE_WAIT_MS) {
printf("SAVING CONFIGURATION TO FILESYSTEM\n");
save_config();
save_noise_spectrum();
save_request_open = false;
}
}
Expand All @@ -139,6 +192,20 @@ void init_configuration() {
if (load_success == false) {
printf("FAIL\n");
save_config();
;
}
else {
printf("PASS\n");
}

// Attempt to load noise_spectrum from flash
printf("LOADING NOISE SPECTRUM...");
load_success = load_noise_spectrum();

// If we couldn't load the file, save a fresh copy
if (load_success == false) {
printf("FAIL\n");
save_noise_spectrum();
}
else {
printf("PASS\n");
Expand Down
1 change: 1 addition & 0 deletions src/global_defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define NUM_FREQS 64
9 changes: 3 additions & 6 deletions src/goertzel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// Functions related to the computation and post-processing of the Goertzel Algorithm to compute DFT
// https://en.wikipedia.org/wiki/Goertzel_algorithm

#define NUM_FREQS 64

#define TWOPI 6.28318530
#define FOURPI 12.56637061
#define SIXPI 18.84955593
Expand All @@ -23,7 +21,6 @@
#define NOTE_STEP 2 // Use half-steps anyways

uint32_t noise_calibration_active_frames_remaining = 0;
float noise_spectrum[NUM_FREQS];

const float notes[] = {
55.0, 56.635235, 58.27047, 60.00294, 61.73541, 63.5709, 65.40639, 67.351025, 69.29566, 71.355925, 73.41619, 75.59897, 77.78175, 80.09432, 82.40689, 84.856975, 87.30706, 89.902835, 92.49861, 95.248735, 97.99886, 100.91253, 103.8262, 106.9131, 110.0, 113.27045, 116.5409, 120.00585, 123.4708, 127.1418, 130.8128, 134.70205, 138.5913, 142.71185, 146.8324, 151.19795, 155.5635, 160.18865, 164.8138, 169.71395, 174.6141, 179.80565, 184.9972, 190.49745, 195.9977, 201.825, 207.6523, 213.82615, 220.0, 226.54095, 233.0819, 240.0118, 246.9417, 254.28365, 261.6256, 269.4041, 277.1826, 285.4237, 293.6648, 302.3959, 311.127, 320.3773, 329.6276, 339.4279, 349.2282, 359.6113, 369.9944, 380.9949, 391.9954, 403.65005, 415.3047, 427.65235, 440.0, 453.0819, 466.1638, 480.02355, 493.8833, 508.5672, 523.2511, 538.8082, 554.3653, 570.8474, 587.3295, 604.79175, 622.254, 640.75455, 659.2551, 678.8558, 698.4565, 719.22265, 739.9888, 761.98985, 783.9909, 807.30015, 830.6094, 855.3047, 880.0, 906.16375, 932.3275, 960.04705, 987.7666, 1017.1343, 1046.502, 1077.6165, 1108.731, 1141.695, 1174.659, 1209.5835, 1244.508, 1281.509, 1318.51, 1357.7115, 1396.913, 1438.4455, 1479.978, 1523.98, 1567.982, 1614.6005, 1661.219, 1710.6095, 1760.0, 1812.3275, 1864.655, 1920.094, 1975.533, 2034.269, 2093.005, 2155.233, 2217.461, 2283.3895, 2349.318, 2419.167, 2489.016, 2563.018, 2637.02, 2715.4225, 2793.825, 2876.8905, 2959.956, 3047.96, 3135.964, 3229.2005, 3322.437, 3421.2185, 3520.0, 3624.655, 3729.31, 3840.1875, 3951.065, 4068.537, 4186.009, 4310.4655, 4434.922, 4566.779, 4698.636, 4838.334, 4978.032, 5126.0365, 5274.041, 5430.8465, 5587.652, 5753.7815, 5919.911, 6095.919, 6271.927, 6458.401, 6644.875, 6842.4375, 7040.0, 7249.31, 7458.62, 7680.375, 7902.13, 8137.074, 8372.018, 8620.931, 8869.844, 9133.558, 9397.272, 9676.668, 9956.064, 10252.072, 10548.08, 10861.69, 11175.3, 11507.56, 11839.82, 12191.835, 12543.85, 12916.8, 13289.75, 13684.875, 14080.0, 14498.62, 14917.24, 15360.75, 15804.26, 16274.145, 16744.03, 17241.855, 17739.68, 18267.11, 18794.54, 19353.36, 19912.18, 20504.17, 21096.16, 21723.38, 22350.6, 23015.12, 23679.64, 24383.67, 25087.7, 25833.6, 26579.5, 27369.75, 28160.0, 28997.24, 29834.48, 30721.5, 31608.52, 32548.295, 33488.07, 34483.72, 35479.37, 36534.225, 37589.08, 38706.665, 39824.25, 41008.285, 42192.32, 43446.76, 44701.2, 46030.24, 47359.28, 48767.34, 50175.4, 51667.2};
Expand Down Expand Up @@ -198,8 +195,8 @@ float collect_and_filter_noise(float input_magnitude, uint16_t bin) {
return output_magnitude;
}
else {
if (input_magnitude > noise_spectrum[bin]) {
noise_spectrum[bin] = input_magnitude;
if (input_magnitude*1.25 > noise_spectrum[bin]) {
noise_spectrum[bin] = input_magnitude*1.25;
}

return input_magnitude;
Expand Down Expand Up @@ -263,7 +260,7 @@ void calculate_magnitudes() {
if(noise_calibration_active_frames_remaining == 0){
// Let the UI know
broadcast("noise_cal_ready");
//save_config_delayed();
save_config_delayed();
}
}

Expand Down

0 comments on commit 113cc49

Please sign in to comment.