📄 tt.m
字号:
p = wavread('ringin.WAV');
indexTable = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];
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, 3327, 3660, 4026, 4428, 4871, 5358,5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767];
valpred = 0;
i=1;
index=1;
p=32767*p;
while i <= length(p)
step = stepsizeTable(index);
diff = p(i)-valpred;
if (diff < 0)
sign = 8;
else sign = 0;
end
if (sign==8)
diff = (-diff);
end
vpdiff = step/8;
% Step 2 - Divide and clamp */
% Note:
% This code *approximately* computes:
% delta(i-44) = diff*4/step;
% vpdiff = (delta(i-44)+0.5)*step/4;
% but in shift step bits are dropped. The net result of this is
% that even if you have fast mul/div hardware you cannot put it to
% good use since the fixup would be too expensive.
%
delta(i) = 0;
if ( diff >= step )
delta(i) = 4;
diff = diff-step;
vpdiff = vpdiff+step;
end
step = step/2;
if ( diff >= step )
delta(i)=bitor(delta(i),2);
diff=diff-step;
vpdiff =vpdiff+step;
end
step=step/2;
if ( diff >= step )
delta(i)=bitor(delta(i),1);
vpdiff=vpdiff+step;
end
% Step 3 - Update previous value
if (sign==8)
valpred=valpred-vpdiff;
else
valpred=valpred+vpdiff;
end
% Step 4 - Clamp previous value to 16 bits
if ( valpred > 32767 )
valpred = 32767;
else if ( valpred < -32768 )
valpred = -32768;
end
end
% Step 5 - Assemble value, update index and step values
delta(i)=bitor(delta(i),sign);
index=index+indexTable(delta(i)+1);
if ( index < 1 ) index =1;
else if ( index > 89 ) index = 89;
end
end
step = stepsizeTable(index);
i=i+1;
end
j=1;
index=1;
valpred=0;
while j<=length(delta)
% Step 2 - Find new index value (for later) */
index=index+indexTable(delta(j)+1);
if ( index < 1 ) index = 1;
end
if ( index > 89 ) index = 89;
end
% Step 3 - Separate sign and magnitude */
sign = bitand(delta(j),8);
delta(j) = bitand(delta(j),7);
% Step 4 - Compute difference and new predicted value */
% Computes 'vpdiff = (delta+0.5)*step/4', but see comment
% adpcm_coder.
%
vpdiff = step /8;
if ( bitand(delta(j),4)) vpdiff =vpdiff+step;
end
if (bitand(delta(j),2)) vpdiff=vpdiff+step/2;
end
if (bitand(delta(j),1)) vpdiff=vpdiff+step/4;
end
if ( sign==8 )
valpred=valpred-vpdiff;
else
valpred=valpred+vpdiff;
end
% Step 5 - clamp output value */
if ( valpred > 32767 )
valpred = 32767;
else if ( valpred < -32768 )
valpred = -32768;
end
end
%Step 6 - Update step value */
step = stepsizeTable(index);
j = j+1;
t(j)=valpred;
end
t=t/32767;
wavwrite(t,16000,'ringincopy.wav');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -