使用音响与麦克风进行声波测距离

今天来聊一聊根据声波测距离的实验,听起来比较迷,但这是目前无线感知领域比较火的一个方向,即通过声波去获取更多的数据,并用于实际生活。

直接补补课,我们平时听到的声音就是20HZ-20000HZ的声波,因此使用计算机去模拟发出一个一定频率的波形,波遇到物理后会反射回来,通过发送波与接受波的时差t,我们可以简单这么去想,声波的速度v=343m/s,那么就可以得到距离R=v*t/2,当然,实际上并没有这么简单。

接下里简单讲讲代码及原理:

这里是一个使用matlab播放FMCW波,并使用麦克风录音的代码:

% 播放声音
A=10;%振幅
f_0=18000;%声音频率
B=2300; %带宽
T=0.02; %周期
fs=48000;   %采样频率
N=4800;    % 采样点数
t=(0:N-1)/fs; %播放时长
% tt=t.*t;
temp=mod(t,T);
tt=temp.*temp;
% y=A*cos(2*pi*(f_0*t)); %单频信号
y=A*cos(2*pi*(f_0*mod(t,T)+B*tt/(2*T))); %单频信号
% figure(2)
% subplot(1,2,1);
plot(t,y);
xlabel('时间');
ylabel('振幅');
title('原始声波图像');
sound(y,fs);  %通过声卡放音

%录音
recordFs=48000; %麦克风采用率
deviceId=3;  %设备号是3
nChannels=1; %单通道是1
nBits = 8; %采样位数
myVoice = audiorecorder(recordFs,nBits,nChannels);

% Define callbacks to show when
% recording starts and completes.
myVoice.StartFcn = 'disp(''Start speaking.'')';
myVoice.StopFcn = 'disp(''End of recording.'')';

time=0.1;
N2=time*recordFs;  % 采样点数
record(myVoice, time);


获取到波后,进行的分析如下:

这里是一个对接受到的信号进行fft的过程

% %% 接受信号
%预处理
%Store data in double-precision array.
myRecording = getaudiodata(myVoice);

lFreq=f_0;
hFreq=lFreq+B;
% band pass,过滤波
[b,a]   = butter(3,[lFreq hFreq]/(fs/2),'bandpass');
myRecording       = filter(b,a,myRecording);

%reshape
myRecording=reshape(myRecording,1,N2);
myRecording=myRecording*1000;
t1=(0:N2-1)/recordFs;
figure(2)
subplot(1,2,1);
plot(t1,myRecording)

%fft
start_p=1;
end_p=4800;
number=end_p-start_p+1;

Y = fft(myRecording(start_p:end_p));
P2 = abs(Y/number);
P1 = P2(1:number/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = 48000*(0:(number/2))/number;
subplot(1,2,2);
plot(f,P1) 
title('Single-Sfided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')


% 求出最大的频率
[row,cow] = size(P1);    % 读取行r、列c
sendMaxFFT=-10000;
maxIndex=-1;
for i = 1:cow
    if sendMaxFFT < P1(1,i)
        sendMaxFFT = P1(1,i);
        maxIndex=i;
    end
    P1(1,i);
end

sendMaxFFT=f(1,maxIndex)

最后要做的就是将发送波的t-f的关系与接收波的t-f进行cross-correlation得出Lap,根据公式R=c*Lap/2*Fs,Fs是采样率,就可以得到距离了。(此部分正在进行实验)

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值