I need implement real time notch filter to 50 HZ powerline noise My MCU is stm32h753bi , how to do that? is it rationale to do that in MCU or I should that on PC side? thank you
-
\$\begingroup\$ You could use an FIR filter. I would think an STM32H7 could do it but I am not sure. But to design the filter, I hope you have the associated MATLAB toolbox or some other software. \$\endgroup\$– DKNguyenJan 13, 2020 at 17:50
-
1\$\begingroup\$ @DKNguyen There are other ways to do FIR filter design than MATLAB - you can do it manually with the window method, or with the assistance of various free libraries. There's also free tools like T-Filter which do everything including the C implementation (see my post below). \$\endgroup\$– SynchrondyneJan 13, 2020 at 18:46
-
\$\begingroup\$ What do you mean by "on the PC side"? Give us the big picture of how you are collecting data and what you are going to do with it. \$\endgroup\$– Elliot AldersonJan 13, 2020 at 21:35
-
\$\begingroup\$ @Elliot Alderson Data(200 Kbytes/second) are collected by MCU and sent to PC via USB and plotted by C# in real time, the question is where is the suitable place to filter the data \$\endgroup\$– MostafaJan 13, 2020 at 22:17
1 Answer
How much attenuation do you need, and what's your sample rate?
If you only need moderate amounts of attenuation, Finite Impulse Response filters are very amenable to implementation on a microcontroller. They aren't necessarily as computationally efficient as Infinite Impulse Response filters for comparable attenuation, but they are unconditionally stable, and are easy to implement in fixed-point arithmetic (IIR filters run into stability problems in fixed-point).
FIR complexity grows with the square of the length of the filter (ie: \$O(n^2)\$), so if you need a lot of attenuation at a high sample rate you probably want to look at something else like a floating point IIR notch filter. You can reduce the complexity of an FIR filter to \$O(n\log n)\$ using an FFT-based algorithm, but that generally only becomes necessary on really long filters (and has real-time implications, since you process the filter in "blocks" at a time rather than continuously).
For computing the filter coefficients, you have a few options. You can do it yourself - the most common method is the window method, which works on prototypes based on a sinc function multiplied by a windowing function. You can also do it with the help of a software library: there are various Python libraries for digital filter design, and you can also use MATLAB if you have the DSP toolbox.
Personally, I quite like T-Filter, which is a little online utility that you can use for FIR filter design: you specify your various passbands, stopbands, and ripple specs, and then let the tool do its job. It even will auto-generate an implementation in C that you can drop right in your firmware project, which is very handy.