%%单个回音%%
[x,fs,bits] = wavread('music11.wav');
t = (0:length(x)-1)/fs;
figure(1);
subplot(3,1,1);
plot(x);xlabel('采样点');ylabel('幅度');
title('原信号');
y = fft(x);
subplot(3,1,2);plot(abs(y));title('原信号幅值');
subplot(3,1,3);plot(angle(y));title('原信号相位');
%sound(x,fs);pause(5);
%回声信号
x1 = x;x2 = x;
x1 = [x1',zeros(1,20000)];
x2 = [zeros(1,12000),0.6*x2',zeros(1,8000)];
y2 = x1+x2;
y2_2 = fft(y2);
figure(2);
subplot(3,1,1);plot(y2);title('回声信号');
subplot(3,1,2);plot(abs(y2_2));title('回声信号幅值');
subplot(3,1,3);plot(angle(y2_2));title('回声信号相位');
%sound(y2,fs);
%相关性测距
h = xcorr(y2);
figure(3);
subplot(2,1,1);plot(abs(h));title('相关函数');
%h1 = h(250000:260000);
%[h11,h11_location] = max(h1);
%h11_location = h11_location+250000;
%subplot(4,1,2);plot(h1);title('最大点');
%h2 = h(263000:270000);
%[h22,h22_location] = max(h2);
%h22_location = h22_location+263000-h11_location;
%subplot(4,1,3);plot(h2);title('次大点');
%T2 = h22_location/fs;
%d = 340*T2/2;%物理距离
%a = h22/h11;%衰减比
[h1,h1_1] = max(h);
hh1 = h;
hh1(1,h1_1-7000:h1_1+7000) = 0;
subplot(2,1,2);plot(hh1);title('次大点');
[h2,h2_2] = max(hh1);
N = abs(h1_1-h2_2)
a = h2/h1%衰减比
ts = N/fs;%延时时间
d = 340*ts/2%物理距离
%消除回音
b=1;
A = zeros(1,20000);A(1) = 1;A(12001)=0.6;
z = filter(b,A,y2);
%sound(z,fs,bits);
z1 = fft(z,fs);
figure(4);
subplot(2,1,2);plot(abs(z1));title('滤波幅值');
subplot(2,1,1);plot(angle(z1));title('滤波相位');
figure(5);
subplot(2,1,1);plot(x);title('原始信号');
subplot(2,1,2);plot(z);title('滤波信号');