% helmholtz dissonance

fo = 300;
fmax = 2000;
m = 10;
h = fo * (1:m); 
h = h(find(h<fmax));  % harmonics of original fo

ints = power(2,(1:12)/12);  % equal-tempered
ints = [16/15 9/8 6/5 5/4 4/3  64/45  3/2 8/5  5/3 7/4 15/8 2];
sc = [];

figure;
for a=1:numel(ints)
  int = ints(a);
  %int = 3/2;     % interval ratio
  
  f = fo * int;
  fh = f * (1:m);
  fh = fh(find(fh<fmax));  % harmonics of new f
  
  subplot(3,4,a); vline(h, 'k-'); vline(fh, 'r');axis([0 fmax 0 2]); hold on;
  title(sprintf('interval: %g', int));
  
  t = zeros(1,2);         % # of dissonant partials of f (badly, partially)
  for j=1:numel(fh)      % loop over harms of f
    i = find(abs((h-fh(j))/fh(j))<0.1 & abs(h-fh(j))>15); % badly diss
    k = find(abs((h-fh(j))/fh(j))<0.15 & abs((h-fh(j))/fh(j))>0.1); % bit diss
    if ~isempty(i), plot([fh(j) h(i)], [1.1 1.1*ones(size(i))], 'b-*'); end
    if ~isempty(k), plot([fh(j) h(k)], [1.2 1.2*ones(size(k))], 'g-o'); end
    t(1) = t(1)+numel(i);
    t(2) = t(2)+numel(k);
  end
  disp(sprintf('Int: %g, # of dissonances = %d (badly), %d (a bit)', ...
               int, t(1), t(2)));
  sc(a) = t(1) + 0.5*t(2);

end

print -depsc2 diss_partials.eps

figure;plot(ints, sc, '+-');            % scores
print -depsc2 diss_scores.eps

% end

