Embedded commonly used algorithms - simple filtering

Simple filtering is a digital signal processing technique used to preprocess digital signals. Its main purpose is to reduce noise in the signal and increase the signal-to-noise ratio of the signal. There are many ways of simple filtering, and I am just introducing one of them here.

There are two main types of simple filtering, namely linear filtering and nonlinear filtering. Linear filtering is based on linear operations, mainly including averaging filtering and convolution filtering. Averaging filtering is to reduce noise by dividing a signal into several parts on average. Convolution filtering is to reduce noise by convolving the signal with a convolution kernel.

Nonlinear filtering is based on nonlinear operations, mainly including median filtering and filter filtering. Median filtering is to reduce noise by sorting a segment of the signal and taking the median value. Filter filtering, on the other hand, reduces noise by convolving the signal with a filter.

/**
 * @brief			简单滤波
 * @param[out]		lpf : 滤波结构数据指针
 * @param[in]		rawData : 原始数据
 */
void SimpleFilter(LpfSimple_t* lpf, int16_t rawData)
{
    
    
	int16_t FilterBuffSort[SimpleFilterDepth] = {
    
    0};	// 暂存排序值

    lpf->OriginData = rawData;
    // 滑动递推更新旧值
    for (uint16_t i = 1; i < SimpleFilterDepth; i ++ ) {
    
    
    	lpf->FilterBuff[i] = lpf->FilterBuff[i - 1];
    }
	// 简单强制消抖 加权存入新值
    if ((fabs(lpf->OriginData - lpf->FilterBuff[0])) < 2.0f) {
    
    
    	lpf->FilterBuff[0] = lpf->OriginData * 0.70f + lpf->FilterBuff[0] * 0.30f;
    } else {
    
    
    	lpf->FilterBuff[0] = lpf->OriginData;
    }
	// 存储待排序值
	memcpy(FilterBuffSort, lpf->FilterBuff, sizeof(lpf->FilterBuff));
	// 升序排序
	Quick_Sort(FilterBuffSort, 0, sizeof(lpf->FilterBuff) - 1, 0);
	// 中值滤波
	lpf->FilterBuff[0] = 0;    // 先清零
	for (uint16_t i = 2; i < (SimpleFilterDepth - 2); i ++ ) {
    
    
		lpf->FilterBuff[0] += FilterBuffSort[i];
	}
	lpf->FilterBuff[0] = lpf->FilterBuff[0] / (SimpleFilterDepth - 4);	// 去除四个极值再计算均值
}

This is an easy-to-understand, simple-to-implement signal preprocessing technique that is primarily used to remove noise and increase the signal-to-noise ratio. However, in the case of dealing with complex noise and requiring high signal time-domain performance, the effect of simple filtering will become inferior to other complex filtering algorithms, which need to be weighed according to specific application scenarios.

Among them, the median filter is to reduce noise by sorting the signal and taking the median value. Filter filtering, on the other hand, reduces noise by convolving the signal with a filter.

The content of the structure of the .h file corresponding to the above code is as follows

#define SimpleFilterDepth 10
typedef struct {
    
    
	int16_t OriginData;
	int16_t FilterBuff[SimpleFilterDepth];
} LpfSimple_t;

The advantage of simple filtering is that it is easy to understand and simple to implement. But its disadvantage is that it is less effective in eliminating complex noise. And it can lead to poor time-domain performance of the signal.

In practical applications, simple filtering algorithms may not fully meet the requirements, and other filtering algorithms need to be combined to improve the effect of signal processing. For example, in signal processing, you can first use simple filtering algorithms for noise elimination, and then use other complex filtering algorithms for signal enhancement.

Filtering is an important part of signal processing. It can effectively reduce noise and improve signal-to-noise ratio. However, when dealing with complex noise and requiring high signal time-domain performance, it is necessary to combine other filtering algorithms to obtain better results. processing effect.

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128701253