当前位置:网站首页>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;
}
边栏推荐
- Market status and development prospects of the global IOT active infrared sensor industry in 2022
- Unity tool Luban learning notes 1
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- leetcode435. Non overlapping interval
- Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
- leetcode406. Rebuild the queue based on height
- The process of browser accessing the website
- Career planning of counter attacking College Students
- 1106 lowest price in supply chain (25 points)
- Do you know UVs in modeling?
猜你喜欢
MPM model and ab pressure test
论文阅读_中文医疗模型_ eHealth
Basic knowledge of reflection (detailed explanation)
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Promise
Actual combat 8051 drives 8-bit nixie tube
leetcode452. Detonate the balloon with the minimum number of arrows
论文阅读_清华ERNIE
Burp suite plug-in based on actual combat uses tips
随机推荐
Oracle SQL table data loss
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
Preparation for school and professional cognition
Shuttle + alluxio accelerated memory shuffle take-off
On typescript and grammar
Notes | numpy-08 Advanced index
document. The problem of missing parameters of referer is solved
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
Notes | numpy-10 Iterative array
[luatos sensor] 2 air pressure bmp180
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
RT thread flow notes I startup, schedule, thread
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
Market status and development prospect prediction of global neutral silicone sealant industry in 2022
leetcode860. Lemonade change
50 practical applications of R language (36) - data visualization from basic to advanced
Learn to use the idea breakpoint debugging tool
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Mobile terminal - uniapp development record (public request encapsulation)
Notes | numpy-11 Array operation