%SimpleCodecCompressior function [OutputBits,len,Windowing]=SimpleCodecCompressor(Input,Windowing) %%% The switch loop section sets the default values for optional variables %%% switch nargin case 1 Windowing=0; end %%% The following if section sets the sound vector depending on the input %%% if length(Input)==1 x=cos((1:2^(13))*2*pi*Input/2^(13)); %Pure tone TitleVersion=1; elseif length(Input)<100 x=cos((1:2^(13))*2*pi*Input(1)/2^(13)); %Chord for i=2:length(Input) x=x+cos((1:2^(13))*2*pi*Input(i)/2^(13)); end TitleVersion=2; else x=Input; %Input music vector len=length(x); x=x(1:floor(len/64)*64).'; TitleVersion=3; end %%% The following lines set up parameters for the compression %%% 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 b=2; 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] %%% The following for loop creates the MDCT matrix %%% 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 OutputBits=[]; % Initialize output vector %%% The following if block creates a windowing vector or an array of ones depending on the choice %%% if Windowing == 1 h=1:1:2*n; h=sqrt(2).*sin((h+.5).*(pi/(2*n))); % Windowing function else h=ones(1,2*n); end h=h.'; %x=x.*h; for k=1:nw-1 % loop over l ength 2n windows x0=x(1+(k-1)*n:2*n+(k-1)*n)'; x0=x0.*h; % Apply windowing vector y0=M*x0; y1=round(y0/q); % transform components quantized OutputBits=[OutputBits,y1]; % Storage/transmission of file occurs here end