置。
表3 控件属性设置 控件类型 Axes Push Button Push Button Push Button Push Button Push Button Push Button Push Button 4.2.3代码的实现
(1)在nidaq.m文件中,设置打开回调函数nidaq_OpeningFcn(),添加如下代码完成程序初始化工作具体代码如下:
function nidaq_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to nidaq (see VARARGIN) openDAQ=daqfind;
for i=1:length(openDAQ); stop(openDAQ(i)); delete(openDAQ(i)); end
% Choose default command line output for nidaq handles.output = hObject; % Update handles structure guidata(hObject, handles);
(2)在nidaq.m文件中,分别在“采集和显示”、“保存”、调用、“关闭”等按钮相应回调函数pushbutton1_Callback()、pushbutton2_Callback、pushbutton3_Callback、pushbutton4_Callback …中添加代码分别实现声卡数据采集和显示、保存数据、调用和关闭GUI窗口等功能,具体代码如下:
unction pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
19
Tag属性 axes1 pushbutton1 Pushbutton2 Pushbutton3 Pushbutton4 Pushbutton5 Pushbutton6 Pushbutton7 String属性 —— 采集和显示 保存 频域分析 关闭 测试 时域分析 读取播放 其他类型 默认 默认 默认 默认 默认 默认 默认 默认 功能 绘图 采集命令 保存命令 频域分析 关闭对话框 发出测试信号 时域分析 播放 % handles structure with handles and user data (see GUIDATA) global data; global Fs;
ai=analoginput('winsound'); chan=addchannel(ai,1); duration=3; Fs=22050;
blocksize=duration*Fs; set(ai,'TriggerType','Manual'); start(ai); trigger(ai); data=getdata(ai); %subplot(121); stop(ai); delete(ai); plot(data); grid on;
title('声音信号的波形显示'); ylabel('Signal level(volts) '); xlabel('Samples');
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global data; global Fs;
wavwrite(data,'sound');
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [data,Fs,N]=wavread('sound.wav');
20
figure(3);
[f,mag] = daqdocfft(data,Fs,12000); subplot(3,1,1); plot(f,mag);
ylabel('magnitude(dB)'); xlabel('frequency(Hz)') title('原信号的频谱图');
x=fft(data.*hamming(length(data))); fm=5000*length(x)/Fs; f=(0:fm)*Fs/length(x);?? subplot(3,1,2);
plot(f,20*log10(abs(x(1:length(f)))+eps)); ?? grid on;
title('滤波后的频谱图'); ylabel('magnitude(dB)'); xlabel('frequency(Hz)') subplot(3,1,3); specgram(data,512,Fs); title('语谱图');
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close all force;
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global data;
ao=analogoutput('winsound'); chan=addchannel(ao,1); duration=4;
21
set(ao,'SampleRate',8000); ActualRate=get(ao,'SampleRate'); set(ao,'TriggerType','Manual'); bls=ActualRate*duration;
data=cos(linspace(0,2*pi*500,bls))'; putdata(ao,data); start(ao); trigger(ao); waittilstop(ao,5); delete(ao); clear ao;
function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [data,Fs,N]=wavread('sound.wav'); F=enframe(data,200); f5=F(:,5); figure(2); subplot(2,1,1); plot(f5); xlabel('样点'); title('清音波形图'); f8=F(:,8); subplot(2,1,2); plot(f8); xlabel('样点'); title('浊音波形图');
function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [data,Fs,N]=wavread('sound.wav'); wavplay(data,Fs);
22
5程序的调试与分析
程序设计、调试完毕,运行程序。直接将带有麦克风的耳机插头插在声卡信号输入端,输入声音信号进行调试。下图10是程序调试的结果:
图10 调试结果图
第一步:点击“测试”按钮,系统将会把一个余弦信号向外发送,扬声器将会把其播放出来。测试设备能否正常工作。
第二步:点击“采集和显示”按钮,系统将会开始采集长达2秒的声音,并且绘出其时域波形图。如下图11所示:
图11 采集的声音波形显示
23