1 简介
语音信号处理是与计算机科学,认知科学,人类智能,心理学,模式识别,语言学等有着密不可分关系的一门新兴的边缘科学.本系统设计能够解决现在许多种语音处理软件容量烦琐,运行困难诸多问题,可完成语音信号的傅里叶变换,变频,滤波,变幅,选取MATLAB全面操纵GUI界面设计,多种函数调用等.
2 部分代码
function varargout = soundanalyze2(varargin)
% SOUNDANALYZE2 MATLAB code for soundanalyze2.fig
% SOUNDANALYZE2, by itself, creates a new SOUNDANALYZE2 or raises the existing
% singleton*.
%
% H = SOUNDANALYZE2 returns the handle to a new SOUNDANALYZE2 or the handle to
% the existing singleton*.
%
% SOUNDANALYZE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SOUNDANALYZE2.M with the given input arguments.
%
% SOUNDANALYZE2('Property','Value',...) creates a new SOUNDANALYZE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before soundanalyze2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to soundanalyze2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help soundanalyze2
% Last Modified by GUIDE v2.5 02-Jul-2014 16:27:40
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @soundanalyze2_OpeningFcn, ...
'gui_OutputFcn', @soundanalyze2_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 soundanalyze2 is made visible.
function soundanalyze2_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 soundanalyze2 (see VARARGIN)
% % **_OpeningFcn函数是GUI在打开时的执行的命令,此时GUI界面还没有显示出来
% % 将背景图像载入Matlab
% % 如果图像不在当前目录,必须使用完整路径
% backgroundImage = importdata('backgound.jpg');
% % 选择坐标系
% axes(handles.axes_backgound);
% % 将图片添加到坐标系中,于是就成了背景了
% image(backgroundImage);
% % 将坐标系的坐标轴标签去掉
% axis off
handles.addednoise = 0;
% Choose default command line output for soundanalyze2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes soundanalyze2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = soundanalyze2_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 recordtime_input_Callback(hObject, eventdata, handles)
% hObject handle to recordtime_input (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_input as text
% str2double(get(hObject,'String')) returns contents of recordtime_input as a double
% --- Executes during object creation, after setting all properties.
function recordtime_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to recordtime_input (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 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)
recordtime = str2double(get(handles.recordtime_input,'string'));
sample_frequency = str2double(get(handles.edit_sample_frequency,'string'));
handles.Fs = sample_frequency;
recObj = audiorecorder(sample_frequency,16,1);
info_msgbox = msgbox('正在录音...请稍等','状态');
recordblocking(recObj, recordtime);
close(info_msgbox)
handles.Sample = getaudiodata(recObj);
handles.Sample = handles.Sample';
handles.inputtype = 1;
guidata(hObject,handles);
plot(handles.axes_time_waveform,...
0:1/sample_frequency:recordtime-1/sample_frequency,...
handles.Sample);
title(handles.axes_time_waveform,'原信号的时域波形')
xlabel(handles.axes_time_waveform,'time(s)');
ylabel(handles.axes_time_waveform,'amplitude');
set(handles.pushbutton_timanyalze,'enable','on')
set(handles.pushbutton_frequencyanyalze,'enable','on')
set(handles.pushbutton_noise_gensig,'enable','on')
set(handles.pushbutton_play,'enable','on')
% --- Executes on key press with focus on radiobutton_soundcard and none of its controls.
function radiobutton_soundcard_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to radiobutton_soundcard (see GCBO)
% eventdata structure with the following fields (see UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton_timanyalze.
function pushbutton_timanyalze_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_timanyalze (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
switch handles.inputtype
case 1
set(handles.period_value,'string',0)
set(handles.frequency_value,'string',0)
set(handles.amplitude_value,'string',0)
set(handles.phase_position_value,'string',0)
case 2
set(handles.period_value,'string',0)
set(handles.frequency_value,'string',0)
set(handles.amplitude_value,'string',0)
set(handles.phase_position_value,'string',0)
case 3
set(handles.period_value,'string',1/str2double(get(handles.edit_frequency,'string')))
set(handles.frequency_value,'string',str2double(get(handles.edit_frequency,'string')))
set(handles.amplitude_value,'string',str2double(get(handles.edit_amplitude,'string')))
set(handles.phase_position_value,'string',str2double(get(handles.edit_phase_position,'string')))
end
set(handles.peak_value,'string',max(handles.Sample))
set(handles.mean_value,'string',mean(handles.Sample))
set(handles.mean_square_error_value,'string',std(handles.Sample))
set(handles.variance_value,'string',var(handles.Sample))
function period_value_Callback(hObject, eventdata, handles)
% hObject handle to period_value (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 period_value as text
% str2double(get(hObject,'String')) returns contents of period_value as a double
% --- Executes during object creation, after setting all properties.
function period_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to period_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function frequency_value_Callback(hObject, eventdata, handles)
% hObject handle to frequency_value (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 frequency_value as text
% str2double(get(hObject,'String')) returns contents of frequency_value as a double
% --- Executes during object creation, after setting all properties.
function frequency_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to frequency_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function amplitude_value_Callback(hObject, eventdata, handles)
% hObject handle to amplitude_value (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 amplitude_value as text
% str2double(get(hObject,'String')) returns contents of amplitude_value as a double
% --- Executes during object creation, after setting all properties.
function amplitude_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to amplitude_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function phase_position_value_Callback(hObject, eventdata, handles)
% hObject handle to phase_position_value (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_position_value as text
% str2double(get(hObject,'String')) returns contents of phase_position_value as a double
% --- Executes during object creation, after setting all properties.
function phase_position_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to phase_position_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function peak_value_Callback(hObject, eventdata, handles)
% hObject handle to peak_value (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 peak_value as text
% str2double(get(hObject,'String')) returns contents of peak_value as a double
% --- Executes during object creation, after setting all properties.
function peak_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to peak_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mean_value_Callback(hObject, eventdata, handles)
% hObject handle to mean_value (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 mean_value as text
% str2double(get(hObject,'String')) returns contents of mean_value as a double
% --- Executes during object creation, after setting all properties.
function mean_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to mean_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mean_square_error_value_Callback(hObject, eventdata, handles)
% hObject handle to mean_square_error_value (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 mean_square_error_value as text
% str2double(get(hObject,'String')) returns contents of mean_square_error_value as a double
% --- Executes during object creation, after setting all properties.
function mean_square_error_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to mean_square_error_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function variance_value_Callback(hObject, eventdata, handles)
% hObject handle to variance_value (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 variance_value as text
% str2double(get(hObject,'String')) returns contents of variance_value as a double
% --- Executes during object creation, after setting all properties.
function variance_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to variance_value (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_frequencyanyalze.
function pushbutton_frequencyanyalze_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_frequencyanyalze (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
samplesize = max(size(handles.Sample));
f = -samplesize/2:1:samplesize/2-1;
plot(handles.axes_frequency_waveform,f,fftshift(abs(fft(handles.Sample))));
title(handles.axes_frequency_waveform,'原信号的频谱')
xlabel(handles.axes_frequency_waveform,'freqency(Hz)');
ylabel(handles.axes_frequency_waveform,'amplitude');
function edit_sample_frequency_Callback(hObject, eventdata, handles)
% hObject handle to edit_sample_frequency (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 edit_sample_frequency as text
% str2double(get(hObject,'String')) returns contents of edit_sample_frequency as a double
% --- Executes during object creation, after setting all properties.
function edit_sample_frequency_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_sample_frequency (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over radiobutton_soundcard.
function radiobutton_soundcard_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to radiobutton_soundcard (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function uipanel3_CreateFcn(hObject, eventdata, handles)
% hObject handle to uipanel3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes when selected object is changed in uipanel3.
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel3
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(hObject,'tag')
case 'radiobutton_soundcard'
set(handles.recordtime_input,'enable','on')
set(handles.startrecord,'enable','on')
set(handles.edit_filepath,'enable','off')
set(handles.pushbutton_selectfile,'enable','off')
set(handles.popupmenu_wavetype,'enable','off')
set(handles.edit_frequency,'enable','off')
set(handles.edit_amplitude,'enable','off')
set(handles.edit_phase_position,'enable','off')
set(handles.pushbutton_gensig,'enable','off')
guidata(hObject,handles);
case 'radiobutton_wavfile'
set(handles.edit_filepath,'enable','on')
set(handles.pushbutton_selectfile,'enable','on')
set(handles.recordtime_input,'enable','off')
set(handles.startrecord,'enable','off')
set(handles.popupmenu_wavetype,'enable','off')
set(handles.edit_frequency,'enable','off')
set(handles.edit_amplitude,'enable','off')
set(handles.edit_phase_position,'enable','off')
set(handles.pushbutton_gensig,'enable','off')
guidata(hObject,handles);
case 'radiobutton_signalgenerator'
set(handles.recordtime_input,'enable','off')
set(handles.startrecord,'enable','off')
set(handles.edit_filepath,'enable','off')
set(handles.pushbutton_selectfile,'enable','off')
set(handles.popupmenu_wavetype,'enable','on')
set(handles.edit_frequency,'enable','on')
set(handles.edit_amplitude,'enable','on')
set(handles.edit_phase_position,'enable','on')
set(handles.pushbutton_gensig,'enable','on')
guidata(hObject,handles);
end
function edit_filepath_Callback(hObject, eventdata, handles)
% hObject handle to edit_filepath (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 edit_filepath as text
% str2double(get(hObject,'String')) returns contents of edit_filepath as a double
% --- Executes during object creation, after setting all properties.
function edit_filepath_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_filepath (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_selectfile.
function pushbutton_selectfile_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_selectfile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.wav','wav音乐文件 (*.wav)'},'选择文件');
if isequal(filename,0) || isequal(pathname,0)
return
else
handles.Filepath = [pathname filename];
set(handles.edit_filepath,'string',handles.Filepath)
[handles.Sample,handles.Fs,handlse.Bbits] = wavread(handles.Filepath);
% subplot(handles.axes_time_waveform)
samplesize = size(handles.Sample);
if samplesize(2) > 1
handles.Sample = handles.Sample(:,1)';
end
handles.inputtype = 2;
guidata(hObject,handles);
plot(handles.axes_time_waveform,handles.Sample);
title(handles.axes_time_waveform,'原信号的时域波形')
set(handles.pushbutton_timanyalze,'enable','on')
set(handles.pushbutton_frequencyanyalze,'enable','on')
set(handles.pushbutton_noise_gensig,'enable','on')
set(handles.pushbutton_play,'enable','on')
end
function edit_frequency_Callback(hObject, eventdata, handles)
% hObject handle to edit_frequency (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 edit_frequency as text
% str2double(get(hObject,'String')) returns contents of edit_frequency as a double
% --- Executes during object creation, after setting all properties.
function edit_frequency_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_frequency (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_amplitude_Callback(hObject, eventdata, handles)
% hObject handle to edit_amplitude (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 edit_amplitude as text
% str2double(get(hObject,'String')) returns contents of edit_amplitude as a double
% --- Executes during object creation, after setting all properties.
function edit_amplitude_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_amplitude (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_phase_position_Callback(hObject, eventdata, handles)
% hObject handle to edit_phase_position (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 edit_phase_position as text
% str2double(get(hObject,'String')) returns contents of edit_phase_position as a double
% --- Executes during object creation, after setting all properties.
function edit_phase_position_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_phase_position (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_gensig.
function pushbutton_gensig_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_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(handles.edit_sample_frequency,'String'));
N = Fs;
frequency = str2double(get(handles.edit_frequency,'String'));
amp = str2double(get(handles.edit_amplitude,'String'));
phase = str2double(get(handles.edit_phase_position,'String'));
n = 0:N-1;
x = n/Fs;
soundtype=get(handles.popupmenu_wavetype,'Value');
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
handles.Sample = y;
handles.inputtype = 3;
guidata(hObject,handles);
plot(handles.axes_time_waveform,handles.Sample);
title(handles.axes_time_waveform,'原信号的时域波形')
set(handles.pushbutton_timanyalze,'enable','on')
set(handles.pushbutton_frequencyanyalze,'enable','on')
set(handles.pushbutton_noise_gensig,'enable','on')
set(handles.pushbutton_play,'enable','off')
% --- Executes on selection change in popupmenu_wavetype.
function popupmenu_wavetype_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_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 = cellstr(get(hObject,'String')) returns popupmenu_wavetype contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_wavetype
% --- Executes during object creation, after setting all properties.
function popupmenu_wavetype_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function slider_filter_fr_Callback(hObject, eventdata, handles)
% hObject handle to slider_filter_fr (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
filter_fr = get(hObject,'Value');
set(handles.edit_filter_fr,'string',filter_fr)
% --- Executes during object creation, after setting all properties.
function slider_filter_fr_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider_filter_fr (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(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit_noise_frequency_Callback(hObject, eventdata, handles)
% hObject handle to edit_noise_frequency (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 edit_noise_frequency as text
% str2double(get(hObject,'String')) returns contents of edit_noise_frequency as a double
% --- Executes during object creation, after setting all properties.
function edit_noise_frequency_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_noise_frequency (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_noise_amplitude_Callback(hObject, eventdata, handles)
% hObject handle to edit_noise_amplitude (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 edit_noise_amplitude as text
% str2double(get(hObject,'String')) returns contents of edit_noise_amplitude as a double
% --- Executes during object creation, after setting all properties.
function edit_noise_amplitude_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_noise_amplitude (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_noise_phase_position_Callback(hObject, eventdata, handles)
% hObject handle to edit_noise_phase_position (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 edit_noise_phase_position as text
% str2double(get(hObject,'String')) returns contents of edit_noise_phase_position as a double
% --- Executes during object creation, after setting all properties.
function edit_noise_phase_position_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_noise_phase_position (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_noise_gensig.
function pushbutton_noise_gensig_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_noise_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(handles.edit_sample_frequency,'String'));
N = Fs;
frequency = str2double(get(handles.edit_noise_frequency,'String'));
handles.Noise_frequency = frequency;
amp = str2double(get(handles.edit_noise_amplitude,'String'));
handles.noise_amplitude = amp;
phase = str2double(get(handles.edit_noise_phase_position,'String'));
n = 0:N-1;
x = n/Fs;
soundtype=get(handles.popupmenu_noise_wavetype,'Value');
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 length(handles.Sample) > length(y)
handles.Sample = handles.Sample(1:length(y));
else
y = y(1:length(handles.Sample));
end
handles.Sample_addnoise = y + handles.Sample;
handles.addednoise = 1;
guidata(hObject,handles);
plot(handles.axes_noise,handles.Sample_addnoise);
title(handles.axes_noise,'叠加噪声后的时域波形')
samplesize = max(size(handles.Sample_addnoise));
f = -samplesize/2:1:samplesize/2-1;
plot(handles.axes_noise_frequency_waveform,f,fftshift(abs(fft(handles.Sample_addnoise))));
title(handles.axes_noise_frequency_waveform,'叠加噪声后的频谱')
xlabel(handles.axes_frequency_waveform,'freqency(Hz)');
ylabel(handles.axes_frequency_waveform,'amplitude');
set(handles.pushbutton_filter,'enable','on')
set(handles.pushbutton_play_addnoise,'enable','on')
% --- Executes on selection change in popupmenu_noise_wavetype.
function popupmenu_noise_wavetype_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_noise_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 = cellstr(get(hObject,'String')) returns popupmenu_noise_wavetype contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_noise_wavetype
% --- Executes during object creation, after setting all properties.
function popupmenu_noise_wavetype_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_noise_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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_filter.
function pushbutton_filter_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_filter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%设计高通滤波器
filter_fr = str2double(get(handles.edit_filter_fr,'String'));
filter_order = str2double(get(handles.edit_filter_order,'String'));
filter_type = get(handles.popupmenu_filter_type,'Value');
switch filter_type
case 1
filter_type_ = 'high';
case 2
filter_type_ = 'low';
case 3
msgbox('带通暂时不可用');
% filter_type_ = 'stop';
end
[a,b]=butter(filter_order,filter_fr,filter_type_);
%绘制叠加函数S经过低通滤波器以后的时域图形
sF=filter(a,b,handles.Sample_addnoise);
plot(handles.axes_noise,sF);
title(handles.axes_noise,'滤波后的时域波形')
samplesize = max(size(handles.Sample_addnoise));
f = -samplesize/2:1:samplesize/2-1;
plot(handles.axes_noise_frequency_waveform,f,fftshift(abs(fft(handles.Sample_addnoise))));
title(handles.axes_noise_frequency_waveform,'滤波后的频谱')
xlabel(handles.axes_noise_frequency_waveform,'freqency(Hz)');
ylabel(handles.axes_noise_frequency_waveform,'amplitude');
function edit_filter_fr_Callback(hObject, eventdata, handles)
% hObject handle to edit_filter_fr (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 edit_filter_fr as text
% str2double(get(hObject,'String')) returns contents of edit_filter_fr as a double
% --- Executes during object creation, after setting all properties.
function edit_filter_fr_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_filter_fr (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_filter_order_Callback(hObject, eventdata, handles)
% hObject handle to edit_filter_order (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 edit_filter_order as text
% str2double(get(hObject,'String')) returns contents of edit_filter_order as a double
% --- Executes during object creation, after setting all properties.
function edit_filter_order_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_filter_order (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu_filter_type.
function popupmenu_filter_type_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_filter_type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_filter_type contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_filter_type
% --- Executes during object creation, after setting all properties.
function popupmenu_filter_type_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_filter_type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_play.
function pushbutton_play_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
p = audioplayer(handles.Sample', handles.Fs);
msg = msgbox('正在播放...');
playblocking(p);
close(msg)
% --- Executes on button press in pushbutton_play_addnoise.
function pushbutton_play_addnoise_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_play_addnoise (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.Noise_frequency < 200 || handles.Noise_frequency > 5000 || handles.inputtype == 3 || handles.noise_amplitude > 3
info_msgbox = msgbox('频率过低或声音过高,有害健康,禁止播放!','警告');
else
p = audioplayer(handles.Sample_addnoise, handles.Fs);
msg = msgbox('正在播放...');
playblocking(p);
close(msg)
end
3 仿真结果
4 参考文献
[1]廖正和. 基于MATLAB的语音信号分析与处理系统的研究[J]. 数字通信世界, 2015(10).
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。