I am working on processing of ECG signals.
In order to process the received data, we need to filter it.
Several papers suggest filtering foemulas for high-pass and low-pass filters.
But I don't know how to write those formulas in AI2 blocks.
For example I quote part of one paper:
Could anyone please help me writing the required blocks?
Taifun's Math Extension to parse a mathematical expression.
to solve the equation is problematical. What value are you solving for? Do you know the value of all the variables?
Someone would need to know what you are filtering and where the data originates. Those equations in themselves are useless out of context.
Another possibility is to use javascript. This article Signal.js shows how to produce a lowpass filter using javascript. App Inventor can use javascript modules. The article does not explain how to use the javascript with AI.
Here's a low pass filter in blocks, courtesy of Wikipedia
// Return RC low-pass filter output samples, given input samples,
// time interval dt, and time constant RC
function lowpass(real[0..n] x, real dt, real RC)
var real[0..n] y
var real α := dt / (RC + dt)
y[0] := α * x[0]
for i from 1 to n
y[i] := α * x[i] + (1-α) * y[i-1]
return y
The loop that calculates each of the n outputs can be refactored into the equivalent:
for i from 1 to n
y[i] := y[i-1] + α * (x[i] - y[i-1])
and here are the blocks, assuming a non-empty list of x values, and dt and RC as described at that article.
// Return RC high-pass filter output samples, given input samples,
// time interval dt, and time constant RC
function highpass(real[0..n] x, real dt, real RC)
var real[0..n] y
var real α := RC / (RC + dt)
y[0] := x[0]
for i from 1 to n
y[i] := α × y[i−1] + α × (x[i] − x[i−1])
return y
The loop which calculates each of the {\displaystyle n}n outputs can be refactored into the equivalent:
for i from 1 to n
y[i] := α × (y[i−1] + x[i] − x[i−1])
Global variables are like medical catheters.
Local variables are like internal organs, you don't want them exposed to the elements, otherwise you are living a horror movie.
Sometimes you really need a global variable, for example when a Clock Timer has to pass data to another part of your code.
But there is a cost to them, if a later you or someone else goes to change the program and forgets all the connections between program parts that pass through the global variable's contents.
I try to avoid them, or to replace them with getter/setter functions if I can.
The programming terms for these ideas are