This course will introduce you to C++ 11 and teach you about syntax fundamentals.
/**
* Zero-Latency Exponential Moving Average.
*/
float zlema(float* vals, int numVals, int currentSample)
{
static float factor = 0;
static int lag = 0;
static float lastema = 0;
float ema;
if(currentSample <= 1)
{
ema = vals[0];
factor = 2.0 / (((float) numVals) + 1.0);
lag = (numVals - 1) / 2;
}
else
{
ema = (factor * ((2.0 * vals[0]) - vals[lag])) +
((1.0 - factor) * lastema);
lastema = ema;
}
return ema;
}
...
static long cs0 = 0;
for(int cnt = NUM_AVERAGE_VALS - 1; cnt > 0; cnt--)
{
m_arrAct0AverageVals[cnt] = m_arrAct0AverageVals[cnt-1];
}
m_arrAct0AverageVals[0] = lPos;
float lPosNew = zlema(m_arrAct0AverageVals, NUM_AVERAGE_VALS, cs0);
cs0++;
...
Would be extremely grateful if someone with a better understanding of the math could please sanity check this for me to see if I've got anything slightly wrong?float y[NUM_VALS -1];
float dy;
float A;
y[0] = 0;
for (int cnt = 1; cnt < NUM_VALS; cnt++){
dy = (u[cnt] - y[cnt-1])/A;
y[cnt] = y[cnt-1] + dy;
}
function NextOutput(float input,float A){
static float dy = 0;
static float y = 0;
dy = (input - y)/A;
y = y + dy;
return y;
}
Copy-of-Low-Pass-Filter.xls
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
The only way (that I know) is to use a transfer function. Since this isn't done physically very easy. So another way is to put a transfer function into a state-space and use the state space and implement.
I don't have code but I did do it in excel. This shows a way to implement it physically. Let me know if it was any help. The equation is in the file.
Low-Pass-Filter.xls