16,550
社区成员
发帖
与我相关
我的任务
分享、
Z=engGetVariable(ep,"xx");
memcpy((char*)z,(char*)mxGetPr(Z),1*sizeof(double));
function xinhao
AI = analoginput('winsound');
%2. Add channels - Add one channel to AI.
chan = addchannel(AI,1);
%3. Configure property values - Assign values to the basic setup properties, 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) % 设置采样速率为8000Hz
ActualRate = get(AI,'SampleRate'); % 从AI中获取实际采样速率
set(AI,'TriggerChannel',chan) % 设置触发通道
set(AI,'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) % 开始采样
try
[data1,time]=getdata(AI); % 将采样得到的数据保存到data1,采样时间保存到time
catch time=0;data1=0;
disp('A timeout occurred.');
end
subplot(2,1,1) % 绘制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; % 对数据先作加窗处理
[b,a]=ellip(4,0.1,20,3000*2/Fs); % 构造椭圆滤波器
data=filter(b,a,data2); % 求加窗处理后的数据经过滤波器的响应
% 对滤波处理后的数据作FFT频谱分析,并将结果在第二张子图上作图表示,加上横纵坐标和标题
[f,mag] = daqdocfft(data1,Fs,blocksize); % 此函数为MATLAB自带
subplot(2,1,2)
plot(f,mag)
grid on
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
title('Frequency Components of Incoming Signal')
% 找到频谱幅度最大值,并在屏幕上显示
xlim([0 1500])
[ymax1,maxindex1] = max(mag);
[ymax2,maxindex2] = max(data1);
xx=maxindex1;
yy=ymax1;
zz=ymax2;
disp(['最大幅值在 ', num2str(xx), ' Hz'])
disp(['最大幅值为 ', num2str(yy), ' dB'])
disp(['峰值为 ', num2str(zz), ' V'])
%当不需要AI时,将它从工作空间和内存中删除
delete(AI)
clear AI