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

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ค. 2024
  • 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/yt/philslab
    PCBs manufactured and assembled by PCBWay: www.pcbway.com
    Patreon: / phils94
    Mixed-signal hardware design course: phils-lab-shop.fedevel.education
    Advanced Hardware Design Course Survey
    forms.gle/X4jwvtZeJ1jTXh7r9
    [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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 87

  • @naturevolt
    @naturevolt ปีที่แล้ว +19

    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  ปีที่แล้ว +3

      Thanks, David - that's a great point!

    • @1over137
      @1over137 ปีที่แล้ว +1

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

  • @seany9519
    @seany9519 ปีที่แล้ว +2

    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

  • @robdavis3220
    @robdavis3220 ปีที่แล้ว +2

    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.

  • @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!

  • @pietrogagliano4484
    @pietrogagliano4484 ปีที่แล้ว +11

    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  ปีที่แล้ว +4

      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 ปีที่แล้ว +2

      @@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 ปีที่แล้ว +3

      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 ปีที่แล้ว +2

    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  ปีที่แล้ว +1

      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.

  • @myetis1990
    @myetis1990 ปีที่แล้ว +3

    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 :)

  • @LLiiaamm_
    @LLiiaamm_ ปีที่แล้ว +2

    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  ปีที่แล้ว +6

      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 :)

  • @1over137
    @1over137 ปีที่แล้ว +1

    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.

  • @shaungovender7805
    @shaungovender7805 ปีที่แล้ว +1

    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...

  • @N4HAY
    @N4HAY ปีที่แล้ว

    Very professional and clear. Thanks.

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว

      Thank you, Richard!

  • @HOKKEY
    @HOKKEY ปีที่แล้ว +2

    Thank You for Sharing !!

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว

      Thanks for watching!

  • @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).

  • @7alfatech860
    @7alfatech860 ปีที่แล้ว

    Great content as always!

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว +1

      Thank you!

  • @thelastofthemartians
    @thelastofthemartians ปีที่แล้ว +2

    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 ปีที่แล้ว +1

    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.

  • @---658
    @---658 ปีที่แล้ว

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

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว

      Thanks for watching!

  • @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.

  • @yanivginlevy
    @yanivginlevy 6 วันที่ผ่านมา

    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

  • @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

  • @adrianovianawerneck472
    @adrianovianawerneck472 ปีที่แล้ว

    I Phil good with these videos.

  • @sumedhburbure4173
    @sumedhburbure4173 ปีที่แล้ว

    Great video!

  • @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!

  • @thetastefultoastie6077
    @thetastefultoastie6077 ปีที่แล้ว +1

    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  ปีที่แล้ว +1

      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..

  • @VladimirDjokic
    @VladimirDjokic ปีที่แล้ว +1

    Cool, I ❤ your videos

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว

      Thank you, Vladimir!

  • @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  ปีที่แล้ว +1

      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!!

  • @dmitry.shpakov
    @dmitry.shpakov 11 หลายเดือนก่อน

    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.

  • @Ncky
    @Ncky ปีที่แล้ว +1

    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.

  • @AmitSharma-bm2qj
    @AmitSharma-bm2qj ปีที่แล้ว

    Great 👍

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว +1

      Thanks, Amit :)

  • @matthewprestine1974
    @matthewprestine1974 ปีที่แล้ว +1

    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  ปีที่แล้ว +1

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

  • @Quantum_Dots
    @Quantum_Dots ปีที่แล้ว +1

    Awesome

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว

      Thank you!

  • @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

  • @Heisenberg2097
    @Heisenberg2097 ปีที่แล้ว

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

  • @TheGmr140
    @TheGmr140 ปีที่แล้ว

    Cool

  • @MrGraf1992
    @MrGraf1992 2 หลายเดือนก่อน

    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.

  • @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

  • @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.

  • @marek_ryn
    @marek_ryn ปีที่แล้ว +1

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

    • @PhilsLab
      @PhilsLab  ปีที่แล้ว +1

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

  • @MEan0207
    @MEan0207 ปีที่แล้ว

    Thank you

  • @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  ปีที่แล้ว +1

      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.

  • @guruG509
    @guruG509 ปีที่แล้ว

    Pls make a video on quaternions

  • @hessel9390
    @hessel9390 6 หลายเดือนก่อน

    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?

  • @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.

  • @Xer777Xes
    @Xer777Xes ปีที่แล้ว

    Could you make a video with the goertzel algorithm?

  • @robertpeters9438
    @robertpeters9438 5 หลายเดือนก่อน

    USE BROADER GRAPH LINES FOR US TO SEE THE COLORS.

  • @tamaseduard5145
    @tamaseduard5145 ปีที่แล้ว +1

    👍🙏❤

  • @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 (

  • @theverner
    @theverner 2 หลายเดือนก่อน

    But how to avoid phase shift in applocation?

  • @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 ปีที่แล้ว +1

      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.

  • @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.

  • @UncleWalter1
    @UncleWalter1 ปีที่แล้ว +1

    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 17 วันที่ผ่านมา

      '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.