数字信号处理实验报告8

更新时间:2024-05-15 17:25:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

数字信号处理实验报告

实验八 音频频谱分析仪设计与实现

姓 名:陈 梦 君

学 号:2010082131

指 导 老 师:陈 立 伟

日 期:2012年11月7日

一 实验题目:音频频谱分析仪设计与实现 二 实验原理:

MATLAB是一个数据分析和处理功能十分强大的工程实用软件,它的数据采集工具箱为实现数据的输入和输出提供了十分方便的函数和命令。本实验将给出基于声卡与MATLAB的声音信号频谱分析仪的设计原理与实现方法,功能包括:

(1) 音频信号信号输入,从声卡输入、从WAV文件输入、从标准信号发生器输入;

(2) 信号波形分析,包括幅值、频率、周期、相位的估计,以及统计量峰值、均值、均方值和方差的计算;

(3) 信号频谱分析,频率、周期的估计,图形显示幅值谱、相位谱、实频谱、虚频谱和功率谱的曲线。

1. 信号频率、幅值和相位估计 (1)频率(周期)检测

对周期信号来说,可以用时域波形分析来确定信号的周期,也就是计算相邻的两个信号波峰的时间差、或过零点的时间差。这里采用过零点(ti)的时间差T(周期)。频率即为

,由于能够求得多个T值(ti有多个),故采用它们的平均值作为周期

的估计值。

(2)幅值检测

在一个周期内,求出信号最大值与最小值的差的一半,即A?(ymax?ymin)/2,同时,也会求出多个A值,但第1个A值对应的最大值和最小值不是在一个周期内搜索得到的,故以除第1个以外的A值的平均作为幅值的估计值。

(3)相位检测

采用过零法,即通过判断与同频零相位信号过零点时刻,计算其时间差,然后换成相应的相位差。

,{x}表示x的小数部分,同样,以?的平均值

作为相位的估计值。

频率、幅值和相位估计的流程如图 1所示。

其中表示第n个过零点,yi为第i个采样点的值,Fs为采样频率。 2. 数字信号统计量估计 (1) 峰值P的估计

在样本数据x中找出最大值与最小值,其差值为双峰值,双峰值的一半即为峰值。

(2) 均值估计

式中,N为样本容量,下同。

图 1 频率、幅值和相位估计的流程图

(3) 均方值估计

(4) 方差估计

时域分析只能反映信号的幅值随时间的变化情况,除单频率分量的简单波形外,很难明确提示信号的频率组成和各频率分量大小,而频谱分析能很好的解决此问题。由于从频域能获得的主要是频率信息,所以本节主要介绍频率(周期)的估计与频谱图的生成。

3. DFT与FFT

对于给定的时域信号y,可以通过Fourier变换得到频域信息Y。Y可按下式计算

式中,N为样本容量,

为采样间隔。

采样信号的频谱是一个连续的频谱,不可能计算出所有的点的值,故采用离散Fourier变换(DFT),即

式中,

。但上式的计算效率很低,因为有大量的指数(等价于三角函数)

运算,故实际中多采用快速Fourier变换(FFT)。其原理即是将重复的三角函数算计的中间结果保存起来,以减少重复三角函数计算带来的时间浪费。由于三角函数计算的重复量相当大,故FFT能极大地提高运算效率。

4. 频率、周期的估计 对于,如果当时,取最大值,则为频率的估计值,由于采样间隔的误差,

周期

也存在误差,其误差最大为

从原理上可以看出,如果在标准信号中混有噪声,用上述方法仍能够精确地估计出原标准信号的频率和周期,这个将在下一章做出验证。

5. 频谱图

为了直观地表示信号的频率特性,工程上常常将Fourier变换的结果用图形的方式表示,即频谱图。

以频率f为横坐标,为纵坐标,可以得到幅值谱;

以频率f为横坐标,以频率f为横坐标,

为纵坐标,可以得到相位谱; 为纵坐标,可以得到实频谱;

以频率f为横坐标,为纵坐标,可以得到虚频谱。 根据采样定理,只有频率不超过的信号才能被正确采集,即Fourier变换的结果中频率大于的部分是不正确的部分,故不在频谱图中显示。即横坐标

6.模块划分

模块化就是把程序划分成独立命名且可独立访问的模块,每个模块完成一个子功能,把这些模块集成起来构成一个整体,可以完成指定的功能满足用户需求。根据人类解决一般问题的经验,如果一个问题由两个问题组合而成,那么它的复杂程度大于分别考虑每个问题时的复杂程度之和,也就是说把复杂的问题分解成许多容易解决的小问题,原来的问题也就容易解决了。这就是模块化的根据。

在模块划分时应遵循如下规则:改进软件结构提高模块独立性;模块规模应该适中;深度、宽度、扇出和扇入都应适当;模块的作用域应该在控制域之内;力争降低模块接口的复杂程度;设计单入口单出口的模块;模块功能应该可以预测。

本着上述的启发式规则,对软件进行如图 2所示的模块划分。

图 2频谱分析仪的模块划分

7.界面设计

为了实现预期的功能,根据所给参考界面,设计出如图 3所示的界面。 最上面的部分为标题区,用于显示软件标题等信息,不具人机交互功能。 再往下是信号输入区,包含3种输入方式,考虑到WAV文件可能是多声道,故提供了声道选择的界面,因为每次只能对单个声道进行分析。在信号发生器中加入了混迭选项,从而可以将产生的信号与原有的信号进行混迭。界面应该具有:只有当每个单选框被选中时才允许使用对应的输入框、按钮等;采样点数输入框在声卡与WAV文件的输入方式下作为输出,在信号发生器的输入方式下作为输入。

再往下是分析区。对于WAV文件及录音的信号,有时只对其中一部分信号进行分析,故提供了分析对象范围设定的界面。另外就是时域分析与频域分析的按钮,该软件的核心代码都在这两个按钮的回调函数中。

图3 声音信号频谱分析仪

分析区下面是分析结果区,用于显示波形基本参数与统计量的计算结果。 分析结果区的下面是波形显示区,用于显示时域波形,在录音结束、打开WAV文件成功或者信号发生器生成波形时会更新显示。

右边为频谱图显示区,用于显示各种频谱的谱线,在点击频域分析后会更新显示。

8.输入模块的实现

采样频率Fs与采样点数N是声音信号输入时共同需要作用的参数,故将其独立出来。下面为别介绍三种输入方式的实现。 (1) 声卡输入

这里声卡输入是指由麦克风录音得到的声音信号的输入,MATLAB提供了wavrecord函数,该函数能够实现读取麦克风录音信号。以下是“开始录音”按钮的回调函数内容。

%首先获得设定的Fs值

Fs=str2double(get(findobj('Tag','samplerate'),'String')); %根据设定的录音时长进行录音,将其存入handles.y中

handles.y=wavrecord(str2double(get(handles.recordtime,'String'))*Fs, Fs,'int16'); %保存handles结构体,使得handles.y在别的函数中也能使用 guidata(hObject,handles); %在波形显示区绘出波形 plot(handles.time,handles.y); title('WAVE');

%将所采到的点的数量输出在“采样点数”中 ysize=size(handles.y)

set(handles.samplenum,'String',num2str(ysize(1))); (2) WAV文件输入

MATLAB提供了wavread函数,该函数能够方便的打开并读取WAV文件中的声音信息,并且同时读取所有声道。下面是“打开文件”按钮回调函数的部分代码。其它代码与声卡输入的类似。

%从WAV文件中读取的声音信息并临时存放到temp变量中 temp = wavread(get(findobj('Tag','filename'),'String')); %获得所选择的声道

channel=str2double(get(handles.channel,'String')); %将指定声道的信息存放到handles.y中 handles.y=temp(:,channel); (3) 信号发生器

MATLAB有产生标准信号的函数,如sawtooth能够产生三角波或钜齿波,首先利用get函数获得波形soundtype,频率frequency,幅值amp和相位phase,然后是以下代码。

switch soundtype

case 1 %标准正弦波

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'); end

if get(handles.add,'Value')==0.0

handles.y=y; %若没有勾选上“混迭”,则将生成的波形赋给handles.y else %否则将生成的波形与原有波形叠加 handles.y=handles.y+y; end

(4) 时域分析

MATLAB提供了mean,std函数,能够方便地计算均值、标准差。下面是过零检测之后的代码,其中T为过零检测得到的周期(向量),amp为过零检测得到的幅值(向量),n为过零点数。

freq=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)./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); %from,to即是界面中的“从第from点到第to点”

%计算并输出均值

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(from:to))^2); (5) 频域分析

频域分析需要作Fourier变换,MATLAB提供了fft函数,能够方便地实现快速Fourier变换算法。以下代码省去了从界面中获得from、to、Fs的部分,也省去了绘图后设置横、纵坐标轴的名称的部分。

%首先提取出待分析的样本,将其存入sample中 sample=handles.y(from:to);

%生成离散化的频率点,以采样频率作为离散化的间隔 f=linspace(0,Fs/2,(to-from+1)/2);

%对样本作快速Fourier变换,变换结果存入Y中 Y=fft(sample,to-from+1);

[C,I]=max(abs(Y)); %获得幅值最大的点及其所对应的下标值I %则f(I)为最大的幅值所对应的频率,即信号频率的估计值 set(handles.foutt,'String',1/f(I)); %计算并输出周期的估计值 set(handles.foutfreq,'String',f(I)); %输出频率的估计值 Y=Y(1:(to-from+1)/2); %为与f对应,只取Y的前半部分 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); %绘制功率谱曲线

三 实验内容

参考给出的代码,查阅相关资料,构建交互界面,设计一个音频频谱分析仪,实现相应的功能。

实验程序如下:

function varargout = untitled13(varargin) % UNTITLED13 M-file for untitled13.fig

% UNTITLED13, by itself, creates a new UNTITLED13 or raises the existing % singleton*. %

% H = UNTITLED13 returns the handle to a new UNTITLED13 or the handle to % the existing singleton*. %

% UNTITLED13('CALLBACK',hObject,eventData,handles,...) calls the local

% function named CALLBACK in UNTITLED13.M with the given input arguments. %

% UNTITLED13('Property','Value',...) creates a new UNTITLED13 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled13_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled13_OpeningFcn via varargin. %

% *See GUI Options on GUIDE's Tools menu. Choose \% instance to run (singleton)\%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help untitled13

% Last Modified by GUIDE v2.5 07-Nov-2012 22:45:03

% Begin initialization code - DO NOT EDIT gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @untitled13_OpeningFcn, ... 'gui_OutputFcn', @untitled13_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1}); end

if nargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else

gui_mainfcn(gui_State, varargin{:}); end

% End initialization code - DO NOT EDIT

% --- Executes just before untitled13 is made visible.

function untitled13_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 untitled13 (see VARARGIN)

% Choose default command line output for untitled13 handles.inputtype=0;

xlabel(handles.plot1,'frequency(Hz)'); xlabel(handles.plot2,'frequency(Hz)'); xlabel(handles.plot3,'frequency(Hz)'); xlabel(handles.plot4,'frequency(Hz)'); xlabel(handles.plot5,'frequency(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;

% Update handles structure guidata(hObject, handles);

% UIWAIT makes untitled13 wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = untitled13_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure varargout{1} = handles.output;

function samplerate_Callback(hObject, eventdata, handles) % hObject handle to samplerate (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of samplerate as text

% str2double(get(hObject,'String')) returns contents of samplerate as a double

% --- Executes during object creation, after setting all properties. function samplerate_CreateFcn(hObject, eventdata, handles) % hObject handle to samplerate (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text

% str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

% See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function soundcard_Callback(hObject, eventdata, handles) % hObject handle to soundcard (see GCBO)

% eventdata reserved - to 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,'enable','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.channe,'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');

% Hints: get(hObject,'String') returns contents of soundcard as text

% str2double(get(hObject,'String')) returns contents of soundcard as a double

% --- Executes during object creation, after setting all properties. function soundcard_CreateFcn(hObject, eventdata, handles) % hObject handle to soundcard (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function filename_Callback(hObject, eventdata, handles) % hObject handle to filename (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of filename as text

% str2double(get(hObject,'String')) returns contents of filename as a double

% --- Executes during object creation, after setting all properties. function filename_CreateFcn(hObject, eventdata, handles) % hObject handle to filename (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function recordtime_Callback(hObject, eventdata, handles) % hObject handle to recordtime (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of recordtime as text

% str2double(get(hObject,'String')) returns contents of recordtime as a double

% --- Executes during object creation, after setting all properties. function recordtime_CreateFcn(hObject, eventdata, handles) % hObject handle to recordtime (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function freq_Callback(hObject, eventdata, handles) % hObject handle to freq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of freq as text

% str2double(get(hObject,'String')) returns contents of freq as a double

% --- Executes during object creation, after setting all properties. function freq_CreateFcn(hObject, eventdata, handles) % hObject handle to freq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function amp_Callback(hObject, eventdata, handles) % hObject handle to amp (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of amp as text

% str2double(get(hObject,'String')) returns contents of amp as a double

% --- Executes during object creation, after setting all properties. function amp_CreateFcn(hObject, eventdata, handles) % hObject handle to amp (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function phase_Callback(hObject, eventdata, handles) % hObject handle to phase (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of phase as text

% str2double(get(hObject,'String')) returns contents of phase as a double

% --- Executes during object creation, after setting all properties. function phase_CreateFcn(hObject, eventdata, handles) % hObject handle to phase (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER.

if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on button press in radiobutton1.

function radiobutton1_Callback(hObject, eventdata, handles) % hObject handle to radiobutton1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1

% --- Executes on button press in generator.

function generator_Callback(hObject, eventdata, handles) % hObject handle to generator (see GCBO)

% eventdata reserved - to 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=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 toggle state of generator

% --- Executes on button press in WAVfile.

function WAVfile_Callback(hObject, eventdata, handles) % hObject handle to WAVfile (see GCBO)

% eventdata reserved - to be 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(handles.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 startrecord.

function startrecord_Callback(hObject, eventdata, handles) % hObject handle to startrecord (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles 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(handles.samplenum,'String',num2str(ysize(1)));

% --- Executes on button press in gensig.

function gensig_Callback(hObject, eventdata, handles) % hObject handle to gensig (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) 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=str2double(get(handles.amp,'String')); phase=str2double(get(handles.phase,'String')); switch soundtype case 1

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'); end

if get(handles.add,'Value')==0.0 handles.y=y; else

handles.y=handles.y+y; end

handles.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'))]);

% --- Executes on button press in fileopen.

function fileopen_Callback(hObject, eventdata, handles) % hObject handle to fileopen (see GCBO)

% eventdata reserved - to 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.channel,'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)));

% --- Executes on button press in add.

function add_Callback(hObject, eventdata, handles) % hObject handle to add (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of add

% --- Executes on selection change in wavetype.

function wavetype_Callback(hObject, eventdata, handles) % hObject handle to wavetype (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns wavetype contents as cell array % contents{get(hObject,'Value')} returns selected item from wavetype

% --- Executes during object creation, after setting all properties. function wavetype_CreateFcn(hObject, eventdata, handles) % hObject handle to wavetype (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER.

if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on button press in analyseall.

function analyseall_Callback(hObject, eventdata, handles) % hObject handle to analyseall (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if get(hObject,'Value')==0.0

set(handles.pointfrom,'Enable','on'); set(handles.pointto,'Enable','on'); else

set(handles.pointfrom,'String','1','Enable','off');

set(handles.pointto,'String',get(handles.samplenum,'String'),'Enable','off'); end

% Hint: get(hObject,'Value') returns toggle state of analyseall

function pointfrom_Callback(hObject, eventdata, handles) % hObject handle to pointfrom (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of pointfrom as text

% str2double(get(hObject,'String')) returns contents of pointfrom as a double

% --- Executes during object creation, after setting all properties. function pointfrom_CreateFcn(hObject, eventdata, handles) % hObject handle to pointfrom (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function pointto_Callback(hObject, eventdata, handles) % hObject handle to pointto (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of pointto as text

% str2double(get(hObject,'String')) returns contents of pointto as a double

% --- Executes during object creation, after setting all properties. function pointto_CreateFcn(hObject, eventdata, handles) % hObject handle to pointto (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on button press in freqanalyse.

function freqanalyse_Callback(hObject, eventdata, handles) % hObject handle to freqanalyse (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs=str2double(get(findobj('Tag','samplerate'),'String')); N=str2double(get(findobj('Tag','samplenum'),'String')); if handles.inputtype==0

msgbox('No wave exist! Please choose a input type!'); return; end

from=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)); 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,'frequency(Hz)'); xlabel(handles.plot2,'frequency(Hz)'); xlabel(handles.plot3,'frequency(Hz)'); xlabel(handles.plot4,'frequency(Hz)'); xlabel(handles.plot5,'frequency(Hz)'); ylabel(handles.plot1,'amplitude'); ylabel(handles.plot2,'phase(rad)'); ylabel(handles.plot3,'real');

ylabel(handles.plot4,'Imaginary'); ylabel(handles.plot5,'power');

% --- Executes on button press in analysetime.

function analysetime_Callback(hObject, eventdata, handles) % hObject handle to analysetime (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs=str2double(get(findobj('Tag','samplerate'),'String')); N=str2double(get(findobj('Tag','samplenum'),'String')); if handles.inputtype==0

msgbox('No wave exist!Please choose a input type!'); return; end n=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<1|to-from<5;

msgbox('Error range!'); return; end

for i=from+2:to-1;

if handles.y(i-1)<0 & handles.y(i-2)<0 &handles.y(i)>=0 & handles.y(i+1)>0 if handles.y(i)==0 ti(n)=i; else

ti(n)=i-handles.y(i)/(handles.y(i)-handles.y(i-1)); end

amp(n)=(ymax-ymin)/2; ymax=0; ymin=0; n=n+1; else

if ymax

if ymin>handles.y(i) ymin=handles.y(i); end end end n=n-1; for i=1:n-1

T(i)=ti(i+1)-ti(i); end

freq=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)./T+floor((ti(1:n-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(from:to))^2);

function outt_Callback(hObject, eventdata, handles) % hObject handle to outt (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outt as text

% str2double(get(hObject,'String')) returns contents of outt as a double

% --- Executes during object creation, after setting all properties. function outt_CreateFcn(hObject, eventdata, handles) % hObject handle to outt (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outfreq_Callback(hObject, eventdata, handles) % hObject handle to outfreq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outfreq as text

% str2double(get(hObject,'String')) returns contents of outfreq as a double

% --- Executes during object creation, after setting all properties. function outfreq_CreateFcn(hObject, eventdata, handles) % hObject handle to outfreq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outamp_Callback(hObject, eventdata, handles)

% hObject handle to outamp (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outamp as text

% str2double(get(hObject,'String')) returns contents of outamp as a double

% --- Executes during object creation, after setting all properties. function outamp_CreateFcn(hObject, eventdata, handles) % hObject handle to outamp (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outphase_Callback(hObject, eventdata, handles) % hObject handle to outphase (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outphase as text

% str2double(get(hObject,'String')) returns contents of outphase as a double

% --- Executes during object creation, after setting all properties. function outphase_CreateFcn(hObject, eventdata, handles) % hObject handle to outphase (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outmean_Callback(hObject, eventdata, handles) % hObject handle to outmean (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outmean as text

% str2double(get(hObject,'String')) returns contents of outmean as a double

% --- Executes during object creation, after setting all properties. function outmean_CreateFcn(hObject, eventdata, handles) % hObject handle to outmean (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outs_Callback(hObject, eventdata, handles) % hObject handle to outs (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outs as text

% str2double(get(hObject,'String')) returns contents of outs as a double

% --- Executes during object creation, after setting all properties. function outs_CreateFcn(hObject, eventdata, handles) % hObject handle to outs (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outmeansquare_Callback(hObject, eventdata, handles) % hObject handle to outmeansquare (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outmeansquare as text

% str2double(get(hObject,'String')) returns contents of outmeansquare as a double

% --- Executes during object creation, after setting all properties. function outmeansquare_CreateFcn(hObject, eventdata, handles)

% hObject handle to outmeansquare (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function outpeak_Callback(hObject, eventdata, handles) % hObject handle to outpeak (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of outpeak as text

% str2double(get(hObject,'String')) returns contents of outpeak as a double

% --- Executes during object creation, after setting all properties. function outpeak_CreateFcn(hObject, eventdata, handles) % hObject handle to outpeak (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function foutt_Callback(hObject, eventdata, handles) % hObject handle to foutt (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of foutt as text

% str2double(get(hObject,'String')) returns contents of foutt as a double

% --- Executes during object creation, after setting all properties. function foutt_CreateFcn(hObject, eventdata, handles) % hObject handle to foutt (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

% See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function foutfreq_Callback(hObject, eventdata, handles) % hObject handle to foutfreq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of foutfreq as text

% str2double(get(hObject,'String')) returns contents of foutfreq as a double

% --- Executes during object creation, after setting all properties. function foutfreq_CreateFcn(hObject, eventdata, handles) % hObject handle to foutfreq (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on button press in play.

function play_Callback(hObject, eventdata, handles) % hObject handle to play (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% --- Executes on slider movement.

function slider1_Callback(hObject, eventdata, handles) % hObject handle to slider1 (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^(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 creation, after setting all properties.

function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (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, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg

set(hObject,'BackgroundColor',[.9 .9 .9]); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on slider movement.

function xmove_Callback(hObject, eventdata, handles) % hObject handle to xmove (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) val=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(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 creation, after setting all properties. function xmove_CreateFcn(hObject, eventdata, handles) % hObject handle to xmove (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, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg

set(hObject,'BackgroundColor',[.9 .9 .9]); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on slider movement.

function scala2_Callback(hObject, eventdata, handles) % hObject handle to scala2 (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^(val*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. function scala2_CreateFcn(hObject, eventdata, handles) % hObject handle to scala2 (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, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg

set(hObject,'BackgroundColor',[.9 .9 .9]); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function chnannel_Callback(hObject, eventdata, handles) % hObject handle to chnannel (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of chnannel as text

% str2double(get(hObject,'String')) returns contents of chnannel as a double

% --- Executes during object creation, after setting all properties. function chnannel_CreateFcn(hObject, eventdata, handles) % hObject handle to chnannel (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc

set(hObject,'BackgroundColor','white'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

输入正弦波

输入方波

录入声音

产生白噪声

本文来源:https://www.bwwdw.com/article/z2s7.html

Top