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

  Рет қаралды 41,380

Phil’s Lab

Phil’s Lab

Күн бұрын

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

Пікірлер: 66
@PhilsLab
@PhilsLab 11 ай бұрын
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 11 ай бұрын
I was looking to find if any one noted that...but your pined comments break my research loop!!
@isaacclark9825
@isaacclark9825 11 ай бұрын
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 10 ай бұрын
@@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 8 ай бұрын
Curious to know why you didn't add CMSIS DSP using the CubeIDE MX Software Pack manager?
@acidangel162
@acidangel162 Ай бұрын
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.
@alifesh
@alifesh 11 ай бұрын
Looking forward to your implementation of discreet FFT!
@PhilsLab
@PhilsLab 11 ай бұрын
Thanks - will be working on that soon! :)
@camk2552
@camk2552 11 ай бұрын
@@PhilsLab That would be sweet Implemented on FPGA?
@Jose-tw9bl
@Jose-tw9bl 11 ай бұрын
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!
@bobwalance7237
@bobwalance7237 8 ай бұрын
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.
@euglossine4ever
@euglossine4ever 11 ай бұрын
Easily one of the best channels for learning dsp, your precision in teaching is remarkable
@PhilsLab
@PhilsLab 11 ай бұрын
Thank you very much!
@ianthehunter3532
@ianthehunter3532 11 ай бұрын
Wish to understand this some day, great work.
@PhilsLab
@PhilsLab 11 ай бұрын
Thank you, Ian!
@fagnereng
@fagnereng 11 ай бұрын
Please, make a video implementing the fft algorithm from scratch as you suggested on this video. We would appreciate it! Congratulations for this video.
@senceryazici
@senceryazici 11 ай бұрын
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 11 ай бұрын
Thank you so much, Sencer!
@mausmettwurst7039
@mausmettwurst7039 4 ай бұрын
Hey, I am working on a similar project. Do you have an public repository with your project?
@R2AUK
@R2AUK 8 ай бұрын
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?
@imoldovan
@imoldovan 11 ай бұрын
Getting closer to using this for receiving rf... :)
@MeeBilt
@MeeBilt 11 ай бұрын
Great video as always!
@PhilsLab
@PhilsLab 11 ай бұрын
Thank you!
@ahmedkamil7655
@ahmedkamil7655 11 ай бұрын
Excellent work
@PhilsLab
@PhilsLab 11 ай бұрын
Thanks, Ahmed!
@theintjengineer
@theintjengineer 11 ай бұрын
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.
@coderebel3568
@coderebel3568 10 ай бұрын
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!
@severingetzner8211
@severingetzner8211 7 ай бұрын
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.
@Kale514-pt8tg
@Kale514-pt8tg 6 ай бұрын
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!
@musicforsoul3324
@musicforsoul3324 11 ай бұрын
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 11 ай бұрын
Thanks for watching. You need to include the imaginary & real part in the calculation of magnitude.
@user-sp8xn6zb3w
@user-sp8xn6zb3w 11 ай бұрын
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 11 ай бұрын
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 11 ай бұрын
@@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
@gretarmark
@gretarmark 11 ай бұрын
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 11 ай бұрын
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 11 ай бұрын
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.
@gabrielbortto
@gabrielbortto 11 ай бұрын
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 11 ай бұрын
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.
@RemcoStoutjesdijk
@RemcoStoutjesdijk 11 ай бұрын
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 Ай бұрын
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?
@PCBWay
@PCBWay 11 ай бұрын
Always good to hear your thoughts, musing, and recommendations, Phil. :)👍👍👍
@PhilsLab
@PhilsLab 11 ай бұрын
Thank you very much! :)
@lucvanhove9639
@lucvanhove9639 11 ай бұрын
Hi Phil. I use STM32 for a while and it has a lot of bennefits agains Arduino. Mostly I learn programming from Digi Key KZbin 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 11 ай бұрын
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.
@marek_ryn
@marek_ryn 11 ай бұрын
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 11 ай бұрын
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 11 ай бұрын
So how could i find the peak frequency without calculate the absolute value from fft output?
@shettymalnad1978
@shettymalnad1978 3 ай бұрын
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
@user-om7os8gj8x
@user-om7os8gj8x 11 ай бұрын
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 11 ай бұрын
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).
@Ozonised
@Ozonised 11 ай бұрын
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 11 ай бұрын
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 11 ай бұрын
​@@PhilsLabThank you Phil :)
@Thats_Mr_Random_Person_to_you
@Thats_Mr_Random_Person_to_you 11 ай бұрын
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 11 ай бұрын
Thanks, good idea! I haven't used it at all myself to be honest, but will give it a try for a future video.
@camk2552
@camk2552 11 ай бұрын
Great Videos on DSP thanks!
@PhilsLab
@PhilsLab 11 ай бұрын
Thank you!
@BlueStream123
@BlueStream123 11 ай бұрын
Do you know a cheap alternative to the TAG Connector? Because these connectors cost about 40$...
@PhilsLab
@PhilsLab 11 ай бұрын
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 11 ай бұрын
@@PhilsLab Okay thank you. Nice video btw :)
@rjordans
@rjordans 11 ай бұрын
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
@FixDaily
@FixDaily 11 ай бұрын
What if you sweep across frequencies, will it detect the highest?
@ChromeExtension
@ChromeExtension 11 ай бұрын
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 11 ай бұрын
Depends on how fast the sweep is, how long your FFT is, and so on.
@luanphamng
@luanphamng 7 ай бұрын
What're you doing for living?
PCB Traces 101 - Phil's Lab #112
30:19
Phil’s Lab
Рет қаралды 40 М.
Understanding the Discrete Fourier Transform and the FFT
19:20
Заметили?
00:11
Double Bubble
Рет қаралды 2,8 МЛН
He tried to save his parking spot, instant karma
00:28
Zach King
Рет қаралды 12 МЛН
Running DSP Algorithms on Arm Cortex M Processors
57:23
Lefteris Kostoulas
Рет қаралды 33 М.
M.2 System-on-Module Hardware Design - Phil's Lab #107
32:05
Phil’s Lab
Рет қаралды 38 М.
Denoising Data with FFT [Python]
10:03
Steve Brunton
Рет қаралды 166 М.
DSP Overdrive Algorithm in Software (STM32) - Phil's Lab #117
32:52
EEVblog #845 - Oscilloscope FFT Comparison
37:00
EEVblog
Рет қаралды 233 М.
FIR Filter Design and Software Implementation - Phil's Lab #17
30:35
FFT in excel for spectral analysis
11:33
Mike Holden
Рет қаралды 118 М.
The Fast Fourier Transform (FFT)
8:46
Steve Brunton
Рет қаралды 335 М.
3.5.A Solar Mobile 📱 Charger
0:39
Gaming zone
Рет қаралды 320 М.
Не обзор DJI Osmo Pocket 3 Creator Combo
1:00
superfirsthero
Рет қаралды 1,2 МЛН
Apple watch hidden camera
0:34
_vector_
Рет қаралды 53 МЛН