1、数字信号处理实验八实验八 音频频谱分析仪设计与实现1、实验原理MATLAB是一个数据信息和处理功能十分强大的工程实用软件,其数据采集工具箱为实现数据的输入和输出提供了十分方便的函数和命令。本实验要求基于声卡与MATLAB实现音频信号频谱分析仪的设计原理与实现,功能包括:(1)信号的输入,从声卡输入、从WAV文件输入、从标准信号发生器输入;(2)信号的波形分析,包括幅值、频率、周期、相位的估计,以及统计量峰值、均值、均方值和方差的计算;(3)信号频谱分析,频率、周期的估计,图形显示幅值谱、相位谱、实频谱、虚频谱和功率谱的曲线。1、频率(周期)检测对周期信号来说,可以用时域波形分析来确定信号的周期
2、,也就是计算相邻的两个信号波峰的时间差或过零点的时间差。这里采用过零点(ti)的时间差T(周期)。频率即为f=1/T,由于能够求得多个T值(ti有多个),故采用它们的平均值作为周期的估计值。2、幅值检测在一个周期内,求出信号的最大值ymax与最小值ymin的差的一半,即A=(ymax-ymin)/2,除以第一个以外的A值的平均作为幅值的估计值。3、相位检测采用过零法,即通过判断与同频零相位信号过零点时刻,计算其时间差,然后换成相应的相位差。Fi=2pi(1-ti/T),同样,以fi的平均值作为相位的估计值。4、数字信号统计量估计(1)峰值P的估计在样本数据x中找出最大值与最小值,其差值为双峰值
3、,双峰值的一半即为峰值。(2)均值估计式中,N为样本容量,下同。(3)均方值估计(4)方差估计5、频谱分析原理是与分析只能反映信号的幅值随时间的变化情况,除单位频率分量的简单波形外,很难明确提示信号的频率组成和个频率分量的大小,而频谱分析能很好的解决此问题。(1)DFT与FFT对于给定的时域信号y,可以通过Foutier变换得到频域信息Y。Y可按下式计算式中,N为样本容量,t=1/Fs为采样间隔。采样信号的频谱是一个连续的频谱,不可能计算出所有的点的值,故采用离散傅立叶变换(DFT),即式中,f=Fs/N。但上式的计算效率很低,因为有大量的指数(等价于三角函数)运算,故实际中多采用快速傅立叶变
4、换(FFT)。其原理是将重复的三角函数计算的中间结果保存起来,以减少重复三角函数计算带来的时间浪费。由于三角函数计算的重复量相当大,故FFT能极大地提高运算效率。(2)频率、周期的估计对于Y(kf),如果当kf=f时,Y(kf)取最大值,则f为频率的估计量,由于采样间隔的误差,f也存在误差,其误差最大值为f/2。周期T=1/f。从原理上可以看出,如果在标准信号中混有噪声,用上述方法仍能够精确地估计出原标准信号的频率和周期。(3)频谱图为了直观的表示信号的频率特性,工程上常常将傅立叶变换的结果用于图形的方式表示,即频谱图。以频谱f为横坐标,|Y(f)|为纵坐标,可以得到幅值谱;以频谱f为横坐标,
5、argY(f)为纵坐标,可以得到相位谱;以频谱f为横坐标,ReY(f)为纵坐标,可以得到实频谱;以频谱f为横坐标,ImY(f)为纵坐标,可以得到虚频谱;根据采样定理,只有频率不超过Fs/2的信号才能被正确采集,即傅立叶变换的结果中频率大于Fs/2的部分是不正确的部分,故不在频谱图中显示。即横坐标f属于0,Fs/2。2、实验内容请参考书中所给的界面和代码,查阅相关资料,构建交互界面,设计一个音频频谱分析仪,实现相应的功能。3、实验程序及结果(1)分析仪界面:输入界面:(2)声卡输入方式相关程序及结果:% - Executes on button press in soundcard.functi
6、on soundcard_Callback(hObject, eventdata, handles)% hObject handle pointto soundcard (see GCBO)% eventdata reserved - pointto be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(findobj(Tag,recordtime),enable,on);h=findobj(Tag,filename);set(h,enabl
7、e,off);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(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.startrecor
8、d,enable,on);% Hint: get(hObject,Value) returns toggle state of soundcard% - Executes on button press in startrecord.function startrecord_Callback(hObject, eventdata, handles)% hObject handle pointto startrecord (see GCBO)% eventdata reserved - pointto be defined in a future version of MATLAB% handl
9、es structure with handles and user data (see GUIDATA)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(handl
10、es.samplenum,String,num2str(ysize(1);滚动条程序:% - Executes on slider movement.function scale_Callback(hObject, eventdata, handles)% hObject handle to scale (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)val=get(hOb
11、ject,value);val=10(val*5+1);x=get(handles.xmove,Value)*str2double(get(handles.samplenum,String);axis(handles.time,x val+x min(handles.y) max(handles.y);% Hints: get(hObject,Value) returns position of slider% get(hObject,Min) and get(hObject,Max) to determine range of slider% - Executes during object
12、 creation, after setting all properties.function scale_CreateFcn(hObject, eventdata, handles)% hObject handle to scale (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: slider controls usually ha
13、ve a light gray background.if isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor) set(hObject,BackgroundColor,.9 .9 .9);end% - Executes on slider movement.function xmove_Callback(hObject, eventdata, handles)% hObject handle to xmove (see GCBO)% eventdata reserved - to be def
14、ined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% get(hObject,Min) and get(hObject,Max) to determine range of sliderval=get(handles.scale,value);val=10(val*5+1);x=get(hObject,Value)*str2double(get(handles.samplenum,String);axis(handles.time,x val+x min(h
15、andles.y) max(handles.y);% Hints: get(hObject,Value) returns position of slider% get(hObject,Min) and get(hObject,Max) to determine range of slider% - Executes during object creation, after setting all properties.function xmove_CreateFcn(hObject, eventdata, handles)% hObject handle to xmove (see GCB
16、O)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: slider controls usually have a light gray background.if isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor) set(hObject,Backgrou
17、ndColor,.9 .9 .9);end% - Executes on slider movement.function scale2_Callback(hObject, eventdata, handles)% hObject handle to scale2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)val=get(hObject,value);val=10(v
18、al*5+1);xlim(handles.plot1,0 val);xlim(handles.plot2,0 val);xlim(handles.plot3,0 val);xlim(handles.plot4,0 val);% Hints: get(hObject,Value) returns position of slider% get(hObject,Min) and get(hObject,Max) to determine range of slider% - Executes during object creation, after setting all properties.
19、function scale2_CreateFcn(hObject, eventdata, handles)% hObject handle to scale2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: slider controls usually have a light gray background.if isequal(
20、get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor) set(hObject,BackgroundColor,.9 .9 .9);end(3)WAV文件输入方式相关程序及结果:% - Executes on button press in WAVfile.function WAVfile_Callback(hObject, eventdata, handles)% hObject handle pointto WAVfile (see GCBO)% eventdata reserved - pointto be
21、 defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)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(han
22、dles.channel,enable,on);set(handles.fileopen,enable,on);set(handles.gensig,enable,off);set(handles.wavetype,enable,off);set(handles.add,enable,off);set(handles.startrecord,enable,off);% Hint: get(hObject,Value) returns toggle state of WAVfile% - Executes on button press in fileopen.function fileopen
23、_Callback(hObject, eventdata, handles)% hObject handle pointto fileopen (see GCBO)% eventdata reserved - pointto be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) temp = wavread(get(findobj(Tag,filename),String);channel=str2double(get(handles.channe
24、l,String);handles.y=temp(:,channel);handles.inputtype=2;guidata(hObject,handles);plot(handles.time,handles.y);title(WAVE);ysize=size(handles.y)set(handles.samplenum,String,num2str(ysize(1);(4)信号发生器输入方式相关程序及结果:% - Executes on button press in generator.function generator_Callback(hObject, eventdata, h
25、andles)% hObject handle pointto generator (see GCBO)% eventdata reserved - pointto be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)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=
26、findobj(Tag,phase);set(h,enable,on);set(findobj(Tag,recordtime),enable,off);set(handles.channel,enable,off);set(handles.fileopen,enable,off);set(handles.gensig,enable,on);set(handles.wavetype,enable,on);set(handles.add,enable,on);set(handles.startrecord,enable,off);% Hint: get(hObject,Value) returns
27、 toggle state of generator% - Executes on button press in gensig.function gensig_Callback(hObject, eventdata, handles)% hObject handle pointto gensig (see GCBO)% eventdata reserved - pointto be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)Fs=str2do
28、uble(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=str2double(get(handles.amp,String);phase=str2double(get(handles.phase,String);switch soundtype case 1
29、y=amp*sin(2*pi*x*frequency+phase); case 2 y=amp*sign(sin(2*pi*x*frequency+phase); case 3 y=amp*sawtooth(2*pi*x*frequency+phase,0.5); case 4 y=amp*sawtooth(2*pi*x*frequency+phase); case 5 y=amp*(2*rand(size(x)-1); otherwise errordlg(Illegal wave type,Choose errer);endif get(handles.add,Value)=0.0 han
30、dles.y=y;else handles.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);正弦波:方波:三角波:钜齿波:白噪声:分析界面:(5)时域分析相关程序及结果:% - Executes on button press in timeanalyse.function timeanalyse_Callback(hObject, eventdata, handles)% hObject handle pointto timeanalyse (see
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1