当前位置:网站首页>C language program ideas and several commonly used filters
C language program ideas and several commonly used filters
2022-07-03 05:05:00 【DoTheTran】
1-17 Fixed the error of sequential execution program and moving average filtering
C Language
Structure
Enumerating body
The header file
Preprocessing instruction
A function pointer
C Chinese language network http://c.biancheng.net/view/2031.html
Code style.
engineering
Indent
notes
vscode
signal communication uart iic spi
Hardware protocol , Simulation protocol
Writing data , Write orders , Reading data
Serial transmission , receive , interrupt
Non blocking delay
int timecnt=-1;// Current count ( count down )
......
{
if(something && timecnt==-1)
{
timecnt=1000;//1000ms count down
}
if(timecnt==0)// The countdown is over
{
timecnt=-1;// Timer reset
somefun();// The program executed after the countdown
}
}
......
timer_irq_handle()// Timer interrupt function , Average period 1ms
{
if(timecnt>0)
timecnt--;
......
}
Sequential execution program ideas
int process[5];//5 Step
......
if(something_start && !process[1])// Start step
{
if(!process[0])
{
initfun0();// Initializer , Omission
process[0]=1;// Set the flag of this step
}
somefun0();// Continue to perform procedures
}
if(something1 && process[0] && !process[2])// Step 1
{
if(!process[1])
{
initfun1();// Initializer
process[1]=1;// Set the flag of this step
}
somefun1();// Continue to perform procedures
}
if(something2 && process[1] && !process[3])// Step 2
{
if(!process[2])
{
initfun2();// Initializer
process[2]=1;
}
somefun2();// Continue to perform procedures
}
......
if(something_end && process[x])// End step ,process[x] It's the last step
{
memset(process,0,sizeof(process));// Clear all flag bits
}
wave filtering
Software filtering plays an important role in embedded data acquisition and processing .
The following software filtering methods refer to “ Craftsman's treasure chest ”, The following is the revision and arrangement according to your own understanding , Thanks to the original author .
The source of the original article is unknown , Baidu Ten common filtering methods
1. Arithmetic average filtering
A、 Method :
Continuous access N Sample values are arithmetic averaged
N When it's worth more : High signal smoothness , But the sensitivity is low
N It's worth less : The signal smoothness is low , But the sensitivity is high
N Value selection : General flow ,N=12; pressure :N=4
B、 advantage :
It is suitable for filtering signals with random interference
The characteristic of such a signal is that it has an average value , The signal fluctuates up and down near a certain range of values
C、 shortcoming :
It is not suitable for real-time control with slow measurement speed or fast data calculation speed
float filter(int N)//N Is the number of samples
{
float value;
for(int i=0;i<N;i++)// Here is continuous sampling , Or change to timing sampling
{
value += get_value();
}
return value/N;
}
2. Limiting filter
A、 Method :
Judge by experience , Determine the maximum allowable deviation between two samples ( Set to A),
Judge every time a new value is detected :
If the difference between the current value and the last value <=A, Then this value is valid ;
If the difference between the current value and the last value >A, Then this value is invalid , Abandon this value , Replace the current value with the last value
B、 advantage :
It can effectively overcome the impulse interference caused by accidental factors
C、 shortcoming
Unable to suppress periodic interference
Poor smoothness
#define ABS(x) (((x) > 0) ? (x) : (-(x)))// Absolute value macro function
float filter(float A) //A Is the allowable deviation value
{
static float new_value,last_value; // Static variables or global variables
last_value = new_value;
new_value = get_value();
if ( ABS(new_value - last_value) > A )
return last_value;
return new_value;
}
in addition , There is another kind of amplitude limiting filtering which is commonly used , If more than ( Next ) Limit , Then the current value = On ( Next ) Limit
void filter(float *value, float min, float max)
{
if(*value >max) *value = max;
else if(*value < min) *value = min;
}
// The following is when calling
value = get_value();
filter(&value,min,max);
Limiting filter is generally used in combination with other filters , Such as amplitude limiting average filtering
3. First order lag filtering ( Lowpass )
A、 Method :
take a=0~1
The result of this filtering is = a* This sample value +(1-a)* Last filter result
B、 advantage :
It has a good inhibiting effect on periodic interference
It is suitable for the occasion with high fluctuation frequency
C、 shortcoming :
Phase lag , Low sensitivity
The degree of lag depends on a Value size
Can not eliminate the filter frequency higher than the sampling frequency 1/2 The interference signal of
float filter( float a)
{
static float new_value,last_value; // Static variables or global variables
last_value = new_value;
new_value = get_value();
new_value = a * (new_value - last_value) + last_value;
return new_value;
}
4. Median filtering
A、 Method :
Continuous sampling N Time (N Take an odd number )
hold N The subsamples are sorted by size
Take the intermediate value as the current effective value
B、 advantage :
It can effectively overcome the fluctuation interference caused by accidental factors
For temperature 、 The measured parameters with slow change of liquid level have good filtering effect
C、 shortcoming :
For flow 、 Fast changing parameters such as speed should not be
#define N 100
float value_buf[N];
int filter()
{
float sum=0,temp;
for(int count=0;count<N;count++)// Collect data
{
value_buf[count] = get_value();
delay();
}
for (int j=0;j<N-1;j++)// Bubble sort
{
for (int i=0;i<N-j;i++)
{
if(value_buf[i] > value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
return value_buf[(N-1)/2];
// Median average filtering
// for(int count = 1; count < N-1; count++)
// sum += value[count];
// return (int)(sum/(N-2));
}
5. Moving average filtering
A、 Method :
Take continuous N Sample values as a queue
The length of the queue is fixed to N
Every time a new data is sampled, it is put at the end of the queue , And throw away the data of the original team leader .( First in, first out principle )
Put... In the queue N Arithmetic average of data , New filtering results can be obtained
N Value selection : Traffic ,N=12; pressure :N=4; liquid surface ,N=412; temperature ,N=14
B、 advantage :
It has a good inhibiting effect on periodic interference , High smoothness
For systems with high frequency oscillations
C、 shortcoming :
Low sensitivity
The inhibition of the occasional impulsive interference is poor
It is not easy to eliminate the sampling value deviation caused by pulse interference
It is not suitable for the occasion of serious pulse interference
#define N 12
int value_buf[N];
float filter()
{
float sum=0;
for(int i=N-1; i>=1; i--)
{
value_buf[i] = value_buf[i-1];// Data backward shift , The high order is replaced
}
value_buf[0] = get_value();// The new value is given to 0 Elements
for(int i=0;i<N-1;i++)
{
sum += value_buf[i]// Ergodic summation
}
return (float)(sum/N);
}
6. Complementary filtering
A、 Method :
The collected values obtained by two ways are weighted and fused
The result of this filtering is = a * Method A Collected value of + (1-a) * Method B Collected value of
B、 advantage
The result of merging the two schemes , Learn from others' strong points and close the gap
C、 shortcoming :
Low sensitivity , Not suitable for data with high frequency
float filter(float a)
{
float value_a, value_b, value;
value_a = get_a();
value_b = get_b();
value = a * (value_a -value_b) + value_b;
return value;
}
边栏推荐
- Shell script Basics - basic grammar knowledge
- Problems encountered in fuzzy query of SQL statements
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- Distinguish between releases and snapshots in nexus private library
- JQ style, element operation, effect, filtering method and transformation, event object
- Go language interface learning notes Continued
- [SQL injection] joint query (the simplest injection method)
- 【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
- Flutter monitors volume to realize waveform visualization of audio
猜你喜欢

The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
![[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)](/img/af/8dfa783c87363a9d75c52e7680d508.jpg)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)

SSM framework integration
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached

Compile and decompile GCC common instructions

Basic use of Metasploit penetration testing framework

leetcode435. Non overlapping interval

leetcode452. Detonate the balloon with the minimum number of arrows

Class loading mechanism (detailed explanation of the whole process)
随机推荐
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
112 stucked keyboard (20 points)
Notes | numpy-10 Iterative array
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Learning record of arouter principle
Thesis reading_ Tsinghua Ernie
Wechat applet distance and map
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
[research materials] 2021 China's game industry brand report - Download attached
Thesis reading_ Chinese NLP_ ELECTRA
Notes | numpy-09 Broadcast
Flutter monitors volume to realize waveform visualization of audio
Silent authorization login and registration of wechat applet
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Sprintf formatter abnormal exit problem
RT thread flow notes I startup, schedule, thread
MediaTek 2023 IC written examination approved in advance (topic)
Self introduction and objectives
Thesis reading_ ICD code_ MSMN
Problems encountered in fuzzy query of SQL statements