Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to also show the peak frequency #7

Merged
merged 2 commits into from
Apr 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Arduino.h>
Expand Down Expand Up @@ -64,22 +66,22 @@ 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
if (sampleRead > 0) {
// 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<BUFFER_SIZE; j++){
// Serial.println(approxBuffer[j]);
// }
Expand All @@ -88,13 +90,15 @@ void loop() {

for (int j = 0; j < 500; j++)
{
Serial.println(print_string[j]);
Serial.print("Data:"); Serial.print(print_string[j]);Serial.print(", ");
Serial.print("Peak_Freq"); Serial.print(Freq_with_Max_Amp); Serial.print(":"); Serial.print(Freq_with_Max_Amp);Serial.print(", ");
Serial.println();
}
delay(1000);
delay(3000); // wait 3 seconds so plot can be read
}
else
{
first_flag += 1;
first_flag++;
}
sampleRead = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
@brief This example reads audio data from the PDM microphones, 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 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 <Arduino.h>
Expand All @@ -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};

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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<BUFFER_SIZE; j++){
// Serial.println(approxBuffer[j]);
// }
Expand All @@ -90,9 +98,11 @@ void loop() {

for (int j = 0; j < 500; j++)
{
Serial.println(print_string[j]);
Serial.print("Data:"); Serial.print(print_string[j]);Serial.print(", ");
Serial.print("Peak_Freq"); Serial.print(Freq_with_Max_Amp); Serial.print(":"); Serial.print(Freq_with_Max_Amp);Serial.print(", ");
Serial.println();
}
delay(1000);
delay(3000); // wait 3 seconds so plot can be read
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
@brief This example reads audio data from the PDM microphones, 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 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 <Arduino.h>
Expand All @@ -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};

Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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<BUFFER_SIZE; j++){
// Serial.println(approxBuffer[j]);
// }
Expand All @@ -96,9 +105,11 @@ void loop() {

for (int j = 0; j < 500; j++)
{
Serial.println(print_string[j]);
Serial.print("Data:"); Serial.print(print_string[j]);Serial.print(", ");
Serial.print("Peak_Freq"); Serial.print(Freq_with_Max_Amp); Serial.print(":"); Serial.print(Freq_with_Max_Amp);Serial.print(", ");
Serial.println();
}
delay(1000);
delay(3000); // wait 3 seconds so plot can be read
}
else
{
Expand Down
22 changes: 13 additions & 9 deletions examples/RAK11200/PDMSerialPlotterFFT/PDMSerialPlotterFFT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
@brief This example reads audio data from the PDM microphones, 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)
@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 <Arduino.h>
Expand All @@ -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};

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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<BUFFER_SIZE; j++){
// Serial.println(approxBuffer[j]);
// }
Expand All @@ -91,9 +93,11 @@ void loop() {

for (int j = 0; j < 500; j++)
{
Serial.println(print_string[j]);
Serial.print("Data:"); Serial.print(print_string[j]);Serial.print(", ");
Serial.print("Peak_Freq"); Serial.print(Freq_with_Max_Amp); Serial.print(":"); Serial.print(Freq_with_Max_Amp);Serial.print(", ");
Serial.println();
}
delay(1000);
delay(3000); // wait 3 seconds so plot can be read
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "RAKwireless-Audio-library",
"version": "1.0.7",
"version": "1.0.8",
"keywords": [
"Audio",
"DSPG",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=RAKwireless-Audio-library
version=1.0.7
version=1.0.8
author=RAKWireless <rakwireless.com>
maintainer=RAKWireless <rakwireless.com>
sentence=RAKWireless library for the RAK audio module
Expand Down