%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)));
function out=simplecodec(x,b,Fs)
len=length(x);
n=2^5; % length of window (64 samples)
nw=floor(len/n); % number of length n windows in x
x=x(1:n*nw); % cut x to integer number of n windows
L=0.3; % b = quantization bits, [-L,L] amplitude range
q=2*L/(2^b-1); % q used for b bits on interval [-L, L]
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=0.3*x/max(abs(x)); % normalize signal to max amplitude = 0.3
%sound(x,Fs) % Matlab's sound command
out=[];
t=1:64;
h=sqrt(2)*sin((t-1/2)*pi/(2*n))';
count = 0;
for k=1:nw-1 % loop over length 2n windows
count = count +1;
x0=x(1+(k-1)*n:2*n+(k-1)*n)';%.*h;
y0=M*x0;
y1=round(y0/q); % transform components quantized
% 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);
w3=w(1:n,k);
%w2=w2.*h(33:64); %windowing function
%w3=w3.*h(1:32);
out=[out;(w2+w3)/2]; % collect the reconstructed signal
end % (of length 2n less than length of x)
end
%pause(1); sound(out,Fs) % play the reconstructed tone
%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)));
function out=simplecodec(x,b,Fs)
len=length(x);
n=2^5; % length of window (64 samples)
nw=floor(len/n); % number of length n windows in x
x=x(1:n*nw); % cut x to integer number of n windows
L=0.3; % b = quantization bits, [-L,L] amplitude range
q=2*L/(2^b-1); % q used for b bits on interval [-L, L]
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=0.3*x/max(abs(x)); % normalize signal to max amplitude = 0.3
%sound(x,Fs) % Matlab's sound command
out=[];
t=1:64;
h=sqrt(2)*sin((t-1/2)*pi/(2*n))';
count = 0;
for k=1:nw-1 % loop over length 2n windows
count = count +1;
x0=x(1+(k-1)*n:2*n+(k-1)*n)';%.*h;
y0=M*x0;
y1=round(y0/q); % transform components quantized
% 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);
w3=w(1:n,k);
%w2=w2.*h(33:64); %windowing function
%w3=w3.*h(1:32);
out=[out;(w2+w3)/2]; % collect the reconstructed signal
end % (of length 2n less than length of x)
end
%pause(1); sound(out,Fs) % play the reconstructed tone