1、精品文档% 设置采样速率为 8000Hz% 从 AI 中获取实际采样速率% 设置触发通道% 设置触发类型% 设置为电压上升至某值后触发% 设置触发电压值% 设置触发时延% 设置触发时延的单位% 定义超时值如何用 matlab 实现声卡的数据采集function xinhaoAI = analoginput(winsound);%2. Add channels - Add one channel to AI.chan = addchannel(AI,1);%3. Configure property values - Assign values to the basic setup proper
2、ties, and %create the variables blocksize and Fs, which are used for subsequent analysis. %The actual sampling rate is retrieved since it may be set by the engine to a %value that differs from the specified value.set(AI,SampleRate,8000)ActualRate = get(AI,SampleRate);set(AI,TriggerChannel,chan)set(A
3、I,TriggerType,software);set(AI,Triggercondition,rising);set(AI,TriggerConditionValue,0.013);set(AI, TriggerDelay, -1);set(AI, TriggerDelayUnits, seconds);set(AI,timeout,2)Fs = ActualRate;clear data1;start(AI)trydata1,time=getdata(AI);样时间保存到 timecatch time=0;data1=0;disp(A timeout occurred.);endsubpl
4、ot(2,1,1)plot(time,data1)xlabel(Time (sec.)ylabel(Signal Level (Volts)grid on% 对采集数据作滤波处理blocksize =length(data1);window = hanning(blocksize);MATLAB 自带 )data2=window.*data1;% 设置采样速率% 开始采样% 将采样得到的数据保存到 data1, 采% 绘制 2 行 1 列的第 1 张子图% 以时间为横轴,数据为纵轴作图% 标注横坐标% 标注纵坐标% 添加网格% 计算窗函数长度% 计 算汉 明 窗 函数 ( 此 函 数 为% 对
5、数据先作加窗处理b,a=ellip(4,0.1,20,3000*2/Fs);% 构造椭圆滤波器data=filter(b,a,data2);% 求加窗处理后的数据经过滤波器的响应% 当不需要 AI 时,将它从工作空间和内存中删除delete(AI)clear AI% 对滤波处理后的数据作FFT 频谱分析,并将结果在第二张子图上作图表示,加上横纵坐标和标题f,mag = daqdocfft(data1,Fs,blocksize);% 此函数为 MATLAB 自带subplot(2,1,2)plot(f,mag)grid onylabel(Magnitude (dB)xlabel(Frequency (Hz)title(Frequency Components of Incoming Signal)% 找到频谱幅度最大值,并在屏幕上显示xlim(0 1500)ymax,maxindex = max(mag);disp(Maximum occurred at , num2str(maxindex), Hz) 随意编辑