Skip to content

FFT example#291

Draft
microbit-carlos wants to merge 6 commits intomasterfrom
fft_rebased
Draft

FFT example#291
microbit-carlos wants to merge 6 commits intomasterfrom
fft_rebased

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 <[email protected]>

Merged CMakeLists.txt target link libraries with master - includes relevant CMSIS libraries.
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

@microbit-carlos
Copy link
Collaborator Author

When using this branch for the first time, the CMSIS_5 git clone might sometimes produce the following error:

Downloading CMSIS/Utilities/Win32/PackChk.exe (2.9 MB)
Error downloading object: CMSIS/Utilities/Win32/PackChk.exe (7934974): Smudge error: Error downloading CMSIS/Utilities/Win32/PackChk.exe (7934974e4e929021a7413a609183ce61142c38d9c87e40925a0feec1bc958892): batch response: This repository exceeded its LFS budget. The account responsible for the budget should increase it to restore access.

Errors logged to '/Users/carlos/workspace/tmp/CMSIS_5/.git/lfs/logs/20251218T101326.591298.log'.
Use `git lfs logs last` to view the log.
error: external filter 'git-lfs filter-process' failed
fatal: CMSIS/Utilities/Win32/PackChk.exe: smudge filter lfs failed

This can be fixed by using the GIT_LFS_SKIP_SMUDGE flag when building the project:

# First delete the libraries folder so build.py clones all repos from scratch
rm -rf build/
# This flag skips trying to download any of the git LFS files, which we don't need for the FFT
GIT_LFS_SKIP_SMUDGE=1 python build.py

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.

3 participants