📄 test_4_audio.m
字号:
clc; clear; clf;
p = 'D:\adorel\a_master_DC\standard_test_audio\';
filename = 'Windows XP Startup.wav';
file = strcat(p,filename);
[I_all,fs,nbits, opts] = wavread(file);
[m,n] = size(I_all);
if n > 1, % then stereo record
I_all = (I_all(:,1) + I_all(:,2) )./2;
end;
Ns = length(I_all); I = I_all;
Ns = 0.5*Ns;I = I_all(100:100+Ns-1);
Ns = 1000; I = I_all(1000:1000+Ns-1);
a = I;
t=(0:Ns-1).*(1/fs);
% 1. Quantize using equal-length intervals and no compander.
V = max(a);
nbits_out = 8;
step = 2 * V / 2^(nbits_out -1); % one bit is for sign...
range = [-V:step:V];
for i=1:length(a),
aq(i) = f_quantization(a(i),range);
end;
figure(1);
subplot(211), plot(t,a); hold on; stairs(t,aq,'r');
xlabel('time[s]'); title('original and quantized signal (red)');
% 2. Use same partition and codebook, but compress
% before quantizing and expand afterwards.
V = max(a);
mu = 255;
ac = compand(a,mu,V, 'mu/compressor');
for i=1:length(a),
outq(i) = f_quantization(ac(i),range);
end;
aexp = compand(outq, mu, max(outq),'mu/expander');;
figure(2);
subplot(311), plot(t,a, t,ac,'r');
title('original and compressed signal (red)');
subplot(312), plot(t,ac); hold on; stairs(t,outq,'r');
title('compressed and quantized signal (red)');
subplot(313), plot(t,a, t, aexp,'r');
title('original and expanded signal (red)');
xlabel('time[s]');
% 3. View details...
n1 = Ns / 2; n2 = n1 + 100; n =[n1:n2];
figure(3);
subplot(211), plot(n,a(n), n, aq(n),'r');
title('original and quantized (red)');
subplot(212), plot(n,a(n),n, aexp(n),'r');
title('original and expanded signal (red)');
xlabel('time[s]');
MSE_1 = sum((aq - a').^2)/ length(a);
MSE_2 = sum((aexp - a').^2) / length(a);
MSE_1 / MSE_2
% 4. Test the sounds..
sound(a, fs, nbits); pause;
sound(aexp, fs, nbits);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -