插到电脑的话筒口上就可以用了。
matlab代码:
clear all
AI = analoginput('winsound');
chan = addchannel(AI,[1,2]);
duration = 0.05; %1 second acquisition
set(AI,'SampleRate',44100)
ActualRate = get(AI,'SampleRate');
set(AI,'SamplesPerTrigger',duration*ActualRate)
%set(AI,'TriggerType','Manual')
%set(AI,'TriggerType','immediate')
set(AI, 'TriggerType', 'software');
set(AI, 'TriggerRepeat', 0);
set(AI, 'TriggerCondition', 'rising');
set(AI, 'TriggerConditionValue', 0.05);
set(AI, 'TriggerChannel', XXXXXannel(2));
set(AI, 'TriggerDelay', -0.01);
set(AI, 'TriggerDelayUnits', 'seconds');
set(AI, 'TimeOut', 200);
blocksize = get(AI,'SamplesPerTrigger');
Fs = ActualRate;
for j =1:100
start(AI)
try
clear datas time;
[datas,time] = getdata(AI);
catch
time = 0; datas = 0;
disp('A timeout occurred.');
end
if time==0 break,end;
data =datas(1:end,1);
data2 =datas(1:end,2);
hold off
plot(data2,'r');
hold on;
plot(data);
%for i=1:3
[tem,x2]=max(data2(700:1050));
[tem,x1]=max(data2(1050:end));
timespace=abs((x2+699)-(x1+1049))/44100;
freq=1/timespace;
speed=0.2/timespace;
text(500,-0.3,{['timespace = ',num2str(timespace*1000),'ms'],'speed = ',num2str(speed),'m/s'});
[x1,y1,button]=ginput(1);
plot(x1,y1,'*','Color',[1 0 i*0.2 ]);
[x2,y2,button]=ginput(1);
plot(x2,y2,'*','Color',[1 0 i*0.2 ]);
timespace=abs(x2-x1)/44100;
freq=1/timespace;
speed=0.2/timespace;
gtext({['timespace = ',num2str(timespace*1000),'ms'],'speed = ',num2str(speed),'m/s'});
%end
[x1,y1,button]=ginput(1);
if button==3 break,end;
%j=j-1;
end
delete(AI)
clear AI