
clear

k = 1:10;
n = 32;
t = 1:(2^12 - 2*n);  %time scale
rmse1=zeros(1,20);
rmse2=zeros(1,20);
rmse3=zeros(1,20);
for i = 5:5:100;
    j = i/5;
    rmsenum1=0;
    rmsenum2=0;
    rmsenum3=0;
    
    xoctave = cos((1:2^(12))*2*pi*64/2^13) + cos((1:2^(12))*2*pi*128/2^13); %singal for frequencies of f*m
    xoctave = 0.3*xoctave/max(abs(xoctave));
    
    xthird = cos((1:2^(12))*2*pi*5*64/2^13) + cos((1:2^(12))*2*pi*4*64/2^13);
    xthird = 0.3*xthird/max(abs(xthird));
    
    xschism = cos((1:2^(12))*2*pi*6.5*64/2^13) + cos((1:2^(12))*2*pi*8*64/2^13);
    xschism = 0.3*xschism/max(abs(xschism));
    
    xoctave2 = simplecodec4(xoctave,i);
    xthird2  = simplecodec4(xthird,i) ;
    xschism2 = simplecodec4(xschism,i);
    
    xoctave = xoctave(33:length(xoctave2)+32)'; %trimmed down signal
    xthird = xthird(33:length(xthird2)+32)'; %trimmed down signal
    xschism = xschism(33:length(xschism2)+32)'; %trimmed down signal

    error1 = (xoctave - xoctave2).^2; %difference squred
    error2 = (xthird - xthird2).^2; %difference squred
    error3 = (xschism - xschism2).^2; %difference squred

    for k = 1:length(xoctave2)
        rmsenum1 = rmsenum1+error1(k); %numeric rmse
        rmsenum2 = rmsenum2+error2(k); %numeric rmse
        rmsenum3 = rmsenum3+error3(k); %numeric rmse
    end
    rmse1(j) = sqrt(rmsenum1/length(xoctave2)); %rmse for each frequency
    rmse2(j) = sqrt(rmsenum2/length(xthird2)); %rmse for each frequency
    rmse3(j) = sqrt(rmsenum3/length(xschism2)); %rmse for each frequency
    

    if i == 5
        figure
        plot(t(1:100),xoctave(1:100),'-r',t(1:100),xoctave2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Octave bits = ',num2str(i)])
        
        figure
        plot(t(1:100),xthird(1:100),'-r',t(1:100),xthird2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Third bits = ',num2str(i)])
        
        figure
        plot(t(1:100),xschism(1:100),'-r',t(1:100),xschism2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Schism bits = ',num2str(i)])
    end
    
    if i == 50
        figure
        plot(t(1:100),xoctave(1:100),'-r',t(1:100),xoctave2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Octave bits = ',num2str(i)])
        
        figure
        plot(t(1:100),xthird(1:100),'-r',t(1:100),xthird2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Third bits = ',num2str(i)])
        
        figure
        plot(t(1:100),xschism(1:100),'-r',t(1:100),xschism2(1:100),'-b') %plots a small section of the signal
        legend('starting wave','new wave')
        title(['Schism bits = ',num2str(i)])
    end
end
rmse1
rmse2
rmse3

%The RMSE is inversely related to the bumber of bits used. If i used 100
%bits on an octive the RMSE is 6.49*10^-16. If i used 25 bits i get that
%the RMSE is 3.52*10^-9. I've now tabled it for the octave.