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

FFT example #291

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

FFT example #291

wants to merge 6 commits into from

Conversation

microbit-carlos
Copy link
Collaborator

@microbit-carlos microbit-carlos commented Mar 29, 2023

Reviving the branch originally developed by Raphael and updated by Joshua.

This is not really mergeable on this state as the CMSIS libs are added as a submodule to this repo and it changes the float-abi compiler flag to hard (we need to check if we can compile the CMSIS DSP code with float-abit=softfp).

Maybe it's worth considering moving this as a "CODAL library" that can also be used as a MakeCode extension, as the MicroBitAudioProcessor class is a fairly self-contained DataSink.

I'm not sure if I've wired the right DataSource to MicroBitAudioProcessor as a I get a lot high frequency values in a relatively quiet room (do we have a DataSource that has filtered out noise?) and the calculated frequencies are twice as expected.

/*
if (mic == NULL){
mic = uBit.adc.getChannel(uBit.io.microphone);
mic->setGain(7,0);
}
if (processor == NULL)
processor = new StreamNormalizer(mic->output, 1.0f, true, DATASTREAM_FORMAT_8BIT_SIGNED, 10);
if (fft == NULL)
fft = new MicroBitAudioProcessor(processor->output);
uBit.io.runmic.setDigitalValue(1);
uBit.io.runmic.setHighDrive(true);
*/
// Code above commented out was from the original example, which can
// probably be replaced with this single line, but we need to double check
// the configuration because the fft calculation results are twice the frequency
fft = new MicroBitAudioProcessor(*uBit.audio.splitter->createChannel());

Also, it looks like CMSIS needs CMake 3.13 min, and that's a newer version than the Yotta docker image, so the MakeCode builds will fail.

raphaelgault and others added 4 commits March 29, 2023 18:29
Signed-off-by: Raphael Gault <raphael.gault@arm.com>

Merged CMakeLists.txt target link libraries with master - includes relevant CMSIS libraries.
Signed-off-by: Raphael Gault <raphael.gault@arm.com>
Signed-off-by: Raphael Gault <raphael.gault@arm.com>
Although it currently calculates frequencies that are twice the
signal frequency, but that might be a simple fix once somebody
can look into the audio configuration.
@github-actions
Copy link

github-actions bot commented Apr 6, 2023

Build diff

Base commit: be35cc1d13697a783e08658a94e3af2b6c1afc4e
Action run: https://github.com/lancaster-university/codal-microbit-v2/actions/runs/4630881119

     VM SIZE    
 -------------- 
  [NEW]  +116Ki    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/CommonTables/arm_common_tables.c
  [NEW] +1.83Ki    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_cfft_f32.c
  [NEW] +1.33Ki    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix8_f32.c
  +0.5%    +608    [section .heap]
  +8.5%    +456    /home/runner/work/codal-microbit-v2/codal-microbit-v2/source/main.cpp
  [NEW]    +454    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/ComplexMathFunctions/arm_cmplx_mag_f32.c
  [NEW]    +432    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/source/MicroBitAudioProcessor.cpp
  [NEW]    +420    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_init_f32.c
  [NEW]    +406    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_f32.c
  [NEW]    +190    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/StatisticsFunctions/arm_max_f32.c
  [NEW]    +156    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_cfft_init_f32.c
  [NEW]    +144    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/CommonTables/arm_const_structs.c
  [NEW]     +74    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-microbit-v2/CMSIS_5/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal2.c
  -2.6%     -40    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-core/source/types/Image.cpp
  -1.5%     -52    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-core/source/core/CodalFiber.cpp
  -0.1%     -66    [97 Others]
 -39.7%     -92    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-core/source/core/CodalCompat.cpp
  -7.5%    -156    /home/runner/work/codal-microbit-v2/codal-microbit-v2/libraries/codal-core/source/drivers/AnimatedDisplay.cpp
 -98.4%    -492    /home/runner/work/codal-microbit-v2/codal-microbit-v2/source/samples/MicrophoneTest.cpp
 -26.6% -5.69Ki    [section .text]
 -97.8% -6.12Ki    /home/runner/work/codal-microbit-v2/codal-microbit-v2/source/samples/OOB.cpp
   +29%  +110Ki    TOTAL

@microbit-carlos
Copy link
Collaborator Author

Next steps for this work:

  • It's possible I haven't wired things correctly as the calculated frequencies are twice the expected value, so we need to figure out the issue there
  • The additional resources used by this feature are too large, 100KBs is a huge chunk of flash, we need to look into how to reduce this
  • I believe the FFT calculation time was measure by Joshua at around 10-11 ms, which is not bad, but if we want this to be constantly running in the background we might need it to yield a few times to ensure other things have a chance to run
    • Talk: https://www.youtube.com/watch?v=LzkN4j2DB_w
    • One of the reasons this might be constantly running in the background is the possibility to create an "event block" for MakeCode, so that things can happen when a specific frequency is detected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants