% Daniel Jacobson
% MATH 447 Project 5 (Audio Compression)
% Problem 2 code
% inputs: f, integer multiple of 64Hz
%         len, length in seconds of tone
% outputs: plays tone, then compresses the tone using prob1codec
%          (MDCT with windowing function on overlaps), then plays
%          compressed tone, plots a small section of original vs
%          decoded signal, and returns root mean squared error
function rmse = question2(f, len)
orig = 0.3 * cos((1:len*2^13)*2*pi*(64*f)/2^13);
decoded = prob2codec(orig, 32, 4);

audiowrite(strcat('prob2-', num2str(f), '.wav'), horzcat(orig, zeros(1,2048), decoded, zeros(1,2048), (orig-decoded)*4), 2^13);

a=1;b=200;
plot(a:b, orig(a:b), a:b, decoded(a:b), a:b, orig(a:b)-decoded(a:b),'LineWidth',2);
ylim([-0.32 0.32]);
legend('Original', 'Decoded', 'Error');
title(strcat(strcat('MDCT with Windowing Function, n = 32, b = 4, f = ', num2str(f))));
print(strcat('prob2-',  num2str(f)), '-dpng');

rmse = sqrt(sum((orig-decoded).^2)/(len*2^13));