gusucode.com > 基于matlab的adpcm语音编码源码程序 > 基于matlab的adpcm语音编码源码程序/2bitadpcm/encoder.m
% ECE 476 Final Project % ADPCM Encoder clear % Store speech signal in array x [x fs numbits]= wavread('digits8'); % fs = 8, numbits = 8 x = x*128; len = length(x); % Generate the step size lookup tables % hardcode it from the paper... index = [-1 4]; currentIndex = 2; tabledesign2; % Simple 2-bit ADPCM algorithm------------------------------------- sign_bit = 2; ss = zeros(1,len); ss(1) = table2(1); z = zeros(1,len); code = zeros(1,len); neg = 0; for n = 2:len d(n) = x(n) - z(n-1); % encoder % compute code(n) from if (d(n) < 0) neg = 1; code(n) = code(n) + sign_bit; d(n) = -d(n); else neg = 0; end if (d(n) >= ss(n-1)) code(n) = code(n) + 1; end % decode the encoded sample % calculate the quantized difference from code(n) if (neg) temp = code(n) - sign_bit; else temp = code(n); end temp2 = (temp+.5)*ss(n-1); if (neg) temp2 = -temp2; end z(n) = z(n-1) + temp2; if (z(n) > 127) z(n) = 127; elseif (z(n) < -127) z(n) = -127; end % compute the new step size temp = temp + 1; currentIndex = currentIndex + index(temp); if (currentIndex < 1) currentIndex = 1; elseif (currentIndex > numSteps) currentIndex = numSteps; end ss(n) = table2(currentIndex); end x = x/128; % put it back in the interval (-1,1)