%Program 11.1 Audio codec
%input: vector x of input signal
%output: vector out of output signal
%Example usage: out=simplecodec(cos((1:2^(12))*2*pi*256/2^(13)));
%               example signal is 1/2 sec. pure tone of frequency f = 256
function out=simplecodecis(x, maxb)
len=length(x);
n=2^5;                                % length of window
nw=floor(len/n);                      % number of length n windows in x
x=x(1:n*nw);                          % cut x to integer number of n windows
Fs=2^(13);                            % Fs=sampling rate
L=0.3;                           % b = quantization bits, [-L,L] amplitude range
window = sqrt(2)*sin(((1:(2*n)) - 1/2)*pi/(2*n));
for i=1:n                             % form the MDCT matrix
  for j=1:2*n
    M(i,j)= cos((i-1+1/2)*(j-1+1/2+n/2)*pi/n);
  end
end
M=sqrt(2/n)*M;
N=M';                                 % inverse MDCT
%x=cos((1:T*Fs)*2*pi*64*f/Fs);         % test signal
%x=x+cos((1:4096)*2*pi*64*5/Fs); 
%x=x+cos((1:4096)*2*pi*64*6/Fs);
x=0.3*x/max(abs(x));                  % normalize signal to max amplitude = 0.3
sound(x,Fs)                           % Matlab's sound command

dumpster = zeros(32,1);
for k=1:nw-1                          % loop over l ength 2n windows
  x0=x(1+(k-1)*n:2*n+(k-1)*n)'.*window';
  y0=M*x0;
  dumpster = dumpster + abs(y0);
end
%dumpster
ldump = log(dumpster + 1);
b = ceil(maxb*ldump/max(abs(ldump)));
twos = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]';
q = (2*L*(twos.^b - 1).^-1);         % q used for b bits on interval [-L, L]
sum(b)

out=[];
for k=1:nw-1                          % loop over l ength 2n windows
  x0=x(1+(k-1)*n:2*n+(k-1)*n)'.*window';
  y0=M*x0;
  y1=round(y0./q);                     % transform components quantized
  if (k == 50)
      %figure();
      %plot(1:64, x0, 'O');
  end
% Storage/transmission of file occurs here  
  y2=y1.*q;                            % transform components dequantized
  w(:,k)=N*y2;                        % invert the MDCT
  if(k>1)
      w2=w(n+1:2*n,k-1).* window(33:64)';
	  w3=w(1:n,k).* window(1:32)';
      out=[out;(w2+w3)/2];          % collect the reconstructed signal
  end                                 % (of length 2n less than length of x)
end
pause(1)
plot(1:100, x(33:132), 'r', 1:100, out(1:100), 'bO')
sound(out,Fs)                         % play the reconstructed tone
