No video

The Simplest Digital Filter (STM32 Implementation) - Phil's Lab #92

How to implement a simple digital filter (low-pass and high-pass exponential moving average (EMA)) on a real-time embedded system (STM32 + audio codec). Useful for noise reduction, removing DC offsets, and more in simple situations.
Discussion on filter basics, advantages & disadvantages, theory, implementation in C, and real-time time- and frequency-domain tests.
[SUPPORT]
Free trial of Altium Designer: www.altium.com...
PCBs manufactured and assembled by PCBWay: www.pcbway.com
Patreon: / phils94
Mixed-signal hardware design course: phils-lab-shop...
Advanced Hardware Design Course Survey
forms.gle/X4jw...
[GIT]
github.com/pms67
[LINKS]
Instagram: / philslabyt
[TIMESTAMPS]
00:00 Introduction
00:44 Altium Designer Free Trial
01:15 What We'll Look At
01:47 EMA Filter Basics
03:40 Digital Filter Basics
05:03 Low-Pass Filter Theory
08:36 Filter Coefficient Effect on Frequency Response (Alpha)
09:57 Software Implementation in C (Low-Pass)
13:11 Low-Pass Filter Real-Time Test
16:39 High-Pass Filter Theory
18:32 Filter Coefficient Effect on Frequency Response (Beta)
19:11 Software Implementation in C (High-Pass)
20:23 High-Pass Filter Real-Time Test
23:04 Outro

Пікірлер: 89

  • @naturevolt
    @naturevolt Жыл бұрын

    Phil - nice video. A unique thing about alpha = 0.5 for the low pass case is that it can be solved with just shifting the input by one bit (divide by 2) and adding it to the previous solution shifted by one bit (divide by 2). This is math any low end micro can achieve.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, David - that's a great point!

  • @1over137

    @1over137

    Жыл бұрын

    @@PhilsLab Just be careful with shifts and signed numbers.

  • @robdavis3220
    @robdavis3220 Жыл бұрын

    great stuff. Nice to see an actual implementation in C , not just a bunch of theory. Can you show some higher order filters as well , maybe 2'nd and 3'rd order.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you, Rob! I'm planning on making some videos on 'classic' IIR filters (e.g. Butterworth, Chebyshev, ..) which will also cover higher order filters.

  • @1over137
    @1over137 Жыл бұрын

    It has been pointed out to me that you don't need buffers, circular arrays, double buffering, DMA or even interrupts to process a realtime audio stream. I refused to believe it, but I tried it and... block reading and block writing 1 stereo sample at a time still leaves you PLENTY of time to process each sample through the filters. Zero latency. No sample rate conversion either, that happens automatically when you trigger off the cadence of the output. Also, the SAI blocks are worth looking into. They can be easily used as basic I2S interfaces as normal, but can do way more, including the support for 8 octet unblocking FIFO.

  • @pietrogagliano4484
    @pietrogagliano4484 Жыл бұрын

    Awesome! I always love these digital filter videos and I'll have to really start doing some digital filter hardware design projects myself! Thank you for the great content as always! I just wanted to ask as well about the status of the advanced hardware design course, can't wait for it to come out. Thank you again Phil

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Pietro :) I'm still working on the course. Unfortunately, due to Chinese New Year, the course board production has been delayed a bit. I hope to have them in my hands and tested by the end of February.

  • @pietrogagliano4484

    @pietrogagliano4484

    Жыл бұрын

    @@PhilsLab Sounds good! Yeah now that I have altium for work I have had time to experiment and learn a lot with the tool and I'm using to design a personal zynq based board that includes usb otg, ethernet, and usb to uart. I'm nearly done with the layout and a lot of your earlier videos from this channel and altium are becoming indispensable in helping me work out the most optimal layout patterns and work flows!

  • @isaacclark9825

    @isaacclark9825

    Жыл бұрын

    I want to warn you that doing digital filter projects can become very addictive. It has digested a bunch of my time. I wish the same for you.

  • @mbarras_ing
    @mbarras_ing Жыл бұрын

    Brill as always Phil, very clear explanation. Wish I had these when starting out. I remember the aha moment when I "implemented" a moving average filter in excel (of all software..). But the cells helped me think of the z domain, and individual samples.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you, Matthew! Agreed - actually implementing this (regardless of the method) really showed the usefulness of all the maths I'd been learning at uni.

  • @LLiiaamm_
    @LLiiaamm_ Жыл бұрын

    Great video as always! Would love to see an implementation of a Chorus effect. It utilizes LFO, delays, and can easily be switched between vibrato and chorus by changing the wet and dry mix. Keep it up, Phil 👍

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Liam! I actually implemented a chorus a while back, which I've been meaning to show. So many cool DSP things to play around with - definitely will be in an upcoming video :)

  • @seany9519
    @seany9519 Жыл бұрын

    Great video Phil, your hardware setup is really clean. It would interesting if you explored linear phase FIR filters. Those filters enable finer control over magnitude and phase response while offering constant group delay. Very important in many applications. A DSP board like yours could implement some reasonably long filters

  • @MusaYmc
    @MusaYmc Жыл бұрын

    Never would have though I'd be implementing filters on stm32 for fun, thank you for the guide!

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks for watching, Kadir!

  • @shaungovender7805
    @shaungovender7805 Жыл бұрын

    Hi Phil, as always your DSP videos are pure quality. Loving this content. Perhaps in future you could also do videos on Digital Control Systems. A lot of DSP theory overlaps with digital control.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Shaun - I'd definitely like to do some control stuff. I actually had a DC motor control board made a while back that I still need to make a video on...

  • @HOKKEY
    @HOKKEY Жыл бұрын

    Thank You for Sharing !!

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks for watching!

  • @yanivginlevy
    @yanivginlevy3 ай бұрын

    I find that if calc moving average over a window, let's say window=32, so averaging over 32 samples then when accelerometer read angle movement bigger then 0.1 degree I decrease the window size to 1 then increasing every new sample until 32. it is super simple and the result is exponentially follow the original signal plus the smoothing of course

  • @thelastofthemartians
    @thelastofthemartians Жыл бұрын

    Nice vid as usual. One minor point though: it may be better to compute 1-alpha once and store it in the struct rather than compute it for every sample. Mind you, the compiler might do that optimisation for you anyway. It would be interesting to see the assembler code it produced.

  • @anagram
    @anagram Жыл бұрын

    Great video! The technical aspect is well explained, but I was wandering how do the filter sound on common waveforms and a real world example.

  • @myetis1990
    @myetis1990 Жыл бұрын

    Hey Phil, great content as ever. I would love to see a video that shows the usage of CORTEX-M4F DSP features. using this feature with DMA would be very delicious for many applications(especially kalman filtering :)

  • @UncleWalter1
    @UncleWalter1 Жыл бұрын

    This makes me think of the filter I used to implement in flash games when I was a kid to smooth out motion. It worked out to something like Yₙ = Yₙ₋₁ + (Xₙ - Yₙ₋₁) * α. In practice it was current_position += (target_position - current_position) / how_sluggish_I_want_it_to_move because it was easier for me to visualise the motion increment as dividing up the line between current_position and target_position. Later I changed the last part to a multiply when I learn about how expensive division can be. Though in fixed-point, a lot of the time, the division does get optimised out. Don't use it for much these days except lazily removing zipper noise from level controls, but it's a handy one-liner nonetheless.

  • @supersynthesis

    @supersynthesis

    3 ай бұрын

    'out += (in - out) * a' is what I use too, and it is actually identical to 'out = (in * a) + (1.0 - a) * out' used here. the one liner feels easier for me to understand, and represents an RC curve very intuitively. I'm actually skeptical of the waveforms phil is getting here, I think the codec is impacting the output signal. this simple form of lowpass should not be introducing ripple or ringing or the soft edge situation we see here.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel Жыл бұрын

    Excellent video and a filter I will use in my video, but I will probably dumb it down for my audience. I will be using an accelerometer for robotic balancing applications (spikes occur if there are abrupt vibrations, whether from abump, or from the vibration from a servo), but I was also thinking of using gyro and adding angular velocity (in some way) to improve the angle measurement. I will try the filtering first to see how well it works. I may need to use a filter that considers more samples as well. When I get to this video, I will make sure to mention this video so viewers can get more detail.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you! If you're interested, I have some sensor fusion videos on my channel that go into how to combine acc + gyro measurements to get angle estimates. In any case, thanks for the shout-out in your upcoming video!

  • @PatrickHoodDaniel

    @PatrickHoodDaniel

    Жыл бұрын

    @@PhilsLab Excellent. I will definitely check them out!!

  • @Kurtsi
    @Kurtsi Жыл бұрын

    Glad to find your channel! There seems to be incorrect sign at the Sigma operator on the high pass block diagram. According to the block diagram, it is x(z) + x(z-1).

  • @thetastefultoastie6077
    @thetastefultoastie6077 Жыл бұрын

    I found this low-pass filter in a flash game over 10yrs ago and have been using it everywhere since, it's so useful! Although the one I found was in the form: smoothed += (raw - smoothed) / factor; // Where factor is 1 or more

  • @UncleWalter1

    @UncleWalter1

    Жыл бұрын

    Heh! I came here to say this.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Which flash game was this?

  • @thetastefultoastie6077

    @thetastefultoastie6077

    Жыл бұрын

    @@PhilsLab "Madness Interactive" It was on newgrounds back in the day and I found and studied the source code and learned enough to make my own flash games, and that was the beginning of my programming journey. Now I'm a qualified professional with degrees and everything, but I wonder what would have been if not for that game.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Oh gosh, that brings back memories! I had completely forgotten that that's what it was called. I remember "wasting" quite a lot of time on those..

  • @N4HAY
    @N4HAY Жыл бұрын

    Very professional and clear. Thanks.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you, Richard!

  • @BillySugger1965
    @BillySugger1965 Жыл бұрын

    I have used these filters for years in simple digital filtering applications. And this video treats the subject very well. The one thing I would disagree with is that these filters are bad at separating frequencies. They’re not, you just have to use them within the range of their effective capability. For a low pass filter of this form, A = Fc/Fs where Fc the cutoff frequency and Fs is the sample frequency. BUT… the effective capability limit is that A

  • @Ncky
    @Ncky Жыл бұрын

    It would be nice if you could add this to your planned projects. Digital filter with stm32 that will work like MSEGQ7 ic and drive addressable led strips.

  • @---658
    @---658 Жыл бұрын

    Thanks you very much for detailed and focus video. Really cool! Best regards

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks for watching!

  • @ChaplainDaveSparks
    @ChaplainDaveSparks2 ай бұрын

    And … things come full circle … I have a degree in electrical engineering and now I trade options. It’s interesting how the exponential moving average is such a staple in stock/options/commodities trading, especially using two moving averages of different time periods and watching for _”crossovers”._ For example the classic _”Golden Cross”_ and _”Death Cross”_ which occur when the 50 day and 200 day moving averages cross. I’d love to see the engineering/signal equivalents of these two and what they might represent (if anything).

  • @dmitry.shpakov
    @dmitry.shpakov Жыл бұрын

    It would be nice to see a performance comparison of the two representations of the equation: 1. y_n = alpha * x_n + ( 1 - alpha ) * y_n-1 2. y_n = y_n-1 + alpha * ( x_n - y_n-1 ) Because the first equation uses two multiplication operations and one summation, but the second equation uses one multiplication operation and two summations.

  • @7alfatech860
    @7alfatech860 Жыл бұрын

    Great content as always!

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you!

  • @matthewprestine1974
    @matthewprestine1974 Жыл бұрын

    The H723 has a built-in FMAC, ever consider covering this as a topic? Many of these higher end units have these features and would be a great series to compare them to roll your own types.

  • @charliegilliland6453

    @charliegilliland6453

    Жыл бұрын

    Wow.. just read an application note on FMAC. Seems really interesting! As I understand, FMAC is slightly slower than the CMSIS DSP library, but has the benefit of freeing up the CPU for other tasks. Also worth noting that FMAC only operates on signed 16-bit integers using fixed-point arithmetic. Agree this would be a topic worth covering!

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    That's a great idea - will definitely be making a video on that at some point in the future.

  • @adrianovianawerneck472
    @adrianovianawerneck472 Жыл бұрын

    I Phil good with these videos.

  • @hessel9390
    @hessel93908 ай бұрын

    So if we put 2 LowPass filters behind each other we can make a 2nd degree low pass filter? And then if we combine a low pass and high pass filter you can make a band pass filter?

  • @3238juan
    @3238juan Жыл бұрын

    Tks! can you show a tutorial on designing a board for the f1c100s/ f1c200s? I think this SOC is a beast for its price

  • @MrGraf1992
    @MrGraf19925 ай бұрын

    Nice video! But how was the parameter (2-beta)/2 chosen for the high-pass filter? For the derivation of the high-pass filter, I can only ever find the highpass=1-lowpass equation and this parameter is missing.

  • @sumedhburbure4173
    @sumedhburbure4173 Жыл бұрын

    Great video!

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks!

  • @MSP_TechLab
    @MSP_TechLab Жыл бұрын

    I've started using STM32 from st32f401 (blackpill board). And its ADC upset me much, because even with noise very low noise levels on oscilloscope that thing gave me 6-7 stable bits out of 12bit mode. Some people said pcb design is bad, others that chip itself has pretty bad ADC. But nevertheless I started to search noise canceling technics and found pretty good method in AN4073 which is basically median filter + moving average filter. But I think it can be improved even more with EMA filter. Thanks.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Yeah, most of the in-built ST ADCs aren't as great as "initially advertised". For audio, I'd definitely always go with an external codec.

  • @mvs3246

    @mvs3246

    Жыл бұрын

    SAR ADC of STM32 is a bit noisy, but with 2.4MSPS also quite fast. Depending on application you can use oversampling to trade speed and power consumption for accuracy.

  • @Heisenberg2097
    @Heisenberg2097 Жыл бұрын

    I recently tried Platform IO to compile for ESP32 and it is so much faster than Arduino IDE.

  • @marek_ryn
    @marek_ryn Жыл бұрын

    Hi! As always excellent video. Is there a chance to show hot to use CMSIS DSP library on STM32?

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Marek! I'd like to cover that in the future.

  • @russianultraviolet4441
    @russianultraviolet4441 Жыл бұрын

    Thanks! Could elaborate on higher order filters? What can we achive by going 2nd through 5th order? They are not hard to implement . If u can implent the 1st order then u can impent the 5th order as well. Can i get a really steap ripple-free high-pass filter by going a few orders higher? Thanks again

  • @Xer777Xes
    @Xer777Xes Жыл бұрын

    Could you make a video with the goertzel algorithm?

  • @maxsintar108
    @maxsintar108 Жыл бұрын

    Hi Phil, great video as always. I noticed you defined a filter structure in section .audiobuffer. Is that section defined in a linker script? Would be interesting to see any linker script tutorial from you!

  • @robertpeters9438
    @robertpeters94388 ай бұрын

    USE BROADER GRAPH LINES FOR US TO SEE THE COLORS.

  • @KingdomChablo
    @KingdomChablo Жыл бұрын

    Hi Phil, (this relates to all of your projects you design and make) I would like to know how you effectively document a project. From start to finish. What software do you use to actually do this? I see you do alot of work which is very impressive. Some projects, such as the xilinx one isn't straightforward. So how do you do project management so effectively.

  • @isaacclark9825
    @isaacclark9825 Жыл бұрын

    Great content, as always! I'd be interested in seeing how you set up double buffering with DMA on an STM32H7. It seems that folks have had problems with that in the past.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Isaac! Good idea - it did take me a bit to work out the H7's quirks with cacheing, MPU config, and so on - especially when trying to use DMA.

  • @J-K-AG
    @J-K-AGАй бұрын

    I can only see at 360p video quality or pay premium for 1080p.

  • @VladimirDjokic
    @VladimirDjokic Жыл бұрын

    Cool, I ❤ your videos

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you, Vladimir!

  • @theverner
    @theverner5 ай бұрын

    But how to avoid phase shift in applocation?

  • @guruG509
    @guruG509 Жыл бұрын

    Pls make a video on quaternions

  • @eleclab4244
    @eleclab4244 Жыл бұрын

    Great as always mr.phil! Have you tried the SigmaDSP from analog devices?

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you! I'd like to but haven't found a reason to pay for the license fees, as I can do everything I want with STM32s.

  • @Quantum_Dots
    @Quantum_Dots Жыл бұрын

    Awesome

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thank you!

  • @sc0or
    @sc0or Жыл бұрын

    Is this true that the alpha means a reduction ratio at a freq nykvist/2? I can see that alha 0.1 means -20dB at f/2, and alpha 0.5 means -6dB at the same freq.

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    At fc=fs/2 (i.e. z=-1), I get that the gain=alpha/(2-alpha). So for alpha={0.1,0.5,0.9}, gain={-26,-10,-2}dB.

  • @sc0or

    @sc0or

    Жыл бұрын

    @@PhilsLab Right. Thanks. I saw your graph, and just used my eyes ) I'm not experienced in z-transform to simply match f/2 with z-1 (

  • @AmitSharma-bm2qj
    @AmitSharma-bm2qj Жыл бұрын

    Great 👍

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Thanks, Amit :)

  • @volkanozdemir44
    @volkanozdemir44 Жыл бұрын

    Do you think EAGLE better than KiCAD?

  • @PhilsLab

    @PhilsLab

    Жыл бұрын

    Definitely not. EAGLE is one of the worst bits of PCB design software I've used.

  • @koenvercammen2259
    @koenvercammen2259 Жыл бұрын

    and now in assembly while using the dsp/simd commands of the cortex and storing the IFE_EMA in the TCM ;-)

  • @ALTracer

    @ALTracer

    Жыл бұрын

    What's the point? Modern GCC for armv7e-m can do wonders without the sane programmers having to descend to assembly levels. I'd like to see inline/vectorization though, or at least manual unroll-by-4 + SIMD over floats. And linkerscript allows placing both the struct in DTCM and hot code in ITCM by labeling sections and using attributes. Most of delay in his code comes from sheer halfbuffer length anyways.

  • @TheGmr140
    @TheGmr140 Жыл бұрын

    Cool

  • @tamaseduard5145
    @tamaseduard5145 Жыл бұрын

    👍🙏❤

  • @MEan0207
    @MEan0207 Жыл бұрын

    Thank you