from
Sonar Demo
by Robert Bemis
Sonic distance measurement using Windows sound card for data acquisition.
|
| sonar_anal(fig)
|
function sonar_anal(fig)
% SONAR_ANAL is a callback for the sonar demo (tof.m).
% Copyright 2001-2010 The MathWorks, Inc.
% user data
ud = get(fig,'userdata');
ai = ud.ai;
ao = ud.ao;
h = ud.h;
% measurement
y = getdata(ai); % get response when done
x = ud.x; % excitation chirp
N = ai.samplespertrigger; % record length
Fs = ai.samplerate; % samples/sec (Hz)
c = ud.c; % wave speed (m/sec)
% analysis
X = fft(x); % forward transforms
Y = fft(y);
Z = conj(X).*Y; % freq-domain equiv xcorr (circ)
% Hilbert transform => envelope
pos = 2:N/2; % double up positive frequencies
Z(pos) = 2*Z(pos);
neg = (N/2+1):N; % zero out negative frequencies
Z(neg) = 0;
z = fftshift(abs(ifft(Z))); % inverse transform
i = (1:N)'; % sample index
t = (i-1-N/2)/Fs; % time index (sec)
[pk,loc] = max(z); % height & location of peak
if length(loc)>1 % tie breaker
if max(diff(loc))==1 % fence strattler
lag = mean(t(loc)); % split the difference
else % real competition
lag = t(loc(1)); % early bird gets the worm
end
else % no contest
lag = t(loc); % time delay (sec)
end
d = lag*c; % distance (m)
% display results
plot(t*c,z/pk), axis([0 25 0 1])
hold on, plot(d,mean(z(loc))/pk), hold off
set(h.text1,'string',sprintf('Distance = %.1f m',d))
% repeat
if strcmp(get(h.Stop,'Enable'),'on') % repeating measurements
putdata(ao,x) % preload output buffer
start([ai ao]) % do it again!
else % single measurement
set(h.Repeat,'enable','on') % enable Repeat button
end
|
|
Contact us at files@mathworks.com