9 sox input.mp3 -b 8 -c 1 -r 8000 -t wav - | ./goertzel
11 Arguments for DTMF decoding:
12 -f 697 -f 770 -f 852 -f 941 -f 1209 -f 1336 -f 1477 -f 1633 -t 10
15 float goertzel_mag(int numSamples
,int TARGET_FREQUENCY
,int SAMPLING_RATE
, float* data
)
18 On lower samplerates and frame sizes this may perform sub-optimally. Eg.:
19 When set to detect 440Hz (at 8000Hz samplerate and ~4000 samples)
20 it actually detects something around 438,3Hz rather than 400Hz...
21 If you can't increase samplerate way around this is just to increase sensitivity.
25 float floatnumSamples
;
26 float omega
,sine
,cosine
,coeff
,q0
,q1
,q2
,magnitude
,real
,imag
;
28 float scalingFactor
= numSamples
/ 2.0;
30 floatnumSamples
= (float) numSamples
;
31 k
= (int) (0.5 + ((floatnumSamples
* TARGET_FREQUENCY
) / SAMPLING_RATE
));
32 omega
= (2.0 * M_PI
* k
) / floatnumSamples
;
40 for(i
=0; i
<numSamples
; i
++)
42 q0
= coeff
* q1
- q2
+ data
[i
];
47 // calculate the real and imaginary results
48 // scaling appropriately
49 real
= (q1
- q2
* cosine
) / scalingFactor
;
50 imag
= (q2
* sine
) / scalingFactor
;
52 magnitude
= sqrtf(real
*real
+ imag
*imag
);
56 void print_help(char ** argv
) {
57 printf("help me %s\n", argv
[0]);
60 void addfreq(int *freqs
, int freq
) {
62 while(freqs
[i
]!=-1) i
++;
67 int main(int argc
, char ** argv
) {
69 int samples[] = {0,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1};
71 float power = goertzel(samplecount, samples, 1.2, 18);
72 printf("G: %f\n", power);
75 int samplerate
= 8000;
76 int samplecount
= 4000;
82 int freqs
[argc
+1]; freqs
[0]=-1;
85 while ((opt
= getopt(argc
, argv
, "?r:s:f:t:iqna")) != -1) {
88 samplerate
= atoi(optarg
);
91 samplecount
= atoi(optarg
);
94 addfreq(freqs
, atoi(optarg
));
97 treshold
= atoi(optarg
);
118 if(freqs
[0]==-1) addfreq(freqs
, 440);
119 float samples
[samplecount
];
124 "#Detected tone: %d Hz\n"
125 "#Samplerate: %d Hz\n"
126 "#Frame lenght: %d samples\n"
129 ,freqs
[0],samplerate
,samplecount
,treshold
);
133 int i
; for(i
=0;freqs
[i
]!=-1;i
++) {
134 printf("\t%2dHz",freqs
[i
]);
139 char print
=0, printnow
=0, printlast
= 0;
140 while(!feof(stdin
)) {
144 for(i
=0;i
<samplecount
&& !feof(stdin
);i
++) {
145 unsigned char sample
;
146 fread(&sample
,1,1,stdin
);
148 //printf("%d\n", sample);
154 for(i
=0;freqs
[i
]!=-1;i
++) {
155 power
[i
] = goertzel_mag(samplecount
, freqs
[i
], samplerate
, samples
);
157 //Set print true if over treshold or if changed to false (print for the last time after going under treshold)
158 printnow
= power
[i
] > treshold
;
159 print
= !(!repeat
&& printlast
&& !(!printnow
)) && (print
|| printnow
|| (printlast
&& !noreturn
));
161 printlast
= printnow
;
166 printf("%8.2f", position
);
167 for(i
=0;freqs
[i
]!=-1;i
++) {
170 printf("%d",(int)power
[i
]);
172 printf("%.4f",power
[i
]);
179 position
+= ((float)samplecount
/(float)samplerate
);
This page took 0.337232 seconds and 4 git commands to generate.