Sie sind auf Seite 1von 5

clear all

close all
clc
%fid = fopen('voice1.wav','r'); % open given raw data file
%[s_in,COUNT] = fread(fid,'int16'); % read the given data into vector X
inputfilename=input('Enter the Wave file name (ie ''test.wav'') => ','s');
disp('
OK ')
[s_in,fs,N]=wavread(inputfilename);
%s_in=s_in(1:end,1);
s_in=s_in';
%zzz=zeros(1,500);%[1 -1 1-1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1
-1 1 -1 1-1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1-1 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1-1 1 -1 1 -1 1 -1 1 -1
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1];
%s_in=[zzz,s_in];
COUNT=22000;
disp(' PLEASE WAIT ADPCM CODING IN PROGRESS');
disp('
')
%%%%%%%%%%%%%LOOK UP TABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
indexTable=[-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];
%stepsizeTable=[ 16 17 19 21 23 25 28 31 34 37 41 45 50 55 60 66 73 80 88 97 107
118 130 143 157 173 190 209 230 253 279 307 337 371 408 449 494 544 598 658 724
796 876 963 1060 1166 1282 1411 1552];
stepsizeTable = [7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34
, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190,
209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876,
963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 33
27, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 114
87, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767]
;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%ENCODER %%%%%%%%%%%%%%%%%%%%%
%initialization
d_n=0;
x_p=0;% x predicted
%d_n=??????
B3 = 0; B2 =0 ; B1 =0; B0 = 0;% bits of quantized difference
index=1;
ss_n=stepsizeTable(index);% stepsize
time=clock;
for (i=1:COUNT)
d_n= s_in(i) - x_p; %%
if d_n 0
B3 = 1;
d_n = abs(d_n);
else
B3 = 0;
end
if (d_n >= ss_n)
B2 = 1; d_n = (d_n - ss_n);
else
B2 = 0;
end
if (d_n >= (ss_n/2) )

B1 = 1; d_n = (d_n - ss_n) / 2 ;


else
B1 = 0;
end
if (d_n >= (ss_n / 4))
B0 = 1;
else
B0 = 0;
end
L_n(i)=(2^3 * B3) + (2^2 * B2) + (2 * B1) + B0;% output ADPCM sample
%%%%%%%%%%encoder end
%%%%%%%%%%decoder start
diff = (ss_n*B2) + ((ss_n/2)*B1) + ((ss_n/4)*B0) +( ss_n/64);
if B3 == 1
diff = diff * (-1); end
x_p = x_p + diff;
if x_p = -32767
x_p = -32767 ; end
if x_p >= 32767
x_p = 32767; end
predicted(i)=x_p;
%%%%%%%decoder end will give predicted value x_p
%%%%%%%%%step size calculation
%ss_n = ss(n) * 1.1*M(L_n); % step size calculation
switch L_n(i)
case {0,1,2,3}
k=1;
case 4
k=5;
case 5
k=6;
case 6
k=7;
case 7
k=8;
otherwise
k=1;
end
index = index + indexTable(k) ;
% Clamp index.
if index = 0
index = 1; end
if index > 89
index = 89; end
%
if index > 49
index = 49; end
ss_n = stepsizeTable(index) ;

i =i+1;
end
comptime= etime(clock,time);
%fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%ENCODER ENDS %%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%DECODER STARTS %%%%%%%%%%%%%%%%%%%%%


%restart execution clock
time = clock;
%%INITIALIZATION
d_index=1;
d_ss_n=stepsizeTable(d_index);% stepsize
d_op=0;
for (j=1:COUNT)
d_B0= bitget(L_n(j),1);d_B1= bitget(L_n(j),2);d_B2= bitget(L_n(j),3);d_B3= bitge
t(L_n(j),4);
d_diff = (d_ss_n*d_B2) + ((d_ss_n/2)*d_B1) + ((d_ss_n/4)*d_B0) +( d_ss_n/64);
if d_B3 == 1
d_diff = d_diff * (-1); end
d_op = d_op + d_diff;
if d_op = -32767
d_op = -32767 ; end
if d_op >= 32767
d_op = 32767; end
output(j)=d_op;%%%
%calculating step size from incoming data
switch L_n(j)
case {0,1,2,3}
m=1;
case 4
m=5;
case 5
m=6;
case 6
m=7;
case 7
m=8;
otherwise
m=1;
end
d_index = d_index + indexTable(m) ;
% Clamp index.
if d_index = 0
d_index = 1; end
if d_index > 89
d_index = 89; end
%if d_index > 49
d_index = 49; end
d_ss_n = stepsizeTable(d_index) ;
j=j+1;
end
decomptime= etime(clock,time);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%DECODER ENDS %%%%%%%%%%%%%%%%%%%%%%%%
disp(' DPCM CODING DECODING FINISHED');

comin=fix(comptime/60);
cosec=round(rem(comptime,60));
demin=fix(decomptime/60);
desec=round(rem(decomptime,60));
disp(' ')
str=[' Time taken to compress file = ',int2str(comin),'min ',int2str(cosec),'sec
'];
disp(str)
str=[' Time to decopress file = ',int2str(demin),'min ',int2str(desec),'sec'];
disp(str)
disp(' ')

%%%%%%%%%%%%%%%%%%%%%%%%%% STATS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


samples=size(s_in);
inputbyte=round((samples(2)*N)/8);
chanlbits=4;
outputbyte=round((samples(2)*chanlbits)/8);
% Percentage compression
percomp=(1-chanlbits/N)*100;
disp('UNCOMPRESSED FILE')
str=[' Samples = ',int2str(samples(2)),' samples at ',int2str(N),'bits and ',int
2str(fs),' Hz'];
disp(str)
str=[' File size = ',int2str(inputbyte(1)),' bytes'];
disp(str)
disp(' ')
disp('COMPRESSED FILE')
str=[' Number of bits per Channel = ',int2str(chanlbits)];
disp(str)
disp(' ')
str=[' Samples = ',int2str(samples(2)),' samples at ',int2str(chanlbits),'bits a
nd ',int2str(fs),' Hz'];
disp(str)
str=[' File size = ',int2str(outputbyte(1)),' bytes'];
disp(str)
disp(' ')
str=[' Compression percentage = ',int2str(percomp),'%'];
disp(str)
disp(' ')
%%%%%%%%%%%%%%%%%%%%%%%%%%SOUNDS%%%%%%%%%%%%%%%%%
loop=1;
while (loop==1)
playlet=input('Do you want to hear sounds of the data (Y/N)? ','s');
switch playlet
case {'Y','y'}
disp('Playing Input Data')
pause(1)
wavplay(s_in,8000);
disp(' ')
pause(3)
disp('Playing Decoded Data')
disp(' ')
wavplay(output,8000);

otherwise
%disp('OK. THANX & ALLAH HAFIZ')
disp(' ')
loop=0;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%PLOTS%%%%%%%%%%%%%%%%%%
let1=input('Do you want to view plots of the data (Y/N) ? ','s');
switch let1
case {'Y','y'}
subplot(2,1,1)
xx=1:151;
plot(xx,s_in(300:450))
title('Original Speech Signal');
subplot(2,1,2)
plot(xx,output(300:450))
title('Decompressed ADPCM Speech Signal');
otherwise
disp(' ')
disp('OK. THANX & ALLAH HAFIZ')
disp(' ')
end

Das könnte Ihnen auch gefallen