www.gusucode.com > matlab的用户界面实例,模拟电话发音源码程序 > dtmfs/DTMFSystem.m

    function varargout = DTMFSystem(varargin)
% DTMFSYSTEM M-file for DTMFSystem.fig
%      DTMFSYSTEM, by itself, creates a new DTMFSYSTEM or raises the existing
%      singleton*.
%
%      H = DTMFSYSTEM returns the handle to a new DTMFSYSTEM or the handle to
%      the existing singleton*.
%
%      DTMFSYSTEM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DTMFSYSTEM.M with the given input arguments.
%
%      DTMFSYSTEM('Property','Value',...) creates a new DTMFSYSTEM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DTMFSystem_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DTMFSystem_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 DTMFSystem

% Last Modified by GUIDE v2.5 20-Nov-2006 21:28:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DTMFSystem_OpeningFcn, ...
                   'gui_OutputFcn',  @DTMFSystem_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 DTMFSystem is made visible.
function DTMFSystem_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 DTMFSystem (see VARARGIN)

% Choose default command line output for DTMFSystem
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = DTMFSystem_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 Key1.
function Key1_Callback(hObject, eventdata, handles)
% hObject    handle to Key1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'1');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','1');
% --- Executes on button press in Key2.

function Key2_Callback(hObject, eventdata, handles)
% hObject    handle to Key2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'2');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','2');
% --- Executes on button press in Key3.

function Key3_Callback(hObject, eventdata, handles)
% hObject    handle to Key3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'3');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','3');
% --- Executes on button press in Key4.

function Key4_Callback(hObject, eventdata, handles)
% hObject    handle to Key4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'4');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','4');
% --- Executes on button press in Key5.

function Key5_Callback(hObject, eventdata, handles)
% hObject    handle to Key5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'5');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','5');
% --- Executes on button press in Key6.

function Key6_Callback(hObject, eventdata, handles)
% hObject    handle to Key6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'6');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','6');   
% --- Executes on button press in Key7.

function Key7_Callback(hObject, eventdata, handles)
% hObject    handle to Key7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'7');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','7');   
% --- Executes on button press in Key8.

function Key8_Callback(hObject, eventdata, handles)
% hObject    handle to Key8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'8');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','8');   
% --- Executes on button press in Key9.

function Key9_Callback(hObject, eventdata, handles)
% hObject    handle to Key9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'9');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','9');   
% --- Executes on button press in KeyStar.

function KeyStar_Callback(hObject, eventdata, handles)
% hObject    handle to KeyStar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'*');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','*'); 
% --- Executes on button press in Key0.

function Key0_Callback(hObject, eventdata, handles)
% hObject    handle to Key0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'0');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','0');
% --- Executes on button press in KeySharp.

function KeySharp_Callback(hObject, eventdata, handles)
% hObject    handle to KeySharp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    Signal=PressKeyDown(handles,'#');
    handles.DTMFSignal=Signal;
    %Update handles structure
    guidata(hObject,handles);
    set(handles.CodeText,'string','#');
% --- Executes on button press in Decode.

function Decode_Callback(hObject, eventdata, handles)
% hObject    handle to Decode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    keys=['1','2','3';'4','5','6';'7','8','9';'*','0','#'];
    RowNumber=FindLowerFreq(handles.DTMFSignal);
    ColumnNumber=FindHigherFreq(handles.DTMFSignal);
    ResultKey=keys(RowNumber,ColumnNumber);
    set(handles.DecodeText,'string',char(ResultKey));
    
% --- Executes during object creation, after setting all properties.
function text2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text2 (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 text2_Callback(hObject, eventdata, handles)
% hObject    handle to text2 (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 text2 as text
%        str2double(get(hObject,'String')) returns contents of text2 as a double


% --- Executes during object creation, after setting all properties.
function text3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --------------------------------------------------------------------
function text3_Callback(hObject, eventdata, handles)
% hObject    handle to text3 (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 CodeText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CodeText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --------------------------------------------------------------------
function CodeText_Callback(hObject, eventdata, handles)
% hObject    handle to CodeText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


%dtfm.m
function x=dtmf(key);
    fs=8000;  N=0.1*fs;%信号时间为50ms
    R=1;  fl=0;  fh=0;
    switch key
        case{'1','2','3'};       fl=697;
        case{'4','5','6'};      fl=770;
        case{'7','8','9'};       fl=852;
        case{'*','0','#'};       fl=941;
    end
    switch key
        case{'1','4','7','*'};    fh=1209;
        case{'2','5','8','0'};   fh=1336;
        case{'3','6','9','#'};    fh=1477;
    end
    x=wav_gener(R,2*pi*fl/fs,N)+wav_gener(R,2*pi*fh/fs,N);
    
%wav_gener.m
function h=wav_gener(R,omega,N)
w1=0;  w2=0;   delta=zeros(1,N);   delta(1)=1;
k=R*cos(omega);  h=zeros(1,N);
    for n=1:N
        w0=delta(n)+2*k*w1-R^2*w2;
        h(n)=w0-k*w1;  w2=w1;   w1=w0;
    end
    
%DesignFilter.m
function B=DesignFilter(Wo)
fs=8000;  N=33
k=2*pi/fs;   Bandwidth=70;
wc1=(Wo-Bandwidth/2)*k;
wc2=(Wo+Bandwidth/2)*k;
B=fir2(N,[0,wc1,wc2,1],[0,1,1,0]);

%DesignHigherFilter.m
function B=DesignHigherFilter(Wo)
fs=8000;   N=199;   k=2*pi/fs;   Bandwidth=110;
wc1=(Wo-Bandwidth/2)*k;
wc2=(Wo+Bandwidth/2)*k;
B=fir2(N,[0,wc1/pi,wc2/pi,1],[0,1,1,0]);

%DesignLowerFilter.m
function B=DesignLowerFilter(Wo)  
fs=8000;   N=399;   k=2*pi/fs;   Bandwidth=70;
wc1=(Wo-Bandwidth/2)*k;
wc2=(Wo+Bandwidth/2)*k;
B=fir2(N,[0,wc1/pi,wc2/pi,1],[0,1,1,0]);

%PressKeyDown.m
function Signal=PressKeyDown(handles,key)
Signal=dtmf(key);
sound(Signal,8000);%利用声卡播放播音号

set(gcf,'CurrentAxes',handles.TimeFig);
plot(Signal);grid;%画信号的时域波形

Spectrum=fft(Signal);
Spectrum=fftshift(Spectrum);%计算信号的频谱

N=length(Spectrum);    nStep=(2*pi)/(N-1);
w=-pi:nStep:pi;
set(gcf,'CurrentAxes',handles.FrequencyFig);
plot(w/(2*pi)*8000,abs(Spectrum));grid;%画信号的频谱图
axis([-2000,2000,0,200]);   xlabel('单位:(Hz)');

%FindHigherFreq.m
function number=FindHigherFreq(Signal)
Freq=[1209,1336,1447];  temp=0;
for n=1:length(Freq)
    Wo=Freq(n);   B=DesignHigherFilter(Wo);
    Out=filter(B,1,Signal);
    Fout=max(abs(fft(Out)));
    if(Fout>temp)
        number=n;    temp=Fout;
    end
end


%FindLowerFreq.m
function number=FindLowerFreq(Signal)
   Freq=[697,770,852,941];   temp=0;
   for n=1:length(Freq)
       Wo=Freq(n);  B=DesignLowerFilter(Wo);
       Out=filter(B,1,Signal);
       Fout=max(abs(fft(Out)));
       if(Fout>temp)
           number=n;   temp=Fout;
       end
   end