%processes samples and computes a norm between the output and the original
%to find a good set of parameters
%calls filtersinesamples to process the samples
%uses sparsespec to analyse the original and processed samples
%computes a 1-norm between the original and processed samples

%in the future we may wish to change this so that instead of processing all
%the samples first, which uses a huge amount of memory, we should do 1 at a
%time, compute the norm, and discard it.  The problem is that we have to
%first compute the minimum length.
%math note:  the length of the output is equal to-
% winlen/overlap*floor(overlap*len/winlen)

overlap = 2;

%first define our audio samples:
f1 = 880;
s = 1.059463;
f2 = s^2 * 880;%988
fs = 44100;
time = 2;
pers = 2;
apers = 3;
c = sinesamples(fs, f1, f2, time, pers, apers);

%if you would like to use a subset of c, define it here.  Keep c as a cell.
c = c(4,1);
%length(c{1,1})

%now define our parameters for processing:

window = [234]; %must be even so that overlap will divide into window
%separation = [.2 .4 .6 .8  1];
separation = [.8];
%order = [2 4 6];
order = [4];
%ftype = {'b', 'c1', 'c3'};
ftype = {'c1'};
%f = {logspace(log10(860), log10(1000), 10), logspace(log10(860),
%log10(1000), 20), logspace(log10(860), log10(1000), 30),logspace(log10(860), log10(1000), 40), logspace(log10(860), log10(1000), 50) };\
%f = {logspace(log10(860), log10(1000), 10), logspace(log10(860), log10(1000), 20), logspace(log10(860), log10(1000), 30), logspace(log10(860), log10(1000), 40)};
freqnums = 2:51;
f = cell(1, length(freqnums));
for i = 1:length(f)
    f{i} = logspace(log10(860), log10(1000), freqnums(i));
end

distortc = {[]};

%process the input with our parameters:
%This has been removed.  We are going to do one at a time and use the same
%memory to do each so that we dont crash my computer

%To compute the norm, we will compute
%sparse spectograms of each original and processed sample.  The differences
%at each frequency and point in time will be computed, and all will be
%summed.

%sparse spectograms do not compute an fft at every possible frame.  Instead
%of shifting by 1 sample, they shift by a much larger number.  There may
%still be some overlap, but need not be.  They use much less memory and
%when plotted reveal about the same information.

numffts = 25;
spacing = 1;
winlen = 512;

Dc = size(c);
s1 = cell(Dc);
c2 = cell(Dc);

%lets find what the minimum length of each sample is right now is right now:
minlength = zeros(size(c));
for i = 1:Dc(1)
    for j = 1:Dc(2)
        len = length(c{i,j});
        minlength(i,j) = len;
        for k = 1:length(window)
            newl = window(k)/2*floor(2*len/window(k));
            if newl < minlength(i,j)
                minlength(i,j) = newl;
            end
        end
        %now truncate the original
        z = c{i,j};
        z = z(1:minlength(i,j));
        c2{i,j} = z;
        %compute sparsespec of original samples after they are truncated
        s1{i,j} = sparsespec(c2{i,j}, numffts, spacing, winlen, 'n');
    end
end

clear n;
[n(1) n(2)] = size(c);
n(3) = length(window);
n(4) = length(separation);
n(5) = length(order);
n(6) = length(ftype);
n(7) = length(f);
%n(8) = length(distortc);

norms = zeros(n);

perms = prod(n); %total number of times we will run distort
current = 1;

for i = 1:n(1)
    for j = 1:n(2)
        for k = 1:n(3)
            for l = 1:n(4)
                for m = 1:n(5)
                   for o = 1:n(6)
                       for p = 1:n(7)
%                           for q = 1:n(8)
                               current
                               current = current + 1;
                               perms
   currentsample = distort(c{i,j}, trianglewindow(window(k)), overlap, f{p}, separation(l),...
       order(m), distortc{1}, fs, ftype{o}, @cleansynth);
                               currentsample = currentsample(1:minlength(i,j));
                               s2 = sparsespec(currentsample, numffts, spacing, winlen, 'n');
                               diff = abs(s2 - s1{i,j});
                               norms(i,j,k,l,m,o,p) = sum(sum(diff));
%                           end
                       end
                   end
                end
            end
        end
    end
end

%norms now contains all the norms