1、一、实验项目名称音频频谱分析仪设计与实现没下载券联系企鹅 241777728给你传原文件二、实验目的构建交互界面,具有播放声音文件,显示波形与频谱,并可测量声音时域参数三、实验内容与步骤1.实验步骤音频信号频谱分析仪功能:(1) 音频信号输入,从声卡输入、从 WAV 文件输入、从标准信号发生器输入;(2) 信号波形分析,包括幅值、频率、周期、相位的估计,以及统计量峰值、均值、均方值和方差的计算;(3) 信号频谱分析,频率、周期的估计,图形显示幅值谱、相位谱、实频谱、虚频谱和功率谱的曲线2.实验内容参考以上界面,查阅相关资料,构建交互界面,设计一个音频频谱分析仪,实现相应的功能。四、实验环境计算
2、机 matlab R2012a环境五、实验过程程序与分析文本各部分的功能函数1.声卡输入function shengkashuru_Callback(hObject, eventdata, handles)%声卡输入的回调函数set(handles.shengkashuru,value,1);set(handles.wavfile,value,0);set(handles.xinhaofasheng,value,0);h=findobj(Tag,luyinshijian); set(h,enable,on);h=findobj(Tag,kaishiluyin);set(h,enable,on)
3、;h=findobj(Tag,wavname);set(h,enable,off);h=findobj(Tag,dakaiwenjian);set(h,enable,off);h=findobj(Tag,inwave);set(h,enable,off);h=findobj(Tag,infudu);set(h,enable,off);h=findobj(Tag,infre);set(h,enable,off);h=findobj(Tag,inphase);set(h,enable,off);h=findobj(Tag,mix);set(h,enable,off);h=findobj(Tag,s
4、hengchengboxing);set(h,enable,off);2.开始录音function kaishiluyin_Callback(hObject, eventdata, handles)%开始录音的回调函数Fs=48000;N=str2double(get(handles.luyinshijian,String)*Fs;handles.y=wavrecord(N, Fs,1);handles.inputtype=1;guidata(hObject,handles); %保存handles结构体,使其能在其他函数中使用plot(handles.fenxiduixiang,handle
5、s.y);%在波形显示区绘出波形ysize=size(handles.y);set(handles.caiyangdianshu,String,num2str(ysize(1);3.WAV 文件function wavfile_Callback(hObject, eventdata, handles)%wav文件形式的回调函数set(handles. shengkashuru,value,0);set(handles.wavfile,value,1);set(handles.xinhaofasheng,value,0);h=findobj(Tag,luyinshijian); set(h,en
6、able,off);h=findobj(Tag,kaishiluyin);set(h,enable,off);h=findobj(Tag,wavname);set(h,enable,on);h=findobj(Tag, dakaiwenjian );set(h,enable,on);h=findobj(Tag,inwave);set(h,enable,off);h=findobj(Tag,infudu);set(h,enable,off);h=findobj(Tag,infre);set(h,enable,off);h=findobj(Tag,inphase);set(h,enable,off
7、);h=findobj(Tag,mix);set(h,enable,off);h=findobj(Tag,shengchengboxing);set(h,enable,off);4.打开文件function dakaiwenjian_Callback(hObject, eventdata, handles)%打开文件的回调函数channel=1; %声道选择filename,filepath=uigetfile(*.wav,wavfile);set(handles.wavname,string,filename);file=filepath,filename;temp,Fs=wavread(f
8、ile); %文件读取函数set(handles.caiyangpinlv,String,Fs);set(handles.caiyangdianshu,String,size(temp,1);handles.inputtype=2; %变量inputtypehandles.y=temp(:,channel);guidata(hObject,handles); %保存handles 结构体,使其能在其他函数中使用plot(handles.fenxiduixiang,handles.y); %在波形显示区绘出波形ysize=size(handles.y)5.信号发生器function xinhao
9、fasheng_Callback(hObject, eventdata, handles)%信号发生器的回调函数set(handles. shengkashuru,value,0);set(handles.wavfile,value,0);set(handles.xinhaofasheng,value,1);h=findobj(Tag,luyinshijian); set(h,enable,off);h=findobj(Tag,kaishiluyin);set(h,enable,off);h=findobj(Tag,wavname);set(h,enable,off);h=findobj(Ta
10、g, dakaiwenjian );set(h,enable,off);h=findobj(Tag,inwave);set(h,enable,on);h=findobj(Tag,infudu);set(h,enable,on);h=findobj(Tag,infre);set(h,enable,on);h=findobj(Tag,inphase);set(h,enable,on);h=findobj(Tag,mix);set(h,enable,on);h=findobj(Tag,shengchengboxing);set(h,enable,on);6.生成波形function shengche
11、ngboxing_Callback(hObject, eventdata, handles)%生成函数的回调函数Fs=str2double(get(findobj(Tag,caiyangpinlv),String);%获取采样频率N=str2double(get(findobj(Tag,caiyangdianshu),String);%获取采样点数x=linspace(0,N/Fs,N);soundtype=get(handles.inwave,Value);frequency=str2double(get(handles.infre,String);amp=str2double(get(ha
12、ndles.infudu,String);phase=str2double(get(handles.inphase,String);switch soundtype %选择波形函数case 1y=amp*sin(2*pi*x*frequency+phase);handles.inputtype=3;case 2y=amp*sign(sin(2*pi*x*frequency+phase);handles.inputtype=3;case 3y=amp*sawtooth(2*pi*x*frequency+phase,0.5);handles.inputtype=3;case 4y=amp*sawt
13、ooth(2*pi*x*frequency+phase);handles.inputtype=3;case 5y=amp*(2*rand(size(x)-1);handles.inputtype=4;endif get(handles.mix,Value)=0.0handles.y=y;elsehandles.y=handles.y+y;endguidata(hObject,handles); %保存handles结构体,使其能在其他函数中使用plot(handles.fenxiduixiang,handles.y); %在波形显示区绘出波形title(WAVE);axis(0 N -str2
14、double(get(handles.infudu,String) str2double(get(handles.infudu,String);7.时域分析function timeanalyse_Callback(hObject, eventdata, handles)%时域分析回调函数Fs=str2double(get(findobj(Tag,caiyangpinlv),String);%获取采样频率N=str2double(get(findobj(Tag,caiyangdianshu),String);%获取采样点数if handles.inputtype=0msgbox(No wave
15、 exist! Please choose a input type!);return;end%guo ling jian cen=1;ymax=max(handles.y(1) handles.y(2);ymin=min(handles.y(1) handles.y(2);from=str2double(get(handles.pointfrom,String);to=str2double(get(handles.pointto,String);if from=0 elseti(n)=i-handles.y(i)/(handles.y(i)-handles.y(i-1);% x1=i-1;%
16、 y1=handles.y(i-1);% x2=i;% y2=handles.y(i);% a=handles.y(i)-handles.y(i-1);% b=a*x1-y1;% ti(n)=b/a;endamp(n)=(ymax-ymin)/2;ymax=0;ymin=0;n=n+1;elseif ymaxhandles.y(i)ymin=handles.y(i);endendendn=n-1;%freqence and periodicityfor i=1:n-1T(i)=ti(i+1)-ti(i);endfreq=Fs/mean(T);if (handles.inputtype=1|ha
17、ndles.inputtype=2|handles.inputtype=4)set(handles.circle,String,非周期 );set(handles.outfreq,String,非周期);endif(handles.inputtype=3)set(handles.circle,String,1/freq);set(handles.outfreq,String,num2str(freq);endset(handles.outamp,String,num2str(mean(amp(2:n-1);%phasephase=2*pi*(1-(ti(1:n-1)-1)./T+floor(t
18、i(1:n-1)-1)./T);set(handles.outphase,String,num2str(mean(phase);%peakset(handles.outpeak,String,(max(handles.y(from:to)-min(handles.y(from:to)/2);%meanset(handles.outmean,String,mean(handles.y(from:to);%meansquareset(handles.outmeansquare,String,mean(handles.y(from:to).2);%sset(handles.outminus,Stri
19、ng,std(handles.y(from:to)2);8.频域分析function freqanalyse_Callback(hObject, eventdata, handles)%频域分析回调函数Fs=str2double(get(findobj(Tag,caiyangpinlv),String);%获取采样频率N=str2double(get(findobj(Tag,caiyangdianshu),String);%获取采样点数if handles.inputtype=0msgbox(No wave exist! Please choose a input type!);return;
20、endfrom=str2double(get(handles.pointfrom,String);to=str2double(get(handles.pointto,String);sample=handles.y(from:to);f=linspace(0,Fs/2,(to-from+1)/2);Y=fft(sample,to-from+1);C,I=max(abs(Y);if(handles.inputtype=1|handles.inputtype=2|handles.inputtype=4)set(handles.foutt,String,非周期);set(handles.foutfr
21、eq,String,非周期 );endif(handles.inputtype=3)set(handles.foutt,String,1/f(I);set(handles.foutfreq,String,f(I);endY=Y(1:(to-from+1)/2);plot(handles.fuzhipu,f,2*sqrt(Y.*conj(Y);%在波形显示区绘出波形plot(handles.xiangweipu,f,angle(Y); %在波形显示区绘出波形plot(handles.shipin,f,real(Y); %在波形显示区绘出波形plot(handles.xupin,f,imag(Y)
22、; %在波形显示区绘出波形plot(handles.gonglvpu,f,abs(Y).2); %在波形显示区绘出波形xlabel(handles.fuzhipu,freqency(Hz);xlabel(handles.xiangweipu,freqency(Hz);xlabel(handles.shipin,freqency(Hz);xlabel(handles.xupin,freqency(Hz);xlabel(handles.gonglvpu,freqency(Hz);ylabel(handles.fuzhipu,amplitude);ylabel(handles.xiangweipu,phase(rad);ylabel(handles.shipin,real);ylabel(handles.xupin,Imaginary);ylabel(handles.gonglvpu,power);构建的GUI界面如图所示:实验结果如图所示:1.由声卡输入2.由 WAV文件输入