% cms.m
%
% This Matlab script provides a very basic example of the CMS
% algorithm, without the statistical mode modeling component.
%
% by Gary P. Scavone, McGill University, 2006.

fs = 44100;
T = 1 / fs;

f = [220 660 1100 1540 2000 3000];
a = -cos(2*pi*f*T);
k = length(f);

N = 40000;
x = 1;
y = zeros(1, N);

hb = [a' ones(k, 1)];
ha = fliplr(hb);
hz = zeros(1, k);
lasty = hz;

cb = 0.0001 * [1 1];    % one-zero
%cb = 0.0001 * [0.1 0.9]; % one-zero
ca = 1;
pole = -0.999;
ca = [1 -pole];
cb = 1.0 - abs( pole );  % normalized one-pole
cb = 0.5 * cb;
cz = 0;
%cb = cb * 0.10;

for n = 1:N,

  % Computer CMS resonant filters.
  for m = 1:k,
    y(n) = y(n) + lasty(m);
    [lasty(m), hz(m)] = filter( hb(m,:), ha(m,:), x - lasty(m), hz(m) );
  end

  % Do feedback and coupling filtering.
  [x, cz] = filter( cb, ca, y(n), cz );

end

plot(y)

y = 0.95 * y / max(abs(y));
sound(y, fs)

