6b6c5aab81262ed422ca576cf7d3bf61a29315d0
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
, "?i:o:a:r:c:d:f:t:nlbq")) != -1) {
88 freopen(optarg
, "r", stdin
);
91 freopen(optarg
, "w", stdout
);
94 freopen(optarg
, "a", stdout
);
97 samplerate
= atoi(optarg
);
100 samplecount
= atoi(optarg
);
103 samplecount
= samplerate
/atoi(optarg
);
106 addfreq(freqs
, atoi(optarg
));
109 treshold
= atoi(optarg
);
130 if(freqs
[0]==-1) addfreq(freqs
, 440);
131 float samples
[samplecount
];
136 "#Detected tone: %d Hz\n"
137 "#Samplerate: %d Hz\n"
138 "#Frame lenght: %d samples\n"
141 ,freqs
[0],samplerate
,samplecount
,treshold
);
145 int i
; for(i
=0;freqs
[i
]!=-1;i
++) {
146 printf("\t%2dHz",freqs
[i
]);
151 char print
=0, printnow
=0, printlast
= 0;
152 while(!feof(stdin
)) {
156 for(i
=0;i
<samplecount
&& !feof(stdin
);i
++) {
157 unsigned char sample
;
158 fread(&sample
,1,1,stdin
);
160 //printf("%d\n", sample);
166 for(i
=0;freqs
[i
]!=-1;i
++) {
167 power
[i
] = goertzel_mag(samplecount
, freqs
[i
], samplerate
, samples
);
169 //Set print true if over treshold or if changed to false (print for the last time after going under treshold)
170 printnow
= power
[i
] > treshold
;
171 print
= !(!repeat
&& printlast
&& !(!printnow
)) && (print
|| printnow
|| (printlast
&& !noreturn
));
173 printlast
= printnow
;
178 printf("%8.2f", position
);
179 for(i
=0;freqs
[i
]!=-1;i
++) {
182 printf("%d",(int)power
[i
]);
184 printf("%.4f",power
[i
]);
191 position
+= ((float)samplecount
/(float)samplerate
);
This page took 0.354326 seconds and 3 git commands to generate.