clear
clc
[y, Fs] = wavread('RFIY.wav');      % y is sound data, Fs is sample frequency.
y = y(1:length(y),:);
t = (1:length(y))/Fs;         % time
duration = length(y)/Fs
channel1 = y(:,1);
%find max in intervals of 10 seconds
length(channel1)
tensecsample = Fs*10;
% find the local maximum every 250ms
rad = Fs*0.25
j = 1;
localmaxindex_fin = [];
count = 0;
while j < length(channel1)
    startsample = j;
    endsample = min(length(channel1),j + tensecsample);
	%get the absolute max
    [maxval maxind] = max(abs(channel1(startsample:endsample)));
    localmaxindex = [];
    tempmax = 0;
    tempind = 0;
    %detecting the local maximums
    for i = startsample:endsample
         if tempind == 0
             tempind = i;
             tempmax = abs(channel1(i));
         else
             if (abs(channel1(i))>tempmax)
				%local maximum so far
                  tempind = i;
                  tempmax = abs(channel1(i));
             end    
			 
             if i - tempind > rad 
                 localmaxindex = [localmaxindex tempind]; 
                  tempind = 0;
             end
         end
		 %the last local maximum
         if (i == length(channel1))
             localmaxindex = [localmaxindex tempind];
         end
         
    end
     length(localmaxindex);
    for i = 1:length(localmaxindex)
     if (abs(channel1(localmaxindex(i)))>0.6*maxval)
	 %checking the threshold
         localmaxindex_fin = [localmaxindex_fin localmaxindex(i)];
     end
    end
    
    j = j + tensecsample;
end


threesecsample = Fs*3;
mean = [];
std = [];
k = 1;
%process the 3 second periods for mean and and std
while k < length(channel1)
    startsample2 = k;
    endsample2 = min(length(channel1),k + threesecsample);
    startsample2;
    endsample2;
	% save 3 second interval mean, standard deviation 
    tempmean = mean2(abs(channel1(startsample2:1:endsample2)));
    tempvar = std2(abs(channel1(startsample2:1:endsample2)));
    mean = [mean tempmean];
    std = [std tempvar];
    k = k + threesecsample;
end
mean;
std;

meanmax = max(mean);
stdmax = max(std);
%writing to file the mean,std, time, ects
f = fopen('songs.h','w');
fprintf(f,'/*This file contains the information needed to let\n the microcontroller decide which effect to generate\n*/\n');
fprintf(f,'\n');
fprintf(f,'\n');
fprintf(f,'//duration is in units of 100ms\n');
fprintf(f,'int duration = %i;\n',floor(duration*10));
fprintf(f,'int length_mstd = %i;\n',length(mean));
fprintf(f,'int length_pulse = %i;\n',length(localmaxindex_fin));
fprintf(f,'double mstd[%i][2] = {',length(mean));
for i = 1:length(mean)
    if i == 1
        fprintf(f,'{%.2f,%.2f}',mean(i)/meanmax,std(i)/stdmax);
    elseif i == length(mean)
        fprintf(f,'};\n'); 
    else 
        fprintf(f,',\n{%.2f,%.2f}',mean(i)/meanmax,std(i)/stdmax);         
    end
end

fprintf(f,'\n');
fprintf(f,'\n');
fprintf(f,'double pulse[%i][2] = {',length(localmaxindex_fin));
% time granularity for local max is 100ms so put all the entries in
% local_max_fin in units of 100ms
%normalize the intensities by the max in the whole song
intmax = max(channel1)
for i = 1:length(localmaxindex_fin)
    if i == 1
        fprintf(f,'{%i,%.2f}',floor(localmaxindex_fin(i)/(Fs/10)),abs(channel1(localmaxindex_fin(i))/intmax));
    elseif i == length(localmaxindex_fin)
        fprintf(f,'};\n'); 
    else 
        fprintf(f,',\n{%i,%.2f}',floor(localmaxindex_fin(i)/(Fs/10)),abs(channel1(localmaxindex_fin(i))/intmax));         
    end
end

fclose(f);

 