注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

阿英讲matlab电话键盘拨号DTMF频率检测  

2013-01-08 14:05:27|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
即以此功德,庄严佛净土。上报四重恩,下济三途苦。惟愿见闻者,悉发菩提心。在世富贵全,往生极乐国。
matlab电话键盘拨号DTMF频率检测demo---信号处理入门 - 阿英 - Mr.Right

Detection of tones can be achieved by using a bank of filters or using the Discrete Fourier Transform (DFT or FFT).

However, the Goertzel algorithm is more efficient for this application.

The Goertzel algorithm is derived from the DFT and exploits the periodicity of the phase factor, exp(-j*2*pi*k/N) to reduce the computational complexity associated with the DFT, as the FFT does.

下面的程序是我今天下午整整一下午的结晶,里面包含了双音多频信号的产生检测完整过程。希望对大家学习有帮助。南无阿弥陀佛

% startup preparation
close all; clear all; 

%% === Generating DTMF Tones =================================================================
% First, let's generate the twelve frequency pairs
symbol = {'1','2','3','4','5','6','7','8','9','*','0','#'};
lfg = [697 770 852 941]; % Low frequency group
hfg = [1209 1336 1477 1633];  % High frequency group
f  = [];
for c=1:4,
    for r=1:3,
        f = [ f [lfg(c);hfg(r)] ];
    end
end
f'
% Next, let's generate and visualize the DTMF tones
Fs  = 8000;       % Sampling frequency 8 kHz
%hint: 100ms(N = 800) for playing sounds, 25.6 ms (N = 205) for detection
N = 800;          % Tones of 100 ms, total observation time Tp = N/Fs = N*Ts = 100ms
t   = (0:N-1)/Fs; % 800 samples at Fs

tones = zeros(N, size(f, 2));
for toneChoice = 1:12  % each column corresponding to a tone
    % Generate tone
    tones(:, toneChoice) = sum(sin(f(:, toneChoice)* 2*pi*t))';
    % Plot tone
    subplot(4, 3, toneChoice), plot(t*1e3, tones(:, toneChoice));
    title(['Symbol "', symbol{toneChoice},'": [',num2str(f(1,toneChoice)),',',num2str(f(2,toneChoice)),']'])
    set(gca, 'XLim', [0 25]);
    ylabel('Amplitude');
    if toneChoice > 9, xlabel('Time (ms)'); end
end
set(gcf, 'Color', [1 1 1], 'Position', [1 1 1280 1024])
annotation(gcf, 'textbox', 'Position', [0.38 0.96 0.45 0.026],...
    'EdgeColor',[1 1 1],...
    'String', '\bf Time response of each tone of the telephone pad', ...
    'FitBoxToText','on');
%% Playing DTMF Tones
% Let's play the tones of phone number 508 647 7000 for example. Notice
% that the "0" symbol corresponds to the 11th tone.
audid = audiodevinfo(0,Fs,16,1);
if  audid ~= -1
    for i = [5 11 8 6 4 7 7 11 11 11]
        p = audioplayer(tones(:, i), Fs, 16, audid);
        play(p);
        pause(0.5);
    end
end 
%=== END--> Generating DTMF Tones <---END ===========================================================


%% === Estimating DTMF Tones with the Goertzel Algorithm ============================================
% The minimum duration of a DTMF signal defined by the ITU standard is 40
% ms. Therefore, there are at most 0.04 x 8000 = 320 samples available for
% estimation and detection. The DTMF decoder needs to estimate the
% frequencies contained in these short signals. 
%
% One common approach to this estimation problem is to compute the
% Discrete-Time Fourier Transform (DFT) samples close to the seven
% fundamental tones. For a DFT-based solution, it has been shown that using
% 205 samples in the frequency domain minimizes the error between the
% original frequencies and the points at which the DFT is estimated.
Nt = 205;
original_f = [lfg(:); hfg(:)]  % Original frequencies
k = round(original_f/Fs*Nt);  % Indices of the DFT, Goertzel use 1-based indexing, DFT use 0-based indexing
estim_f = round(k*Fs/Nt)      % Frequencies at which the DFT is estimated
% To minimize the error between the original frequencies and the points at
% which the DFT is estimated, we truncate the tones, keeping only 205
% samples or 25.6 ms for further processing.
%hint: 100ms(N = 800) for playing sounds, 25.6 ms (N = 205) for detection
tones = tones(1:205, :); 
% Plot Goertzel's DFT magnitude estimate of each tone on a grid
% corresponding to the telephone pad.
figure,
for toneChoice = 1:12
    % Select tone
    tone = tones(:, toneChoice);
    % Estimate DFT using Goertzel
    ydft(:, toneChoice) = goertzel(tone, k + 1); % Goertzel use 1-based indexing
    % Plot magnitude of the DFT
    subplot(4, 3, toneChoice),stem(estim_f, abs(ydft(:, toneChoice)));
    title(['Symbol "', symbol{toneChoice},'": [',num2str(f(1, toneChoice)),',',num2str(f(2, toneChoice)),']'])
    set(gca, 'XTick', estim_f, 'XTickLabel', estim_f, 'XLim', [650 1550]);
    ylabel('DFT Magnitude');
    if toneChoice>9, xlabel('Frequency (Hz)'); end
end
set(gcf, 'Color', [1 1 1], 'Position', [1 1 1280 1024])
annotation(gcf,'textbox', 'Position',[0.28 0.96 0.45 0.026],...
    'EdgeColor',[1 1 1],...
    'String', '\bf Estimation of the frequencies contained in each tone of the telephone pad using Goertzel', ...
    'FitBoxToText','on');
%===END--> Estimating DTMF Tones with the Goertzel Algorithm <---END  ================================


%% === Detecting DTMF Tones ============================================
k = [18, 20, 22, 24, 31, 34, 38, 42]; % Indices of the DFT, Goertzel use 1-based indexing, DFT use 0-based indexing
Fs = 8e3;
digitsNo = 11; % phone code length
keypad = [1, 2, 3, 65;...  % char(65) == 'A'
 4, 5, 6, 66;...
     7, 8, 9, 67;...
    42, 0, 35, 68]; 
f_rw = [697, 770, 852, 941]; % low frequency group
f_col = [1209, 1336, 1477, 1633]; % high frequency group
fprintf('please input %d digits phone number\n', digitsNo);
phoneCode_Tx = input('phone code = '); 
phoneCode_Rx = []; % phone code at the reveive end
threshold = 80; % if the spectrum line magnitude > threshold then the key is detected
figure;
for iKey = 1:digitsNo % the detection loop
currentDigit = fix(phoneCode_Tx/10^(digitsNo - iKey)); % 10^(digitsNo - iKey) is the weight for currentDigit   
    phoneCode_Tx = phoneCode_Tx - currentDigit*10^(digitsNo - iKey); % remove the current MSB from phoneCode_Tx      
% locating the currentDigit in keypad --- note: break acting as the exit in a labyrinth
for ridx = 1:length(f_rw)
        for cidx = 1:length(f_col)
            if keypad(ridx, cidx) == currentDigit; break, end       % find the row index for currentDigit
        end
      if keypad(ridx, cidx) == currentDigit; break, end      % find the column index for currentDigit
    end
n = 0:800 - 1; % N = 800; Tp = N/Fs = 100ms for playing the sound  
x = sin(2*pi*n*f_rw(ridx)/Fs) + sin(2*pi*n*f_col(cidx)/Fs); % n/Fs = t
sound(x, Fs); 
X = goertzel(x(1:205), k + 1); % Goertzel use 1-based indexing, DFT use 0-based indexing
magX = abs(X);
subplot(ceil(digitsNo/2), 2, iKey);
    stem(k, magX, '.'); grid on; xlabel('k'); ylabel('|X(k)|');
% locate the column index of the magnitude peak
for cidxHighFreq = 5:8 % Goertzel use 1-based indexing
     if magX(cidxHighFreq) > threshold, break, end;
end
% locate the row index of the magnitude peak
for ridxLowFreq = 1:4 % Goertzel use 1-based indexing
     if magX(ridxLowFreq) > threshold, break, end;
end
% phoneCode_Rx = phoneCode_Rx + keypad(ridxLowFreq, cidxHighFreq - 4)*10^(digitsNo - iKey); % 10^(digitsNo - iKey) is the weight for currentDigit
phoneCode_Rx = [phoneCode_Rx keypad(ridxLowFreq, cidxHighFreq - 4)]; 
end
display('the detected phone number is:');
phoneCode_Rx
%===END--> Detecting DTMF Tones <---END  ================================
  评论这张
 
阅读(940)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016