STM32 Fast Fourier Transform (CMSIS DSP FFT) - Phil's Lab

Sdílet
Vložit
  • čas přidán 3. 08. 2024
  • How to implement a Fast Fourier Transform (FFT) on an embedded system (STM32 microcontroller + CODEC) using ARM's CMSIS library. Full walkthrough and demo of a peak frequency detector. PCBs by PCBWay www.pcbway.com
    [SUPPORT]
    Hardware design courses: phils-lab-shop.fedevel.education
    Course content: www.phils-lab.net/courses
    Free trial of Altium Designer: www.altium.com/yt/philslab
    Tag-Connect: www.tag-connect.com/solutions...
    Patreon: / phils94
    [GIT]
    github.com/pms67
    [SOCIAL]
    Instagram: / philslabyt
    [LINKS]
    CMSIS Libraries: github.com/ARM-software/CMSIS_4
    STM32CubeIDE: www.st.com/en/development-too...
    3blue1brown FFT Video: • But what is the Fourie...
    Hardware Walkthrough Video: • Mini 6-Layer Mixed-Sig...
    I2S/DMA Set-Up Video: • STM32 I2S ADC DMA & Do...
    [TIMESTAMPS]
    00:00 Introduction
    01:13 Altium Designer Free Trial
    01:36 PCBWay
    01:56 Previous Videos
    02:27 FFT Basics
    03:04 CMSIS Libraries
    04:24 Adding Libraries to CubeIDE
    06:06 Basic Code Structure
    07:12 Including arm_math.h
    07:43 ARM FFT Function Overview
    09:04 FFT Variables & Defines
    09:56 Initialising FFT
    10:14 Processing Callback (Fill Buffer, Compute FFT)
    12:32 Peak Frequency Detector
    13:26 FFT Complex Result
    14:27 Computing Magnitude
    15:10 Frequency Bins
    17:15 Data via USB
    17:38 Test Set-Up
    18:10 Live Demo
    19:43 Outro
  • Věda a technologie

Komentáře • 66

  • @PhilsLab
    @PhilsLab  Před rokem +14

    Note: For a peak detector, you of course don't actually need to compute the square-root of (real^2 + imag^2) (sqrt being fairly processing-heavy). I left it in for the sake of 'completeness'. Also keep in mind there are many things that can be optimised in this example. The main purpose of this video is simply to show how to incorporate CMSIS DSP libraries, and use certain FFT functions.

    • @imlassuom
      @imlassuom Před rokem +1

      I was looking to find if any one noted that...but your pined comments break my research loop!!

    • @isaacclark9825
      @isaacclark9825 Před rokem +1

      I love DSP content! Thanks for sharing.
      There is a CMSIS function that will calculate the complex magnitude from the FFT output. I understand that a peak detector does not need that optimization but folks who are playing around with the CMSIS libraries might want to give it a try.

    • @jithin_isaac
      @jithin_isaac Před rokem +1

      @@isaacclark9825 Yes we can either use arm_cmplx_mag_f32() for a square-root magnitude or arm_complx_mag_squared_f32() for a non-square root value

    • @robc3863
      @robc3863 Před 10 měsíci +1

      Curious to know why you didn't add CMSIS DSP using the CubeIDE MX Software Pack manager?

    • @acidangel162
      @acidangel162 Před 3 měsíci

      There is another reason why you don't need the square root. You are actually interested in the power of the signal, not the magnitude. This is how CD has 96 dB dynamic range. Every bit gives your 3 dB more amplitude. With CD's 16 bit your amplitude has 48 dB of dynamic range. log10(65536) = 4.8165. But with 48 dB of amplitude you can get 96 dB of signal power. Amplitude tells you the signal voltage but power tells you, well power.
      To get the audio power you square your amplitude. You never need to calculate the expensive square root. Just use the real^2 + complex^2 and you're good to go. This is not only cheaper to calculate but more accurate. If you took the FFT with let's say 4096 samples, your screen might not have that much resolution. You might want to combine each frequency together so that you have the same resolution of for example a piano keyboard. You need to sum the powers together to get an accurate picture. If you sum the amplitudes the image won't look right but if you sum the powers it does.
      In the use case of this video of finding the loudest frequency you might want to get the power in logarithmic units. Just take a log10 of the power. You're interested in how loud that frequency is in deciBels, not how high the signal amplitude is. Another reason not to not calculate the square root is that log10(sqrt(power)) = log10(power) / 2. Square root is just an unnecessary in that case too. Dividing by two is much cheaper.
      I worked on this a couple of years ago. At first I took 2048 square roots and that's expensive. It cost me like 2 milliseconds per frame and I didn't have two milliseconds. Because the accuracy of the square root is not that important I used the fast inverse square root algorithm that was 20 times faster with an error of only 0.07 %. But in the end it turned out that the square root was completely unneeded.
      I hope this helps or that someone found this at least interesting.

  • @yedidiaovadia407
    @yedidiaovadia407 Před 19 dny

    Great Video! Best explanation for adding the CMSIS DSP library!

  • @Jose-tw9bl
    @Jose-tw9bl Před rokem

    Thanks for this video! The part i found most interesting was how to include and configure the ide to use precompiled libraries. In lots of videos people assume we know how to do it but i had no idea. Thanks!

  • @euglossine4ever
    @euglossine4ever Před rokem

    Easily one of the best channels for learning dsp, your precision in teaching is remarkable

  • @bobwalance7237
    @bobwalance7237 Před 10 měsíci

    Phil, this is the only place that I could find with proper format of the output of the rFFT. There is a lot of misinformation out there regarding the arm_rfft_fast_f32. Thank you for presenting such an accurate and easy-to-understand description. Also, it's important to note that this function destroys the values in the input buffer, and this is documented at the Keil webpage for the CMSIS-DSP routines.

  • @ianthehunter3532
    @ianthehunter3532 Před rokem +2

    Wish to understand this some day, great work.

  • @senceryazici
    @senceryazici Před rokem +6

    I just wished we had this awesome video a year ago when we were working on sound source localization using TDOA in a project. ChatGPT + Phil's Lab Videos is a powerhouse combo that equips one with all the tools we need to tackle any challenge haha :D The quality in this channel is unbeatable, as always, Excellent!

    • @PhilsLab
      @PhilsLab  Před rokem

      Thank you so much, Sencer!

    • @mausmettwurst7039
      @mausmettwurst7039 Před 6 měsíci

      Hey, I am working on a similar project. Do you have an public repository with your project?

  • @ahmedkamil7655
    @ahmedkamil7655 Před rokem +1

    Excellent work

  • @MeeBilt
    @MeeBilt Před rokem +1

    Great video as always!

  • @fagnereng
    @fagnereng Před rokem +1

    Please, make a video implementing the fft algorithm from scratch as you suggested on this video. We would appreciate it! Congratulations for this video.

  • @PCBWay
    @PCBWay Před rokem

    Always good to hear your thoughts, musing, and recommendations, Phil. :)👍👍👍

  • @imoldovan
    @imoldovan Před rokem

    Getting closer to using this for receiving rf... :)

  • @severingetzner8211
    @severingetzner8211 Před 9 měsíci +2

    I would beg you to upload the code. I am really struggeling to get this DMA working proberly and I can see from you comments in the code that you also tried alot to get it working on the STM32H723.
    Thank you for all the video uploads.

  • @theintjengineer
    @theintjengineer Před rokem

    I have an H757i-Eval Board which is a dual-core board, which makes it difficult when it comes to Tutorials, as most of them are based on single-core boards, meaning I always have the problem of - how to handle the CM4, for instance. What to do with it, start it or not, delegate stuff to it or not and so on.
    It was a gift, so, I didn't have much of a say. But I'm now thinking of buying a single-core board to educate myself and know enough to know how to appropriately handle the dual-core one.

  • @marek_ryn
    @marek_ryn Před rokem +1

    Excellent video!! Just small optimization: when searching peak frequency in the loop you can omit sqrtf function, as it is very costly. Thank you for all your videos, I learn so much from them!

    • @PhilsLab
      @PhilsLab  Před rokem +1

      Thanks, Marek! That's correct - although I left in the sqrt for sake of 'completeness'/'correctness'. Given the rather crude nature of the surrounding implementation and no need for 'speed increase', it doesn't make much of a difference for this example.

    • @fagnereng
      @fagnereng Před rokem

      So how could i find the peak frequency without calculate the absolute value from fft output?

  • @R2AUK
    @R2AUK Před 10 měsíci

    Many thanks for the video. I noticed that you choose CMSIS_4 while CMSIS_5 is available. Is there a reason to prefer the older version?

  • @musicforsoul3324
    @musicforsoul3324 Před rokem +2

    Thank you for your amazing tutorial. If you are interested in only FFT frequency do you have to add imaginary part to calculate value. I think without it you would save some processing time

    • @PhilsLab
      @PhilsLab  Před rokem +3

      Thanks for watching. You need to include the imaginary & real part in the calculation of magnitude.

  • @RemcoStoutjesdijk
    @RemcoStoutjesdijk Před rokem +1

    You might want to add in the library as source instead.. I went from 95% CPU load to roughly 70% on a cortex-m4 for a customer. It looks like the library as compiled doesn't use any optimizations. Also, it appears to have been compiled with round-as-floor where I could get about 6dB lower noise floor by changing that to round to nearest (if memory serves).

    • @dmarman
      @dmarman Před 3 měsíci

      Any chance of an example or guides on how to add the library as source? Just adding the headers files and include paths to the project?

  • @lucvanhove9639
    @lucvanhove9639 Před rokem +3

    Hi Phil. I use STM32 for a while and it has a lot of bennefits agains Arduino. Mostly I learn programming from Digi Key CZcams cours, Controllers Tech and books from Elector. But your movie was the next level and I appreciat it a lot. Where can I find all this study info about the Cube compiler, I mean books or ... Thanks a lot.

    • @PhilsLab
      @PhilsLab  Před rokem +1

      Thanks! I'd suggest working on a real-life project and figuring out what you need (Google, Stackoverflow, datasheets, etc..) when the time comes during that project. That approach works the best for me.

  • @user-sp8xn6zb3w
    @user-sp8xn6zb3w Před rokem

    Very nice vid Phil, been always fan of your designs! Do you think this approach differs compared to using the HAL_ADC or HAL_DAC HalfConv/Conv Callbacks in case of standard ADC/DAC DMA streaming (i mean by taking into account the computational time, audio quality etc) ?

    • @PhilsLab
      @PhilsLab  Před rokem +2

      Thank you!
      Audio quality is not really achievable using the STM32's built-in ADCs/DACs. I had tried this a while back, but the ENOB is too low, even with oversampling. For other applications, where you only need 12bits or less, then the internal ADCs/DACs can achieve that.
      However, the actual FFT implementation as shown in the video can be done in pretty much the same way if you're if using the STM32's 'on-board' ADCs and DACs.

    • @user-sp8xn6zb3w
      @user-sp8xn6zb3w Před rokem

      @@PhilsLab Thank you for your quick response. I am actually using it that way to minimize components on board (and the complexity of a codec but might go that way though) and being using FFT with a noise level cutoff to achieve a better signal. But i assume the better the signal in the input the better the output

  • @camk2552
    @camk2552 Před rokem

    Great Videos on DSP thanks!

  • @Kale514-pt8tg
    @Kale514-pt8tg Před 9 měsíci

    Hi. If I have a 1kHz sinusoidal signal input to the ADC channel, how do I give the SAMPLING_RATE a value in the code? I am not using I2S or SAI1 as in your case. If I am not mistaken, I should set the sampling rate to at least twice the frequency of the input signal, right? Thank you!

  • @gretarmark
    @gretarmark Před rokem +1

    This was great! I once implemented an simple peak detector using Arduino to detect the peaks of a sine wave signal. The purpose was to keep the signal at 50Hz, just like the power system. But I didn't think about doing it by using FFT. Do you think it is more benefitial to use FFT to do it rather than just basic math to detect peaks?

    • @user-sp8xn6zb3w
      @user-sp8xn6zb3w Před rokem +1

      Hello, i am not Phil but every opinion counts i assume :D I think an envelope detector, if well configured, is better for your purpose than FFT because FFT provides delay depending on the accuracy you need.Also arduino is not as good mcu as stm32 in this case, so FFT would not be that good for this case.

    • @gretarmark
      @gretarmark Před rokem

      Hello @@user-sp8xn6zb3w, thank you for the answer. That's true, STM32 is much better, I had to use atmega328p in a course I took in my study program, otherwise I would have go for something better. But FFT is actually very fast algorithm, it was invented by James Cooley and John Tukey to make fourier calculations as fast as possible. I once heard that FFT is used in ABS braking systems of cars because of its speed and accuracy, so I was just wondering. But it's speed is O(NlogN), which is very good speed.

  • @user-om7os8gj8x
    @user-om7os8gj8x Před rokem

    Thank you very much, your tutorials are very important!
    Do you have intention to create DSP programming tutorials for fpga zynq?
    It would be amazing!

    • @PhilsLab
      @PhilsLab  Před rokem

      Thanks for watching! Yeah, I wanna make some 'basic' FPGA DSP tutorials first (Spartan 7), before moving over to doing that on the Zynq (although the workflow is of course rather similar).

  • @coderebel3568
    @coderebel3568 Před rokem

    Hi Phil! I was wondering how you're finding those tag-connect connectors working for you. I noticed you've used two different versions. In Phil's lab #45 I noticed the Tag-Connect connector doesn't have four large holes around it and here in this design it does. I've seen some of those tag-connect connectors have some plastic legs that seem to be for holding on to the PCB. Is that the reason why you switched? And is there perhaps already a video where you talk about the version you're using here? I'm thinking about a custom design for a DSP board in my guitar with an STM32H7A3 and contemplating using such a tag-connect connector for programming as well..
    By the way, I've tried experimenting with an AD1937 audio codec before, but I couldn't get it to work. I don't know what I'm doing wrong. Either it's an EMI issue, or I'm not setting up the PLL correctly or I'm making a software error.. Or I messed up the chip while soldering or something.. I did just get an easy to use MAX98357 on an adafruit breakout board to produce sound using DMA with a Nucleo F401, but for the guitar I'd prefer to use a more powerful codec chip with multiple inputs and outputs. Could you do a video about setting up audio codecs in the general sense, like what do they mean with 256fs and such. I have some idea about what they mean and how to set it up, but with all the assumptions I'm having to make, I think I'm just making too many mistakes and not quite sure how to debug it. I've searched around for some tutorials about that and found nothing, so maybe that's an idea. I like your channel a lot. I've learned so much! Thanks a lot!

  • @gabrielbortto
    @gabrielbortto Před rokem

    I'm getting a Hard Fault when the function arm_rfft_fast_init_f32(&fftHandler, FFT_BUFFER_SIZE); gets called.
    Any ideas?
    I am using a STM32F411CEU6. I have the FPU set to hard and float.

  • @stickyfox
    @stickyfox Před rokem +1

    Is CubeIDE the current version of the "System Workbench" Eclipse package? Because *that* was a disaster, and why I stopped even trying to use STM32 chips in the first place. The online community was dead and so was the support for the software. I tried using VSCode/PlatformIO but it never installed properly because Cygwin was also on my machine, so I just said forget it.
    It would be fantastic if they've gotten a real working dev suite that isn't pay-to-play.

  • @Thats_Mr_Random_Person_to_you

    Totally off topic: as Altium also make/own CircuitMaker which is free would be interesring to have an opinion / design example walkthrough (being a tech consultant for Altium I think it does fit heavily into your overall content so its not too off piste if you did think it interesting/worth doing).

    • @PhilsLab
      @PhilsLab  Před rokem +1

      Thanks, good idea! I haven't used it at all myself to be honest, but will give it a try for a future video.

  • @EmbeddedEnigma
    @EmbeddedEnigma Před 17 dny

    what is the formula used on int16_t_to_float directive ?

  • @Ozonised
    @Ozonised Před rokem

    Where did you learn digital signal processing from? Was it from college or the internet? If it was from the internet then can you point me to some good sources or books?

    • @PhilsLab
      @PhilsLab  Před rokem +3

      We had some introductory DSP classes at uni. The rest is pretty much self-taught. Check out: "The Scientist and Engineer's Guide to Digital Signal Processing" (SMITH) and - if you're into audio - "DAFX: Digital Audio Effects" (ZÖLZER).

    • @Ozonised
      @Ozonised Před rokem

      ​@@PhilsLabThank you Phil :)

  • @shettymalnad1978
    @shettymalnad1978 Před 5 měsíci

    Hello, DSP_CMSIS library limits to 4096 points in my project needs to 16384 point how to achive this ? which library is there to do how to add and and achive any suggestion,links or videos will be very helpfull

  • @FixDaily
    @FixDaily Před rokem

    What if you sweep across frequencies, will it detect the highest?

    • @ChromeExtension
      @ChromeExtension Před rokem

      Depends on your SWEEP rate + SAMPLING rate + FFT buffer size.
      This peak frequency detector doesn't detect what is the "highest" frequency but rather what frequency component makes up majority of the input signal.
      For a sweep, an ideal DFT of the full sweep will result in peaks of all the same magnitude (i.e. all frequency components contribute equally to the input signal) [imagine a column chart with all same heights, from 0 - max sweep frequency]. Assuming the sweep is linear and sampling windows line up.
      If the sweep doesn't fit within buffer, e.g. like in this video, since it seems to be calculating FFT many times a second, then for each FFT buffer period, it will result in a similar picture to situation above, except starting frequency will not be 0 and ending frequency will not be max sweep either. Basically it will show chunks of column charts based on which part of the sweep it is at/sampled.
      To implement a highest frequency detector, like what I think you are suggesting, you would need to first determine what threshold or magnitude is considered as being more than NOISE/significant enough.
      Then just evaluate the FFT buffer in reverse from highest frequency, until you find a bin that exceeds your threshold.
      To understand more, find some videos about Fourier Transforms, like the one suggested in video, it will clear the confusion about frequency vs magnitude / components

    • @PhilsLab
      @PhilsLab  Před rokem

      Depends on how fast the sweep is, how long your FFT is, and so on.

  • @BlueStream123
    @BlueStream123 Před rokem

    Do you know a cheap alternative to the TAG Connector? Because these connectors cost about 40$...

    • @PhilsLab
      @PhilsLab  Před rokem +1

      The initial cost is quite high, yes - but depending on how many boards you have made, assembled, programmed etc. the cost justifies itself quite quickly.
      Don't know of any alternatives, other than making your own.

    • @BlueStream123
      @BlueStream123 Před rokem

      @@PhilsLab Okay thank you. Nice video btw :)

    • @rjordans
      @rjordans Před rokem

      There are some pogo pin connector variations that clip onto the board edge. The main constraint with those is that you need to put the connector footprint near the board edge which may not be the most convenient place

  • @dharmasastha8632
    @dharmasastha8632 Před měsícem

    Can you send source code?

  • @luanphamng
    @luanphamng Před 9 měsíci

    What're you doing for living?