DTMF的matlab实现

1,DTMF原理和产生思路

DTMF即双音多频。双音多频的拨号键盘是4×4的矩阵,每一行代表一个高频,每一列代表一个低频。用户每按一个键就发送一个高频和低频的正弦信号组合,比如在键盘上按“1”键,电话机就会发送一个697 Hz和1209 Hz的组合信号给电话交换机。每个按键的按键音包含着两个频率的信号,在生成时即可将两个频率的信号进行加和来生成DTMF信号。

2,代码

fs=8000;%采样率
tk=0.5;%the time of the key按键的声音持续时间
t=0:1/fs:(tk-1/fs);%时间点
z=zeros(1,4000);%按键与按键之间空白时间
%--------------------生成每个频率的信号----------------------
s697=sin(2*pi*697*t);
s770=sin(2*pi*770*t);
s852=sin(2*pi*852*t);
s941=sin(2*pi*941*t);
s1209=sin(2*pi*1209*t);
s1336=sin(2*pi*1336*t);
s1477=sin(2*pi*1477*t);
s1633=sin(2*pi*1633*t);
%---------------------生成每个按键的多频-------------------
p1=s697+s1209;
p2=s697+s1336;
p3=s697+s1477;
pa=s697+s1633;
p4=s770+s1209;
p5=s770+s1336;
p6=s770+s1477;
pb=s770+s1633;
p7=s852+s1209;
p8=s852+s1336;
p9=s852+s1477;
pc=s852+s1633;
pxing=s941+s1209;%星号**
p0=s941+s1336;
pjing=s941+s1477;%#号
pd=s941+s1633;
%------------------生成声音序列----------------------------
sound=[p1,z,p6,z,p0,z,p1,z,p0,z,p1,z,p9,z,p9,z,p0,z,p4,z,p4,z,];
sound=sound/max(abs(sound));%归一化声音序列
figure(1);
plot(sound);
%写出音频
audiowrite('my_phone_number_sound_test.wav',sound,8000);

%读出音频文件,绘制图线
[phone_sound,fs]=audioread('my_phone_number_sound_test.wav');
figure(2);
plot(phone_sound);

3,代码解释

音频信号默认的采样率为8000Hz所以一开始设定采样率fs=8000Hz

audiowrite用来生成音频

audioread用来读取音频

其他见注释

4,结果

生成的图像即为本人学号

5,代码改进

 这是老师的一种思路实际上是一样的)

fl=[697 770 852 941];%低频频率
fh=[1209 1336 1477];%高频频率

Fs=8000;%采样频率8kHz
last_time=0.5;%单个按键声音持续时间
compound=[];
numString='16010199044';%要转换的号码

for i=1:length(numString)
    switch numString(i)
        case'1'
            freq_low=fl(1);freq_hgh=fh(1);
        case'2'
            freq_low=fl(1);freq_hgh=fh(2);
        case'3'
            freq_low=fl(1);freq_hgh=fh(3);
        case'4'
            freq_low=fl(2);freq_hgh=fh(1);
        case'5'
            freq_low=fl(2);freq_hgh=fh(2);
        case'6'
            freq_low=fl(2);freq_hgh=fh(3);
        case'7'
            freq_low=fl(3);freq_hgh=fh(1);
        case'8'
            freq_low=fl(3);freq_hgh=fh(2);
        case'9'
            freq_low=fl(3);freq_hgh=fh(3);
        case'0'
            freq_low=fl(4);freq_hgh=fh(2);
        case'*'
            freq_low=fl(4);freq_hgh=fh(1);
        case'#'
            freq_low=fl(3);freq_hgh=fh(3);
        otherwise
            error('naive!');
    end
    single=0.25*sin(2*pi*freq_low*[1/Fs:1/Fs:last_time])+...
        0.25*sin(2*pi*freq_hgh*[1/Fs:1/Fs:last_time]);
    single=[single,zeros(1,1000)];
    compound=[compound,single];%将每个按键串在一起
    
    sound(compound,Fs);%播放声音
    
end
plot(compound);
audiowrite('test.wav',compound,Fs);%保存声音文件

  结果

5,函数用法

audiowrite

posted @ 2019-01-15 09:35  孤独野猪骑士  阅读(3206)  评论(1编辑  收藏  举报