
clear

k = 1:10;
f = 64.*k;
n = 32;
t = 1:(2^12 - 2*n);  %time scale
rmse = zeros(1,10); %initialize rmse
for m = 1:length(k)
    rmsenum = 0;
    
    x =cos((1:2^(12))*2*pi*f(m)/2^13); %singal for frequencies of f*m
    x = 0.3*x/max(abs(x));
    
    %sound(x,frequency,n)
    x2 = simplecodec2(x,f);
    x1 = x(33:length(x2)+32)'; %trimmed down signal

    error = (x1 - x2).^2; %difference squred

    for i = 1:length(x2)
        rmsenum = rmsenum+error(i); %numeric rmse
    end
    rmse(m) = sqrt(rmsenum/length(x2)); %rmse for each frequency

    
    figure
    plot(t(1:100),x1(1:100),'-r',t(1:100),x2(1:100),'-b') %plots a small section of the signal
    legend('starting wave','new wave')
    title(['f = 64*',num2str(m)])
end

rmse

%The RMSE is moderately better relative to part 1. The evens in part 1 had
%the highest rmse at 0.09 and the introduction o fthe windowing function in
%part 2 reduced this error by a factor of 3 or more. Though if we apply the
%windowing functions to the lower RMSE values from part 1 we find that the
%RMSE went up on the alaready matchign functions.
%0.0045    0.0033    0.0043    0.0011    0.0043    0.0014    0.0048    0.0026    0.0043    0.0022