1、实验八 音频频谱分析仪设计与实现1、实验原理MATLAB 是一个数据分析和处理功能十分强大的工程实用软件,其数据采集工具箱为实现数据的输入和输出提供了十分方便的函数和指令。本实验基于声卡与 MATLAB 实现音频信号频谱分析仪。1、频率(周期)检测对周期信号来说,可以用时域波形分析来确定信号的周期,也就是计算相邻两个信号波峰的时间差或过零点的时间差。采用过零点(ti)的时间差 T(周期) 。频率即为 f=1/T,由于能够求得多个 T 值,故采用他们的平均值作为周期的估计值。2、峰值检测在一个周期内,求出信号最大值 与最小值 的差得一半记为 A,同样得到多maxyminy个 A 值,但第一个 A
2、 值对应的 和 不是在一个周期内搜索得到的,故以除第一个in以外的 A 值的平均作为幅值的估计值。3、相位检测采用过零法,即通过判断与同频零相位信号过零点时刻,计算其时间差,然后换成相应的相位差。 ,同样以 的平均值作为相位的估计值。) ( T/ti-124、数字信号统计量估计(1)峰值 P 的估计在样本数据 x 中找出最大值与最小值,其差为双峰值,双峰值的一半即为峰值。(2)均值估计,N 为样本容量。 1)y(E(3)均方值估计 22)((4)方差估计 2)(E-N1)y(Dy5、频谱分析原理时域分析只能反映信号的幅值随时间的变化情况,除但频率分量的简单波形外,很难明确提示信号的频率组成和各
3、频率分量大小,而频谱分析能很好的解决此问题。(1)DFT 与 FFT对于给定的时域信号 y,可以通过 Fourier 变换得到频域信息 Y。Y 可按下式计算式中,N 为样本容量,t = 1/Fs 为采样间隔。采样信号的频谱是一个连续的频谱,不可能计算出所有的点的值,故采用离散 Fourier 变换(DFT),即式中,f = Fs/N。但上式的计算效率很低,因为有大量的指数(等价于三角函数) 运算,故实际中多采用快速 Fourier 变换(FFT)。其原理即是将重复的三角函数算计的中间结果保存起来,以减少重复三角函数计算带来的时间浪费。由于三角函数计算的重复量相当大,故FFT 能极大地提高运算效
4、率。(2) 频率、周期的估计对于 Y(k f),如果当 kf = 时,Y(k f)取最大值,则 为频率的估计值,由于采样 f间隔的误差, 也存在误差,其误差最大为 f / 2。周期 T=1/f。f从原理上可以看出,如果在标准信号中混有噪声,用上述方法仍能够精确地估计出原标准信号的频率和周期。(3)频谱图为了直观地表示信号的频率特性,工程上常常将 Fourier 变换的结果用图形的方式表示,即频谱图。以频率 f 为横坐标,|Y(f)|为纵坐标,可以得到幅值谱;以频率 f 为横坐标,arg Y(f)为纵坐标,可以得到相位谱;以频率 f 为横坐标,Re Y(f)为纵坐标,可以得到实频谱;以频率 f
5、为横坐标,Im Y(f)为纵坐标,可以得到虚频谱。根据采样定理,只有频率不超过 Fs/2 的信号才能被正确采集,即 Fourier 变换的结果中频率大于 Fs/2 的部分是不正确的部分,故不在频谱图中显示。即横坐标 f 0, Fs/26、模块划分模块化就是把程序划分成独立命名且可独立访问的模块,每个模块完成一个子功能,把这些模块集成起来构成一个整体,可以完成制定的功能,满足用户需求。2、实验内容设计一个音频频谱分析仪,功能包括:(1)音频信号输入,从声卡输入、从 WAV 文件输入、从标准信号发生器输入;(2)信号波形分析,包括幅值、频率、周期、相位的估计,以及统计量峰值、均值。均方值和方差的计
6、算。(3)信号频谱分析,频率、周期的估计,图形显示幅值谱、相位谱、时频谱、虚频谱和功率谱的曲线。3实验结果1.声卡输入(1)正弦波(2)方波(3)三角波(4)锯齿波(5)白噪声程序function varargout = fanwenhan(varargin)gui_Singleton = 1;gui_State = struct(gui_Name, mfilename, .gui_Singleton, gui_Singleton, .gui_OpeningFcn, fanwenhan_OpeningFcn, .gui_OutputFcn, fanwenhan_OutputFcn, .gui_
7、LayoutFcn, , .gui_Callback, );if nargin endif nargoutvarargout1:nargout = gui_mainfcn(gui_State, varargin:);elsegui_mainfcn(gui_State, varargin:);endfunction fanwenhan_OpeningFcn(hObject, eventdata, handles, varargin)handles.inputtype=0;xlabel(handles.plot1,freqency(Hz);xlabel(handles.plot2,freqency
8、(Hz);xlabel(handles.plot3,freqency(Hz);xlabel(handles.plot4,freqency(Hz);xlabel(handles.plot5,freqency(Hz);ylabel(handles.plot1,amplitude);ylabel(handles.plot2,phase(rad);ylabel(handles.plot3,real);ylabel(handles.plot4,Imaginary);ylabel(handles.plot5,power);handles.output = hObject;function varargou
9、t = fanwenhan_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output;function figure1_CreateFcn(hObject, eventdata, handles)function timeanalyse_Callback(hObject, eventdata, handles)Fs=str2double(get(findobj(Tag,samplerate),String);N=str2double(get(findobj(Tag,samplenum),String);if handl
10、es.inputtype=0msgbox(No wave exist! Please choose a input type!);return;endn=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)
11、;endamp(n)=(ymax-ymin)/2;ymax=0;ymin=0;n=n+1;elseif ymaxhandles.y(i)ymin=handles.y(i);endendendn=n-1;for i=1:n-1T(i)=ti(i+1)-ti(i);endfreq=Fs/mean(T);set(handles.outt,String,1/freq);set(handles.outfreq,String,num2str(freq);set(handles.outamp,String,num2str(mean(amp(2:n-1);phase=2*pi*(1-(ti(1:n-1)-1)
12、./T+floor(ti(1:n-1)-1)./T);set(handles.outphase,String,num2str(mean(phase);set(handles.outpeak,String,(max(handles.y(from:to)-min(handles.y(from:to)/2);set(handles.outmean,String,mean(handles.y(from:to);set(handles.outmeansquare,String,mean(handles.y(from:to).2);set(handles.outs,String,std(handles.y
13、(from:to)2);function WAVfile_Callback(hObject, eventdata, handles)h=findobj(Tag,filename);set(h,enable,on);h=findobj(Tag,freq);set(h,enable,off);h=findobj(Tag,amp);set(h,enable,off);h=findobj(Tag,phase);set(h,enable,off);set(findobj(Tag,recordtime),enable,off);set(handles.channel,enable,on);set(hand
14、les.fileopen,enable,on);set(handles.gensig,enable,off);set(handles.wavetype,enable,off);set(handles.add,enable,off);set(handles.startrecord,enable,off);function soundcard_Callback(hObject, eventdata, handles)set(findobj(Tag,recordtime),enable,on);h=findobj(Tag,filename);set(h,enable,off);h=findobj(T
15、ag,freq);set(h,enable,off);h=findobj(Tag,amp);set(h,enable,off);h=findobj(Tag,phase);set(h,enable,off);set(handles.channel,enable,off);set(handles.fileopen,enable,off);set(handles.gensig,enable,off);set(handles.wavetype,enable,off);set(handles.add,enable,off);set(handles.startrecord,enable,on);funct
16、ion generator_Callback(hObject, eventdata, handles)h=findobj(Tag,filename);set(h,enable,off);h=findobj(Tag,freq);set(h,enable,on);h=findobj(Tag,amp);set(h,enable,on);h=findobj(Tag,phase);set(h,enable,on);set(findobj(Tag,recordtime),enable,off);set(handles.channel,enable,off);set(handles.fileopen,ena
17、ble,off);set(handles.gensig,enable,on);set(handles.wavetype,enable,on);set(handles.add,enable,on);set(handles.startrecord,enable,off);function filename_Callback(hObject, eventdata, handles)function filename_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObje
18、ct,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction freq_Callback(hObject, eventdata, handles)function freq_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction amp_Callbac
19、k(hObject, eventdata, handles)function amp_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction phase_Callback(hObject, eventdata, handles)function phase_CreateFcn(hObject, eventdata, handle
20、s)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction samplerate_Callback(hObject, eventdata, handles)function samplerate_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,Backgroun
21、dColor,get(0,defaultUicontrolBackgroundColor);endfunction samplenum_Callback(hObject, eventdata, handles)function samplenum_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction ampfreq_Creat
22、eFcn(hObject, eventdata, handles)function recordtime_Callback(hObject, eventdata, handles)function recordtime_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction startrecord_Callback(hObjec
23、t, eventdata, handles)Fs=str2double(get(findobj(Tag,samplerate),String);handles.y=wavrecord(str2double(get(findobj(Tag,recordtime),String)*Fs, Fs,int16);handles.inputtype=1;guidata(hObject,handles);plot(handles.time,handles.y);title(WAVE);ysize=size(handles.y)set(handles.samplenum,String,num2str(ysi
24、ze(1);function fileopen_Callback(hObject, eventdata, handles)temp = wavread(get(findobj(Tag,filename),String);channel=str2double(get(handles.channel,String);handles.y=temp(:,channel);handles.inputtype=2;guidata(hObject,handles);plot(handles.time,handles.y);title(WAVE);ysize=size(handles.y)set(handle
25、s.samplenum,String,num2str(ysize(1);function gensig_Callback(hObject, eventdata, handles)Fs=str2double(get(findobj(Tag,samplerate),String);N=str2double(get(findobj(Tag,samplenum),String);x=linspace(0,N/Fs,N);soundtype=get(handles.wavetype,Value);frequency=str2double(get(handles.freq,String);amp=str2
26、double(get(handles.amp,String);phase=str2double(get(handles.phase,String);switch soundtypecase 1y=amp*sin(2*pi*x*frequency+phase);case 2y=amp*sign(sin(2*pi*x*frequency+phase);case 3y=amp*sawtooth(2*pi*x*frequency+phase,0.5);case 4y=amp*sawtooth(2*pi*x*frequency+phase);case 5y=amp*(2*rand(size(x)-1);
27、otherwiseerrordlg(Illegal wave type,Choose errer);endif get(handles.add,Value)=0.0handles.y=y;elsehandles.y=handles.y+y;endhandles.inputtype=3;guidata(hObject,handles);plot(handles.time,handles.y);title(WAVE);axis(0 N -str2double(get(handles.amp,String) str2double(get(handles.amp,String);function ou
28、tfreq_Callback(hObject, eventdata, handles)function outfreq_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction outamp_Callback(hObject, eventdata, handles)function outamp_CreateFcn(hObject
29、, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction outphase_Callback(hObject, eventdata, handles)function outphase_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hO
30、bject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction outpeak_Callback(hObject, eventdata, handles)function outpeak_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction ou
31、tmean_Callback(hObject, eventdata, handles)function outmean_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction outmeansquare_Callback(hObject, eventdata, handles)function outmeansquare_Cre
32、ateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction outs_Callback(hObject, eventdata, handles)function outs_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);else
33、set(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction foutfreq_Callback(hObject, eventdata, handles)function foutfreq_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfun
34、ction foutamp_Callback(hObject, eventdata, handles)function foutamp_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction foutphase_Callback(hObject, eventdata, handles)function foutphase_Cre
35、ateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction edit18_Callback(hObject, eventdata, handles)function edit18_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);
36、elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction edit19_Callback(hObject, eventdata, handles)function edit19_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfun
37、ction edit20_Callback(hObject, eventdata, handles)function edit20_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction edit21_Callback(hObject, eventdata, handles)function edit21_CreateFcn(h
38、Object, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction freqanalyse_Callback(hObject, eventdata, handles)Fs=str2double(get(findobj(Tag,samplerate),String);N=str2double(get(findobj(Tag,samplenum),String);i
39、f handles.inputtype=0msgbox(No wave exist! Please choose a input type!);return;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);set(handles.foutt,String,1/f(I);
40、set(handles.foutfreq,String,f(I);Y=Y(1:(to-from+1)/2);plot(handles.plot1,f,2*sqrt(Y.*conj(Y);plot(handles.plot2,f,angle(Y);plot(handles.plot3,f,real(Y);plot(handles.plot4,f,imag(Y);plot(handles.plot5,f,abs(Y).2);xlabel(handles.plot1,freqency(Hz);xlabel(handles.plot2,freqency(Hz);xlabel(handles.plot3
41、,freqency(Hz);xlabel(handles.plot4,freqency(Hz);xlabel(handles.plot5,freqency(Hz);ylabel(handles.plot1,amplitude);ylabel(handles.plot2,phase(rad);ylabel(handles.plot3,real);ylabel(handles.plot4,Imaginary);ylabel(handles.plot5,power);function pointfrom_Callback(hObject, eventdata, handles)function po
42、intfrom_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction pointto_Callback(hObject, eventdata, handles)function pointto_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,Background
43、Color,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction play_Callback(hObject, eventdata, handles)Fs=str2double(get(findobj(Tag,samplerate),String);wavplay(handles.y,Fs);function scale_Callback(hObject, eventdata, handles)val=get(hObject,value);val=10(val*5+1)
44、;x=get(handles.xmove,Value)*str2double(get(handles.samplenum,String);axis(handles.time,x val+x min(handles.y) max(handles.y);function scale_CreateFcn(hObject, eventdata, handles)usewhitebg = 1;if usewhitebgset(hObject,BackgroundColor,.9 .9 .9);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBa
45、ckgroundColor);endfunction scale2_Callback(hObject, eventdata, handles)val=get(hObject,value);val=10(val*5+1);xlim(handles.plot1,0 val);xlim(handles.plot2,0 val);xlim(handles.plot3,0 val);xlim(handles.plot4,0 val);function scale2_CreateFcn(hObject, eventdata, handles)usewhitebg = 1;if usewhitebgset(
46、hObject,BackgroundColor,.9 .9 .9);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction channel_Callback(hObject, eventdata, handles)function channel_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,def
47、aultUicontrolBackgroundColor);endfunction wavetype_Callback(hObject, eventdata, handles)function wavetype_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,BackgroundColor,white);elseset(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);endfunction add_Callback(hObject, eventdata, handles)function add_DeleteFcn(hObject, eventdata, handles)function xmove_Callback(hObject, eventdata, handles)val=get(