MATLAB 回声信号的产生与消除

%我们使用QQ语音聊天的时候,如果不带耳机,而使用喇叭。那么麦克风会收到两个信号:第一个人声,第二个,从喇叭传出的声音(回声)。本程序用语消除回声。

%首先使长时间的FIR滤波器产生回声。(产生房间的冲击响应)

M = 4001;

fs = 8000;

[B,A] = cheby2(4,20,[0.1 0.7]);

Hd = dfilt.df2t([zeros(1,6) B],A);

hFVT = fvtool(Hd); % Analyze the filter

set(hFVT, 'Color', [1 1 1])

H = filter(Hd,log(0.99*rand(1,M)+0.01).*sign(randn(1,M)).*exp(-0.002*(1:M)));

H = H/norm(H)*4; % Room Impulse Response

plot(0:1/fs:0.5,H);

xlabel('Time [sec]');

ylabel('Amplitude');

title('Room Impulse Response');

set(gcf, 'Color', [1 1 1])

%然后读取matlab里面自带的人声,并且播放(不必紧张,这不是英语听力)

load nearspeech

n = 1:length(v);

t = n/fs;

plot(t,v);

axis([0 33.5 -1 1]);

xlabel('Time [sec]');

ylabel('Amplitude');

title('Near-End Speech Signal');

set(gcf, 'Color', [1 1 1])

p8 = audioplayer(v,fs);

playblocking(p8);

The Far-End Speech Signal

%现在播放回声:就是从喇叭发出,房间反弹,最后被麦克风接收到的声音。这把声音是通过我们上面设计的FIR滤波器产生的

load farspeech

x = x(1:length(x));

dhat = filter(H,1,x);

plot(t,dhat);

axis([0 33.5 -1 1]);

xlabel('Time [sec]');

ylabel('Amplitude');

title('Far-End Echoed Speech Signal');

set(gcf, 'Color', [1 1 1])

p8 = audioplayer(dhat,fs);

playblocking(p8);

%人声和回声加在一起,麦克风接收到的实际声音是:

d = dhat + v+0.001*randn(length(v),1);

plot(t,d);

axis([0 33.5 -1 1]);

xlabel('Time [sec]');

ylabel('Amplitude');

title('Microphone Signal');

set(gcf, 'Color', [1 1 1])

p8 = audioplayer(d,fs);

playblocking(p8);

%使用频域自适应滤波器去除回声

mu = 0.025;

W0 = zeros(1,2048);

del = 0.01;

lam = 0.98;

x = x(1:length(W0)*floor(length(x)/length(W0)));

d = d(1:length(W0)*floor(length(d)/length(W0)));

% Construct the Frequency-Domain Adaptive Filter

hFDAF = adaptfilt.fdaf(2048,mu,1,del,lam);

[y,e] = filter(hFDAF,x,d);

n = 1:length(e);

t = n/fs;

pos = get(gcf,'Position');

set(gcf,'Position',[pos(1), pos(2)-100,pos(3),(pos(4)+85)])

subplot(3,1,1);

plot(t,v(n),'g');

axis([0 33.5 -1 1]);

ylabel('Amplitude');

title('Near-End Speech Signal');

subplot(3,1,2);

plot(t,d(n),'b');

axis([0 33.5 -1 1]);

ylabel('Amplitude');

title('Microphone Signal');

subplot(3,1,3);

plot(t,e(n),'r');

axis([0 33.5 -1 1]);

xlabel('Time [sec]');

ylabel('Amplitude');

title('Output of Acoustic Echo Canceller');

set(gcf, 'Color', [1 1 1])

p8 = audioplayer(e/max(abs(e)),fs);

playblocking(p8);

%凳凳凳凳,大功告成

%回声的产生方法很多,还有一种镜像法,模拟声音在一个六面体房间内墙面的每次反弹。澳洲某个大学的哥们的个人主页上有程序,有兴趣可以找找。