• ### An easy Adaptative Filter for our projects

Well when you are reading a sensor, but that sensor, has some kind of craziness, We can built a Adaptative Filter like the next program. The proyect and the source code are on that, but you remember this equiation:

The famous linear regression equation that is, i know so beautiful, because it's amazing, but for our proposes are good that you know it. I let the link if you want to play with that equitation and understand the principal points.

So taking this equation we can see some topics of machine learning that says:

The algorithm tries to understand the data like a linear regression something the data, and learning of the sensor, for this example, are trying to follow the response of an AC signal, we can make the prediction with another consideration, using the formula with Noise, and training the system to reach that signal, that can be mean, Learning Supervised, or only letting to discover the relationship, Unsupervised. Like the another code

```#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//What number of elements do you want
#define BackFilter 10
#define LearningParameter 0.1
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class FiltroInteligente{
//We have three variables with all the information
float Vin[BackFilter]; //Input values
float Kca[BackFilter]; //K parameters
float Pro[BackFilter]; //=inputs * K parameters
//Method to clean the vectors
void ClearData(){
for(int i=0; i<BackFilter;i++)	{
Vin[i]=0;
Pro[i]=0;
}
}
//Limpieza del vector de datos
void ClearProd(){
for(int i=0; i<BackFilter;i++)	{
Pro[i]=0;
}
}
//Funcion para predecir los datos
public:
float Result=0;
float Wantted=0;
float ErrorN=0;
float ErrorS=0;
//Randomize K
void RandomizeK(){
for(int i=0; i<BackFilter;i++)	{
Kca[i]=rand()%BackFilter;
}
}
// Make the operacion of Result=sum(Vin*Kpar) Remeber that Vin and Kpar are vectors
void ProductoYPrecedir(){
Result=0;
for(int i=0; i<BackFilter;i++)	{
Pro[i]=Vin[i]*Kca[i];
Result=Pro[i]+Result;
}
}
//Initialize the class
void Initialize(){
ClearData();
RandomizeK();
Vin[BackFilter-1]=1; //La ultima posicion es el desface llamada Beta
}
//Print my data of learning
void PrintMySavedData(){
printf("Saved:\t");
for(int i=0; i<BackFilter;i++)	{
printf("%3.3f\t",Vin[i]);
}
printf("\nK:\t");
for(int i=0; i<BackFilter;i++)	{
printf("%3.3f\t",Kca[i]);
}
}
//Insert an X value, remember the last position are 1 because
/*
y=mx+b
Where K are a vector that represents m
And Vin are a vector that represents x
But the b can be absorbed with
[x1,x2,x3,...] point [k1,k2,k3] +b => [x1,x2,x3,..,1] point [k1,k2,k3,b]
*/
for(int i=0; i<(BackFilter-1);i++)	{
Vin[i]=Vin[i+1];
}
}
//This traing the model One Time
/*Using
h(x)=mx+b
Where
e=1/2*(y-h(x))^2
de/dk=(y-h(x))*(-Vin) //Remeber that y-h(x) is a number that will be multiplied by a vector
//(y-h(x))*(Vin) => This will be a vector
k(n)=k(n-1)+de/dk =>Remeber that k is a vector and de/dk also
*/
void TraingOnce(float WanttedData){
ProductoYPrecedir();
ErrorN=WanttedData-Result;
ErrorS=1/2*(ErrorN)*ErrorN;
for(int i=0; i<BackFilter;i++)	{
Kca[i]=Kca[i]+ErrorN*Vin[i]*LearningParameter;
}
}
};
//Signal of testing
float Signal2Analize(float Time){
return sin(Time*3.14/180);
}
float SignalWithNoise(float Time){
return sin(Time*3.14/180)+0.2+sin(20*Time*3.14/180);
}
int main(int argc, char** argv) {
FiltroInteligente MyFilter;
float TimeA=0;
float Signal=0;
MyFilter.Initialize();
bool OnlyOne=false;
int ConterMaxTraining=20;
srand (time(NULL));
//El ciclo es infinito
while(true){
Signal=Signal2Analize(TimeA);
MyFilter.ProductoYPrecedir();
printf("Prediction\n");
printf("Kn: %f \tSk: %f \tPredicted:%f",TimeA,Signal,MyFilter.Result);
...```