Main Content

Create an Echometer Using Audio Measurements

该示例显示了如何使用音频数据采集和信号处理创建Echomet Sonar,该信号处理可以通过确定从表面反射的声音脉冲的飞行时间来测量距离。

该示例采用一种用于发布音频输出的同步和输入数据时间戳的方法,该应用是输入信号响应输出和/或输出/输入定时相关性相关的应用。示例应用包括声学表征设置或刺激响应实验。使用信号处理工具箱中的相关函数确定和校正相对输出/输入滞后。

要求

  • 马铃薯草R2020a or later

  • Data Acquisition Toolbox

  • Data Acquisition Toolbox Support Package for Windows Sound Cards

  • Signal Processing Toolbox

硬件设置

运行此示例需要:

  • Focusrite Scarlett 2i2 audio interface device, or another device / sound card with two output and two input audio channels

  • Audio interface device DirectSound drivers provided by the vendor, or using default Windows device drivers if available

  • One powered speaker and one microphone compatible with the audio interface device

  • Audio patch cables and connector adaptors

典型的声音处理软件suppor音频接口设备ted by Data Acquisition Toolbox do not support hardware synchronization between the output and input channels. Pairs of audio input or output channels are synchronized by the audio device, however the output and input channels can have a non-negligible relative start lag.

To synchronize the output and input data timestamps in post-processing, the following setup can be used:

  • Connect one of the output channels (output 1 or left channel of stereo plug) to one of the input channels (input 1 or left channel) to generate and acquire a synchronization signal in a loopback configuration.

  • 其他输出(输出2或右信道)和输入通道(输入2或右信道)用于输出激励/刺激信号,并分别获取测量/响应信号。

  • Read data from both audio input channels, with one of the channels being used for reading the synchronization signal, and the other channel for reading the actual response signal.

  • With this setup, you read data from both audio input channels, and simultaneously write data to both audio output channels.

echomet sonar.

As a demonstration of this approach, you can use an audio device, a powered speaker, and a microphone to put together an echometer sonar setup. A pulse echometer measures the distance to an object by emitting a short sound pulse, measuring the reflected pulse echo, and determining the time of flight by comparing the original output pulse signal with the measured input response signal.

扬声器和麦克风彼此相邻放置并朝向其中壁的壁,声音脉冲反射,如下图所示。

Synthesize a Pulse Signal

通常用于声纳应用的脉冲信号是短持续时间扫描或啁啾。因为扁平啁啾信号的开始和结束时的尖锐幅度边缘可能导致测量伪影,所以脉冲通过包络函数衰减/成形。选项包括汉宁,高斯,凯撒等频率范围,脉冲宽度或持续时间,脉冲包络/形状取决于预期的应用。在该示例中,使用具有1-5kHz线性频率啁啾的3 ms持续时间脉冲和汉宁窗口进行测量。合成信号如下图所示。原始啁啾信号显示在左侧,右侧示出了成形脉冲。汉宁窗口显示为虚线。

确保使用扬声器正确生成的信号频率范围,由麦克风拾取,并由音频接口设备采样。用于音频设备测量的采样率为192 kHz。

%脉冲宽度T = 3E-3;% Sampling rate (Hz)Fs = 192E+3;% Initial and final pulse frequency (Hz)f0 = 1e + 3;f1 = 5e + 3;%时间向量t = (0:1/Fs:T)';% Pulse signal, chirp attenuated by an windowing functionyc = chirp(t,f0,t(end),f1); w = hanning(numel(t)); y = yc.*w;%绘制信号图tileplot = tiledlayout(1,2);tileplot.tilespacing =.'compact';tileplot.Padding ='compact';Nextdile Plot(T,YC)保持onplot(t,w,' - ') xlabel("Time (s)") ylabel("Amplitude") 标题("Original chirp signal and Hanning envelope") nexttile plot(t,y) xlabel("Time (s)") 标题("Shaped pulse")

Data Acquisition

Use two separate数据采集对象,一个用于音频输出通道,一个用于音频输入通道。由于即使是常见的,音频输入和输出通道对在音频输入和输出通道对之间也没有自动同步数据采集对象用于所有通道,这种方法允许更多地控制数据采集操作。

do = daq("directsound");addoutput(do,"Audio4","1","Audio");addoutput(do,"Audio4","2","Audio");do.Rate = Fs; di = daq("directsound");addinput(di,"Audio1","1","Audio");addinput(di,"Audio1","2","Audio");di.Rate = Fs;

由于脉冲持续时间相对较短,因此填充具有零值(200ms持续时间)的脉冲信号的结束,以确保脉冲被正确产生。

yout = [y; zeros(Fs*200E-3,1)];

首先启动输入作为连续后台采集,然后在两个音频输出通道上生成相同的信号。其中一个通道用作同步信号。

开始(di,“连续”)写(做,[yout])停止(di)

Read the acquired data into the workspace. By default, thereadfunction returns atimetable.

数据=读取(di,"all");

Plot the acquired data. Signals Audio1_1 and Audio1_2 correspond to the audio input channels 1 and 2. Input channel 1 was used to record the synchronization signal generated by output channel 1 in a loopback configuration. Input channel 2 was used to record the actual response signal picked by the microphone. Two pulses are observed in the response signal, followed by other secondary echoes which depend on the room acoustics. Also notice the large relative start lag time between the input and output channels.

图堆叠(数据)

同步输出和输入数据时间戳

Find the lag and align the output and input signal timestamps by discarding the points before the detected synchronization signal.

lag = finddelay(y,data.audio1_1);T0 = LAG / FS
t0 = 0.3058
alpientData =数据(LAG + 1:结束,:);alplipddata.time = simbleddata.time-alimentData.time(1);图S = StackEdplot(SententedData);XLIM(秒([0 0.025]))S.AXESPROPERTIES(1).ylimits = [-0.55 0.55];

Visually validate the quality of the time alignment, by comparing the synthetic pulse data with the measured loopback signal.

figure plot(seconds(t),y,alignedData.Time(1:numel(t)),alignedData.Audio1_1(1:numel(t))) ylabel("Amplitude") xlabel("Time (s)") legend(["Pulse signal to output",“测量环回信号”],"Location","bestoutside")

Determine Pulse Propagation Time and Distance

You can use theXCorr.cross-correlation function to determine and visualize similarities between the original pulse signal and the measured response signal.

[Xcorr,LAG] = XcorR(SententData.Audio1_2,Y);图绘图(LAGS / FS,XCORR)XLABEL('Lags (s)') ylabel('Cross-correlation') axis紧的

互相关图表示多个相似之处,其中两个较大的峰值和其他较小的混响峰值。

Find timestamp and total propagation distance corresponding to the first two observed correlated pulses in the measured signal. The first observed pulse corresponds to the direct propagation path from the speaker to the microphone. The second observed pulse is the echo pulse reflected by the wall. The functionfinddelayreturns the lag for which the normalized cross-correlation has the highest value, and in this case it corresponds to the first pulse in the response signal.

t1 = finddelay(y,sententeddata.audio1_2)/ fs
t1 = 0.0011

你可以计算propagation time of the echo pulse as the starting timestamp of the second pulse in the response signal by usingfinddelay在第一脉冲之后的信号区域中。

t2 = t1 + T + finddelay(y,alignedData(timerange(seconds(t1+T),“inf”),:)。Audio1_2)/ FS
T2 = 0.0122
% Plot the response signal and highlight the first two detected pulsesfigure plot(alignedData.Time,alignedData.Audio1_2) xlim(seconds([0 t2+2*T])) holdonfirstPulse = alignedData(timerange(seconds(t1),seconds(t1+T)),:); plot(firstPulse.Time,firstPulse.Audio1_2) echoPulse = alignedData(timerange(seconds(t2),seconds(t2+T)),:); plot(echoPulse.Time,echoPulse.Audio1_2) ylabel("Amplitude") xlabel("Time (s)")

%计算与回声脉冲相对应的距离(m)% Speed of sound in air at 20 deg. C (m/s)v = 343.1; d2 = t2*v/2
D2 = 2.1006.

The distance (2.10 m) measured by the echometer sonar setup (half of the total path) is very close to the actual distance (2.06 m) between the speaker/microphone setup and the wall reflecting the echo pulse.