“#”确认键的作用是将前面拨过的号码进行确认保留,意味着此时连续拨号音信 号的存储单元 NUM 中的信号即为最后用于识别的连续拨号音 DTMF 信号, 并在显示窗口中显示“#”号作为标记。给出MATLAB代码
时间: 2024-03-05 11:49:06 浏览: 70
以下是一个简单的Matlab GUI双音拨号程序,其中包括了“#”确认键的实现。在按下“#”确认键时,会将前面拨过的号码进行确认保留,并在显示窗口中显示“#”号作为标记。
```matlab
function varargout = DTMF_GUI(varargin)
% DTMF_GUI MATLAB code for DTMF_GUI.fig
% DTMF_GUI, by itself, creates a new DTMF_GUI or raises the existing
% singleton*.
%
% H = DTMF_GUI returns the handle to a new DTMF_GUI or the handle to
% the existing singleton*.
%
% DTMF_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DTMF_GUI.M with the given input arguments.
%
% DTMF_GUI('Property','Value',...) creates a new DTMF_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DTMF_GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DTMF_GUI_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 DTMF_GUI
% Last Modified by GUIDE v2.5 05-Sep-2021 14:40:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DTMF_GUI_OpeningFcn, ...
'gui_OutputFcn', @DTMF_GUI_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 DTMF_GUI is made visible.
function DTMF_GUI_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 DTMF_GUI (see VARARGIN)
% Choose default command line output for DTMF_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DTMF_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Initialize global variables
global freq_low freq_high duration
freq_low = 697;
freq_high = 1209;
duration = 0.5;
% --- Outputs from this function are returned to the command line.
function varargout = DTMF_GUI_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;
% --- Executes on button press in pushbutton_1.
function pushbutton_1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '1'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_2.
function pushbutton_2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '2'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_3.
function pushbutton_3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '3'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_4.
function pushbutton_4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '4'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_5.
function pushbutton_5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '5'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_6.
function pushbutton_6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '6'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_7.
function pushbutton_7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '7'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_8.
function pushbutton_8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '8'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_9.
function pushbutton_9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '9'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_star.
function pushbutton_star_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_star (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low 1336];
play_sound(freqs,duration);
nums = [nums '*'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_0.
function pushbutton_0_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [freq_low freq_high];
play_sound(freqs,duration);
nums = [nums '0'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_pound.
function pushbutton_pound_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_pound (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global freq_low freq_high duration nums
freqs = [941 freq_high];
play_sound(freqs,duration);
nums = [nums '#'];
set(handles.edit_nums,'String',nums);
% --- Executes on button press in pushbutton_call.
function pushbutton_call_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_call (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global nums
disp(['Dialing ' nums]);
% --- Executes on button press in pushbutton_clear.
function pushbutton_clear_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global nums
nums = '';
set(handles.edit_nums,'String',nums);
% --- Plays a DTMF tone with the given frequencies and duration
function play_sound(freqs,duration)
Fs = 8000;
t = 0:1/Fs:duration;
y = cos(2*pi*freqs(1)*t) + cos(2*pi*freqs(2)*t);
sound(y,Fs);
% --- Executes during object creation, after setting all properties.
function edit_nums_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_nums (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 key press with focus on pushbutton_pound and none of its controls.
function pushbutton_pound_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to pushbutton_pound (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.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
%
% This function is called when a key is pressed on the pushbutton_pound, and
% checks whether the key was the '#' key.
global nums
if strcmp(eventdata.Key,'#')
nums = [nums '#'];
set(handles.edit_nums,'String',nums);
end
```
在上述代码中,我们在“#”确认键的回调函数pushbutton_pound_Callback中,添加了以下代码:
```matlab
nums = [nums '#'];
set(handles.edit_nums,'String',nums);
```
这段代码会将“#”号添加到拨号号码的最后,并在显示窗口中显示出来。
此外,我们还在pushbutton_pound_KeyPressFcn回调函数中,添加了以下代码:
```matlab
global nums
if strcmp(eventdata.Key,'#')
nums = [nums '#'];
set(handles.edit_nums,'String',nums);
end
```
这段代码会在用户按下键盘上的“#”键时,自动将“#”号添加到拨号号码的最后,并在显示窗口中显示出来。
阅读全文