
outp1=zeros(1,6); %the output matrix of neural network

weights1=zeros(129,length(outp1)); %weights contains connects between 12 inps to one outp
for i=1:6,
    weights1(5+(i-1)*19:5+19*(i-1)+29,i)=randn(30,1);
end

learningrates1=zeros(129,length(outp1)); %the learning rate for the training
beginrate=0.015;
for i=1:6,
    learningrates1(5+(i-1)*19:5+19*(i-1)+29,i)=beginrate;
end

untrainedweights1=weights1; %saving untrained weights for comparison

%%%%%%%%%%LAYER 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic %timing the procedure
for anneal=1:3, %to slowly decrease the learning rate
for iter=1:30, %iterate multiple times through training set
    for nummat=1:1141, %iterate through training set stimuli
    tempnum=num2str(nummat); 
    filename=['birdmatsmax\birds (',tempnum,')'];
    
    load(filename)
    %soundmatrix contains freq v. time, 129 frequencies (rows)
    %axis properties
        
      threshold=max(mean(soundmatrix))/7; %only train if the timestep is worth training on, meaning if there is information contained in the timestep
        for i=1:size(soundmatrix,2), %iterate through time
            timestep=soundmatrix(:,i);
            if mean(timestep)>threshold,
                %outputing
                outp1=timestep'*weights1;   %get inputs*weights
                %training
                
                timesteprep=timestep(:,ones(6,1));
                outp1rep=outp1(ones(129,1),:);
                
                weightchange1=learningrates1.*(timesteprep.*outp1rep-outp1rep.^2.*weights1); %training procedure, Oja's rule    
                weights1=weights1+weightchange1; %update the weights
            end
        end    
    end
    fprintf(num2str(iter));
end
learningrates1=learningrates1/10; %decrease the learning rate so the weights do not oscillate around a value
end
toc %end timing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%SAVING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save weights1 weights1 
save untrainedweights1 untrainedweights1
