I am given two pieces of codes for Low pass filter and High pass filter, however I am not able to fully understand it and have the following question:
High Pass Filter:
Code:
if isempty(val)
val = single(zeros(numel(input),1));
out = single(zeros(numel(input),1));
end
for i = 1:numel(input)
if reset
out(i) = single(0.0);
val(i) = single(0.0);
else
out(i) = input(i) - (samplePeriod / tc) * val(i);
val(i) = val(i) + out(i);
end
end
output = out(:);
How does it work please? My main question is, how come the val(i) is multiplied before it is updated? Because when I dry run it, every time the for loop is entered, val(i) is a fresh zero value because the i is updated.
Low Pass Filter:
Code:
if isempty(previousOutput)
previousOutput = single(zeros(numel(input),1));
out = single(zeros(numel(input),1));
end
for i = 1:numel(input)
if reset
out(i) = single(0.0);
else
out(i) = ((input(i) - previousOutput(i)) * samplePeriod / tc) + previousOutput(i);
end
end
output = out(:);
previousOutput = out(:);
For this one, I just simply can not derive the above equation from y = 1/(1+Tc/Ts*z-1).