Skip to content

Instantly share code, notes, and snippets.

@tfcollins
Created July 27, 2015 12:51
Show Gist options
  • Save tfcollins/c11e2d859abf4e4befdb to your computer and use it in GitHub Desktop.
Save tfcollins/c11e2d859abf4e4befdb to your computer and use it in GitHub Desktop.
MATLAB CIC Moving Average Filter
function output = CICFilter(input)
% Asserts
assert(isa(input, 'double') && ~isreal(input) && all(size(input) == [80*100,1]))
persistent lastInputs lastOutput
numTaps = 16;
% Initialize History
if isempty(lastInputs) || isempty(lastOutput)
lastInputs = complex(zeros(numTaps,1));
lastOutput = complex(0);
end
output = coder.nullcopy(complex(zeros(size(input))));
for x = 1:length(input)
%output[n] = output[n-1] + input[n] - input[n-taps]
if (x-numTaps)>0
output(x) = output(x-1) + input(x) - input(x-numTaps);
elseif (x==1)
output(x) = lastOutput + input(x) - lastInputs(x);
else
output(x) = output(x-1) + input(x) - lastInputs(x);
end
end
% Save history for next call
lastOutput = output(end);
lastInputs(1:numTaps) = input(end-numTaps+1:end);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment