Sei sulla pagina 1di 4

#include "stdafx.

h"
#include "ipp.h"
#include "tools.h"
#include "wave.h"
// header row and column of touch-tone frequencies in cycles/second
int pFreqs1[] = { 697, 770, 852, 941 };
int pFreqs2[] = { 1209, 1336, 1477, 1633 };
// 4x4 table of touch-tone frequencies in cycles/second
const int mapSize = 16;
struct CodeMap { char code;int freq1; int freq2; }
codeList[mapSize] = {
'1', 697, 1209, '2', 697, 1336, '3', 697, 1477, 'A', 697, 1633,
'4', 770, 1209, '5', 770, 1336, '6', 770, 1477, 'B', 770, 1633,
'7', 852, 1209, '8', 852, 1336, '9', 852, 1477, 'C', 852, 1633,
'*', 941, 1209, '0', 941, 1336, '#', 941, 1477, 'D', 941, 1633
};
int myMakeTone_16s(Ipp16s* pDst, int len, char code, int mag, int samplesPerSec)
{
// Sampling rate must be at least twice the highest frequency
if (samplesPerSec < (1633*2)) return -1;
int index = -1, i;
for (i=0; i<mapSize; i++)
if (code == codeList[i].code) { index = i; break; }
if (index == -1) return -1;
// Generate tones
// Frequency in cycles/sample is (freq in cycles/sec)/(samples/sec)
Ipp16s* pTmp = ippsMalloc_16s(len);
float phase = 0;
ippsTone_Direct_16s(pDst, len, mag/2, (float)codeList[index].freq1 /
(float)samplesPerSec, &phase, ippAlgHintNone);
phase = 0;
ippsTone_Direct_16s(pTmp, len, mag/2, (float)codeList[index].freq2 /
(float)samplesPerSec, &phase, ippAlgHintNone);
ippsAdd_16s_I(pTmp, pDst, len);
ippsFree(pTmp);
return 0;
}
int main(int argc, char* argv[])
{
const int numSize = 7;
int i;
char pNumber[numSize+1] = "2345678";
// char pNumber[numSize+1] = "5551212";
float toneLen, toneLenMax = .2;
int sampleRate, sampleRateMax = 44100;
int toneSamps = toneLenMax * sampleRateMax;
int gapSamps = toneSamps / 2;
int totSamps = toneSamps * numSize + gapSamps * (numSize-1);
Ipp16s *pDial = ippsMalloc_16s(totSamps);
sampleRate = 22000;
toneLen = 0.1;
Ipp16s* pTmp = pDial;
toneSamps = toneLen * sampleRate;
gapSamps = toneSamps / 2;
totSamps = toneSamps * numSize + gapSamps * (numSize-1);
ippsZero_16s(pDial, totSamps);
for (i=0; i<numSize; i++)
{
myMakeTone_16s(pTmp, toneSamps, pNumber[i], 16384, sampleRate);
pTmp += toneSamps + gapSamps;
}
DrawingSettings settings =
{
1,1,
PS_SOLID, PS_DASH,
0x00606060, 0x00000000,
1,1,
0x00cfcfcf, 0x007f7f7f
};
spView_16s(pDial, totSamps, "Tones", 0, settings);
spView_16s(pDial, toneSamps, "First Tone", 1, settings);
// Optional code to play the signal:
// WaveMem wm;
// wm.InitAlloc(totSamps, 16, sampleRate/1.2, 1);
// wm.Set(pDial);
// wm.Play(0);
ippsFree(pDial);
return 0;
}
#include <math.h>
00019
00020 #include "DTMF.h"
00021
00022 #define DTMF_WINDOW 205
00023
00024 DTMF::DTMF(unsigned int sampleRate, float amp, char tone) {
00025 unsigned int f[2];
00026 switch (tone) {
00027 case '1': case '2': case '3': f[0] = DTMF_FREQ[0]; break;
00028 case '4': case '5': case '6': f[0] = DTMF_FREQ[1]; break;
00029 case '7': case '8': case '9': f[0] = DTMF_FREQ[2]; break;
00030 case '*': case '0': case '#': f[0] = DTMF_FREQ[3]; break;
00031 }
00032 switch (tone) {
00033 case '1': case '4': case '7': case '*': f[1] = DTMF_FREQ[4]; break
;
00034 case '2': case '5': case '8': case '0': f[1] = DTMF_FREQ[5]; break
;
00035 case '3': case '6': case '9': case '#': f[1] = DTMF_FREQ[6]; break
;
00036 }
00037 sprintf(descr, "[+] DTMF %c (%uHz, %uHz)", tone, f[0], f[1]);
00038 s[0] = new Sine(sampleRate, amp, f[0]);
00039 s[1] = new Sine(sampleRate, amp, f[1]);
00040 }
00041
00042 void DTMF::processSignal(float* signal, unsigned long length) {
00043 s[0]->processSignal(signal, length);
00044 s[1]->processSignal(signal, length);
00045 }
00046
00047 DTMF::~DTMF() {
00048 delete s[0];
00049 delete s[1];
00050 }
00051
00052 DTMF_Detector::DTMF_Detector(unsigned int sampleRate) {
00053 sprintf(descr, "[=] DTMF_Detector");
00054 for (int i=0; i<7; i++) {
00055
00056 c[i] = 2.0*cos(2.0*PI*(float)DTMF_FREQ[i]/(float)sampleRate);
00057 for (int j=0; j<3; j++) {
00058 d[i][j] = 0.0;
00059 }
00060 }
00061
00062 index = 0;
00063 }
00064
00065 void DTMF_Detector::processSignal(float* signal, unsigned long length) {
00066 for (unsigned long int i=0; i<length; i++) {
00067 for (int j=0; j<7; j++) {
00068 d[j][0]=signal[i]+c[j]*d[j][1]-d[j][2];
00069 d[j][2]=d[j][1];
00070 d[j][1]=d[j][0];
00071 }
00072
00073 if (index++ > DTMF_WINDOW) {
00074 for (int k=0; k<7; k++) {
00075 r[k]= d[k][1]*d[k][1] - c[k]*d[k][1]*d[k][2] + d[k][2]*d[k
][2];
00076 d[k][1]=0;
00077 d[k][2]=0;
00078 }
00079 index = 0;
00080 }
00081 }
00082 }
00083
00084 char DTMF_Detector::detected(void) {
00085 int lf_index, hf_index;
00086 double max = 0.0;
00087 for (int i=0; i<4; i++) {
00088 if (r[i]>max) {
00089 max = r[i];
00090 lf_index = i;
00091 }
00092 }
00093 max = 0.0;
00094 for (int i=4; i<7; i++) {
00095 if (r[i]>max) {
00096 max = r[i];
00097 hf_index = i;
00098 }
00099 }
00100 return DTMF_KEYS[lf_index][hf_index-4];
00101 }

Potrebbero piacerti anche