5 float goertzel_mag(int numSamples
,int TARGET_FREQUENCY
,int SAMPLING_RATE
, float* data
)
8 On lower samplerates and frame sizes this may perform sub-optimally. Eg.:
9 When set to detect 440Hz (at 8000Hz samplerate and ~4000 samples)
10 it actually detects something around 438,3Hz rather than 400Hz...
11 If you can't increase samplerate way around this is just to increase sensitivity.
15 float floatnumSamples
;
16 float omega
,sine
,cosine
,coeff
,q0
,q1
,q2
,magnitude
,real
,imag
;
18 float scalingFactor
= numSamples
/ 2.0;
20 floatnumSamples
= (float) numSamples
;
21 k
= (int) (0.5 + ((floatnumSamples
* TARGET_FREQUENCY
) / SAMPLING_RATE
));
22 omega
= (2.0 * M_PI
* k
) / floatnumSamples
;
30 for(i
=0; i
<numSamples
; i
++)
32 q0
= coeff
* q1
- q2
+ data
[i
];
37 // calculate the real and imaginary results
38 // scaling appropriately
39 real
= (q1
- q2
* cosine
) / scalingFactor
;
40 imag
= (q2
* sine
) / scalingFactor
;
42 magnitude
= sqrtf(real
*real
+ imag
*imag
);
46 void print_help(char ** argv
) {
51 "\t-i <file>\tInput from file (default STDIN)\n"
52 "\t-o <file>\tOutput to file (default STDOUT)\n"
53 "\t-a <file>\tOutput to file (append) (default STDOUT)\n"
55 "\t-r <samplerate>\tInput samplerate (deault 8000 Hz)\n"
56 "\t-c <count>\tFrame size in samples (default 4000 Samples)\n"
57 "\t-d <ratio>\tFrame size (default 2) (samplerate will be divided by this number to get frame size same as -c)\n"
59 "\t-f <freq>\tAdd frequency in Hz to detect (if no specified 440 Hz will be added)\n"
61 "\t-t <treshold>\tSet treshold (used to hide magnitudes lower than treshold) (defaults -1)\n"
62 "\t-n\t\tPrint integers rather than floats\n"
63 "\t-l\t\tDo not repeat values while still over treshold\n"
64 "\t-b\t\tDo not print first value that will fall under treshold\n"
65 "\t-q\t\tQuiet mode: print only values\n"
67 "\t-?\t\tPrint help\n"
74 "\tsox input.mp3 -b 8 -c 1 -r 8000 -t wav - | %s\n"
75 "\t%s -n -q -l -r 8000 -d 20 -t $tresh -f 697 [-f 770 ...]\n"
77 ,argv
[0],argv
[0],argv
[0]
81 "Frequencies for DTMF decoding:\n"
82 "\t-f 697 -f 770 -f 852 -f 941 -f 1209 -f 1336 -f 1477 -f 1633 -t 10\n"
86 void addfreq(int *freqs
, int freq
) {
88 while(freqs
[i
]!=-1) i
++;
93 int main(int argc
, char ** argv
) {
94 int samplerate
= 8000;
95 int samplecount
= 4000;
101 int freqs
[argc
+1]; freqs
[0]=-1;
104 while ((opt
= getopt(argc
, argv
, "?i:o:a:r:c:d:f:t:nlbq")) != -1) {
107 freopen(optarg
, "r", stdin
);
110 freopen(optarg
, "w", stdout
);
113 freopen(optarg
, "a", stdout
);
116 samplerate
= atoi(optarg
);
119 samplecount
= atoi(optarg
);
122 samplecount
= samplerate
/atoi(optarg
);
125 addfreq(freqs
, atoi(optarg
));
128 treshold
= atoi(optarg
);
149 if(freqs
[0]==-1) addfreq(freqs
, 440);
150 float samples
[samplecount
];
155 "#Detected tone: %d Hz\n"
156 "#Samplerate: %d Hz\n"
157 "#Frame lenght: %d samples\n"
160 ,freqs
[0],samplerate
,samplecount
,treshold
);
164 int i
; for(i
=0;freqs
[i
]!=-1;i
++) {
165 printf("\t%2dHz",freqs
[i
]);
170 char print
=0, printnow
=0, printlast
= 0;
171 while(!feof(stdin
)) {
175 for(i
=0;i
<samplecount
&& !feof(stdin
);i
++) {
176 unsigned char sample
;
177 fread(&sample
,1,1,stdin
);
179 //printf("%d\n", sample);
185 for(i
=0;freqs
[i
]!=-1;i
++) {
186 power
[i
] = goertzel_mag(samplecount
, freqs
[i
], samplerate
, samples
);
188 //Set print true if over treshold or if changed to false (print for the last time after going under treshold)
189 printnow
= power
[i
] > treshold
;
190 print
= !(!repeat
&& printlast
&& !(!printnow
)) && (print
|| printnow
|| (printlast
&& !noreturn
));
192 printlast
= printnow
;
197 printf("%8.2f", position
);
198 for(i
=0;freqs
[i
]!=-1;i
++) {
201 printf("%d",(int)power
[i
]);
203 printf("%.4f",power
[i
]);
210 position
+= ((float)samplecount
/(float)samplerate
);
This page took 0.517295 seconds and 5 git commands to generate.