From fbe92a3f67a88231cfbbfd9144cb5e8a717099c0 Mon Sep 17 00:00:00 2001 From: Geoff Roddick Date: Wed, 17 Apr 2024 07:57:41 -0400 Subject: [PATCH 1/2] Updated to also show the peak frequency --- .../AnalogMicSerialPlotterFFT.ino | 26 +++++++++------- .../HighRatePDMSerialPlotterFFT.ino | 30 ++++++++++++------ .../HighRatePDMSerialPlotterFFT_RAK18003.ino | 31 +++++++++++++------ .../PDMSerialPlotterFFT.ino | 22 +++++++------ 4 files changed, 69 insertions(+), 40 deletions(-) diff --git a/examples/RAK11200/AnalogMicSerialPlotterFFT/AnalogMicSerialPlotterFFT.ino b/examples/RAK11200/AnalogMicSerialPlotterFFT/AnalogMicSerialPlotterFFT.ino index b2fca0f..b5913e6 100644 --- a/examples/RAK11200/AnalogMicSerialPlotterFFT/AnalogMicSerialPlotterFFT.ino +++ b/examples/RAK11200/AnalogMicSerialPlotterFFT/AnalogMicSerialPlotterFFT.ino @@ -4,10 +4,12 @@ @brief This example reads PCM audio datas from the analog microphones by I2S, and prints out the FFT transfer samples to the Serial console. The Serial Plotter built into the Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter) - @note This example need use the RAK18040 analog microphone module. - @version 0.1 - @date 2022-06-6 - @copyright Copyright (c) 2022 + @note + 1) To use the ploter function Arduino IDE version 1.x is needed (version 2.x plotter does not work with this code) + 2) This example uses the RAK18040 analog microphone module. + @version 0.2 + @date 2024-03-25 + @copyright Copyright (c) 2024 */ #include @@ -64,6 +66,7 @@ void loop() { // Read data from microphone int sampleRead = I2S.read(sampleBuffer, sizeof(sampleBuffer)); + float Freq_with_Max_Amp; sampleRead = sampleRead >> 1; //each sample data with two byte // wait for samples to be read @@ -71,15 +74,14 @@ void loop() { // Fill the buffers with the samples memset(approxBuffer, 0, sizeof(approxBuffer)); - for (int i = 0; i < BUFFER_SIZE; i++) - { + for (int i = 0; i < BUFFER_SIZE; i++){ approxBuffer[i] = sampleBuffer[i]; - // Serial.println(sampleBuffer[i]); + // Serial.println(approxBuffer[i]); } if (first_flag > 20) //Discard the first 20 samples data { - Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); + Freq_with_Max_Amp = Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); // default return frequency with max aplitude // for (int j=0; j Serial Plotter) - @note This example need use the microphones that can supports higher sampling rate. - @version 0.1 - @date 2022-06-6 - @copyright Copyright (c) 2022 + @note + 1) To use the ploter function Arduino IDE version 1.x is needed (version 2.x plotter does not work with this code) + 2) This example needs a microphone that can support higher sampling rates. + 3) This example supports a sampling rate up to 120kHz, for support up to 156 kHz the PDM library needs to be modified as follows: + packages\rakwireless\hardware\esp32\2.0.3\libraries\PDM\src\PDM.cpp + change .use_apll from true to false. This changes the clock source for the I2S/PDM peripheral + Note: The acccuracy of the clock when using .use_appll true is not as accurate! + @version 0.2 + @date 2024-03-25 + @copyright Copyright (c) 2024 */ #include @@ -21,7 +27,8 @@ short sampleBuffer[BUFFER_SIZE]; // Audio sample buffers used for analysis and display int approxBuffer[BUFFER_SIZE]; // ApproxFFT sample buffer -const double samplingFrequency = 48000; // +const double samplingFrequency = 48000; //must match frequency above! +// Frequency range plotted is sampling frequency/4 int print_string[500] = {0}; @@ -52,8 +59,7 @@ void setup() { // initialize PDM with: // - one channel (mono mode) - // - a 200 kHz sample rate - // default PCM output frequency + // default PCM output frequency if (!PDM.begin(channels, frequency)) { Serial.println("Failed to start PDM!"); while (1) yield(); @@ -62,11 +68,13 @@ void setup() { pinMode(LED_BLUE, OUTPUT); digitalWrite(LED_BLUE, HIGH); } + static uint8_t first_flag = 0; void loop() { // Read data from microphone int sampleRead = PDM.read(sampleBuffer, sizeof(sampleBuffer)); + float Freq_with_Max_Amp; sampleRead = sampleRead >> 1; //each sample data with two byte // wait for samples to be read @@ -81,7 +89,7 @@ void loop() { if (first_flag > 10) //Discard the first 10 samples of data { - Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); + Freq_with_Max_Amp = Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); // default return frequency with max aplitude // for (int j=0; j Serial Plotter) - @note This example need use the microphones that can supports higher sampling rate and need use the RAK18003 module. - @version 0.1 - @date 2022-06-6 - @copyright Copyright (c) 2022 + @note + 1) To use the ploter function Arduino IDE version 1.x is needed (version 2.x plotter does not work with this code) + 2) This example needs a microphone that can support higher sampling rates. + 3) This example uses the RAK18003 module. + 4) This example supports a sampling rate up to 120kHz, for support up to 156 kHz the PDM library needs to be modified as follows: + packages\rakwireless\hardware\esp32\2.0.3\libraries\PDM\src\PDM.cpp + change .use_apll from true to false. This changes the clock source for the I2S/PDM peripheral + Note: The acccuracy of the clock when using .use_appll true is not as accurate! + @version 0.2 + @date 2024-03-25 + @copyright Copyright (c) 2024 */ #include @@ -24,7 +31,8 @@ short sampleBuffer[BUFFER_SIZE]; // Audio sample buffers used for analysis and display int approxBuffer[BUFFER_SIZE]; // ApproxFFT sample buffer -const double samplingFrequency = 48000; // Frequency range plotted is 2.5kHz (sampling frequency/4) +const double samplingFrequency = 48000; //must match frequency above! +// Frequency range plotted is sampling frequency/4 int print_string[500] = {0}; @@ -58,13 +66,13 @@ void setup() { // initialize PDM with: // - one channel (mono mode) - // - a 200 kHz sample rate + // default PCM output frequency if (!PDM.begin(channels, frequency)) { Serial.println("Failed to start PDM!"); while (1) yield(); } - Serial.println("=====================FFT example ====================="); + pinMode(LED_BLUE, OUTPUT); digitalWrite(LED_BLUE, HIGH); } @@ -73,6 +81,7 @@ void loop() { // Read data from microphone int sampleRead = PDM.read(sampleBuffer, sizeof(sampleBuffer)); + float Freq_with_Max_Amp; sampleRead = sampleRead >> 1; //each sample data with two byte // wait for samples to be read @@ -87,7 +96,7 @@ void loop() { if (first_flag > 10) //Discard the first 10 samples of data { - Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); + Freq_with_Max_Amp = Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); // default return frequency with max aplitude // for (int j=0; j Serial Plotter) - @version 0.1 - @date 2022-06-6 - @copyright Copyright (c) 2022 + @note + 1) To use the ploter function Arduino IDE version 1.x is needed (version 2.x plotter does not work with this code) + @version 0.2 + @date 2024-03-25 + @copyright Copyright (c) 2024 */ #include @@ -20,7 +22,8 @@ short sampleBuffer[BUFFER_SIZE]; // Audio sample buffers used for analysis and display int approxBuffer[BUFFER_SIZE]; // ApproxFFT sample buffer -const double samplingFrequency = 16000; // Frequency range plotted is 2.5kHz (sampling frequency/4) +const double samplingFrequency = 16000; //must match frequency above! +// Frequency range plotted is 2.5kHz (sampling frequency/4) int print_string[500] = {0}; @@ -51,13 +54,11 @@ void setup() { // initialize PDM with: // - one channel (mono mode) - // - a 16 kHz sample rate // default PCM output frequency if (!PDM.begin(channels, frequency)) { Serial.println("Failed to start PDM!"); while (1) yield(); } - Serial.println("=====================FFT example ====================="); pinMode(LED_BLUE, OUTPUT); digitalWrite(LED_BLUE, HIGH); @@ -68,6 +69,7 @@ void loop() { // Read data from microphone int sampleRead = PDM.read(sampleBuffer, sizeof(sampleBuffer)); + float Freq_with_Max_Amp; sampleRead = sampleRead >> 1; //each sample data with two byte // wait for samples to be read @@ -82,7 +84,7 @@ void loop() { if (first_flag > 10) //Discard the first 10 samples of data { - Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); + Freq_with_Max_Amp = Approx_FFT(approxBuffer, BUFFER_SIZE, samplingFrequency); // default return frequency with max aplitude // for (int j=0; j Date: Wed, 24 Apr 2024 19:48:00 -0400 Subject: [PATCH 2/2] V1.0.8 Updated to also show the peak frequency for FFT samples --- library.json | 2 +- library.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 8320dd8..e4416c0 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "RAKwireless-Audio-library", - "version": "1.0.7", + "version": "1.0.8", "keywords": [ "Audio", "DSPG", diff --git a/library.properties b/library.properties index 1cc763a..c1d8359 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=RAKwireless-Audio-library -version=1.0.7 +version=1.0.8 author=RAKWireless maintainer=RAKWireless sentence=RAKWireless library for the RAK audio module