⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sonar_anal.m

📁 matlab开发的一个声音处理程序
💻 M
字号:
function sonar_anal(fig)
% SONAR_ANAL is a callback for the sonar demo (tof.m).

% user data
ud=get(fig,'userdata');
ai=ud.ai;
ao=ud.ao;
h=ud.h;
txt=h.text1;

% 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); Y=fft(y);                     % forward transforms
Z=conj(X).*Y;                           % freq-domain equiv xcorr (circ)

% Hilbert transform => envelope
pos=2:N/2; Z(pos)=2*Z(pos);             % double positive frequencies
neg=(N/2+1):N; Z(neg)=0;                % zero out neg frequencies

z=fftshift(abs(ifft(Z)));               % inverse transform

i=(1:N)';                   % sample index
t=(i-1-N/2)/Fs;             % time index (sec)

z(t>6/c)=0;                 % ignore insane distances (>6m)

[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
%disp(sprintf('t=%.2fmsec d=%.3fm,%.1fin',lag*1e3,d,d/0.0254))
plot(t*c,z/pk), axis([0 6 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -