DSPRelated.com
Forums

Simple question regarding frequency detection

Started by Atri Mandal February 20, 2004
Hi,
I am writing an application to detect a particular frequency(viz. 4.5
Khz) in an input audio signal. I have written the code to capture the
sound using a microphone; but I have to properly design a digital filter
which will return TRUE as soon as it detects sound of this frequency in
the input signal. Also my project demands that I have to detect the
signal by looking at a very small number of samples e.g. 44. So I will
apply the digital filter on a window of 44 samples of the input signal
repeatedly till I get a high response indicating that I have got the
reqd. frequency.

I know DFT/FFT is an option but are there any better algorithms I can
use for my project. Please suggest the best algorithm that will do my
work. I am writing the code in C++ so if anyone can point me to existing
code on the net that will be really helpful.

Thanks,
Atri.

Atri Mandal wrote:
> Hi, > I am writing an application to detect a particular frequency(viz. 4.5 > Khz) in an input audio signal. I have written the code to capture the > sound using a microphone; but I have to properly design a digital filter > which will return TRUE as soon as it detects sound of this frequency in > the input signal. Also my project demands that I have to detect the > signal by looking at a very small number of samples e.g. 44. So I will > apply the digital filter on a window of 44 samples of the input signal > repeatedly till I get a high response indicating that I have got the > reqd. frequency. > > I know DFT/FFT is an option but are there any better algorithms I can > use for my project. Please suggest the best algorithm that will do my > work. I am writing the code in C++ so if anyone can point me to existing > code on the net that will be really helpful. > > Thanks, > Atri. >
Atri: Try the Goertzel algorithm. Good luck, OUP
One Usenet Poster wrote:

> Atri Mandal wrote: > >> Hi, >> I am writing an application to detect a particular frequency(viz. 4.5 >> Khz) in an input audio signal. I have written the code to capture the >> sound using a microphone; but I have to properly design a digital filter >> which will return TRUE as soon as it detects sound of this frequency in >> the input signal. Also my project demands that I have to detect the >> signal by looking at a very small number of samples e.g. 44. So I will >> apply the digital filter on a window of 44 samples of the input signal >> repeatedly till I get a high response indicating that I have got the >> reqd. frequency. >> >> I know DFT/FFT is an option but are there any better algorithms I can >> use for my project. Please suggest the best algorithm that will do my >> work. I am writing the code in C++ so if anyone can point me to existing >> code on the net that will be really helpful. >> >> Thanks, >> Atri. >> > > Atri: > > Try the Goertzel algorithm. > > Good luck, > OUP
"44 samples" at what sample rate? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Atri Mandal <mandala@ics.uci.edu> wrote in message news:<40359D7A.12B15FED@ics.uci.edu>...
> Hi, > I am writing an application to detect a particular frequency(viz. 4.5 > Khz) in an input audio signal. I have written the code to capture the > sound using a microphone; but I have to properly design a digital filter > which will return TRUE as soon as it detects sound of this frequency in > the input signal. Also my project demands that I have to detect the > signal by looking at a very small number of samples e.g. 44. So I will > apply the digital filter on a window of 44 samples of the input signal > repeatedly till I get a high response indicating that I have got the > reqd. frequency. > > I know DFT/FFT is an option but are there any better algorithms I can > use for my project. Please suggest the best algorithm that will do my > work. I am writing the code in C++ so if anyone can point me to existing > code on the net that will be really helpful. > > Thanks, > Atri.
Here is Matlab code for the Goertzel algm if you just need a TRUE/FLASE indication. Set a threshold, and if y is greater than the threshold, then the frequency is detected. x = input vector f = frequency desired N = number of samples to test over Fs = sample frequency function y = goert(x,f,N,Fs) k = fix(0.5 + N*f/Fs); w = (2*pi/N)*k; coeff = 2 * cos(w); Q1 = 0; Q2 = 0; for j = 1:N Q0 = coeff * Q1 - Q2 + x(j); Q2 = Q1; Q1 = Q0; end y = sqrt(Q1^2 + Q2^2 - Q1 * Q2 * coeff); Here is code to test the alghm. Plot F aganist y to see the detector output as a function of frequency. fdet is the frequency desired N = number of samples to test over Fs = sample frequency function [F,y] = testgoert(fdet,N,Fs) t = 0:1/Fs:((1/Fs)*N - (1/Fs)); k = 1; for f = fix(fdet/2:(fdet/2)/15:fdet*2) x = sin(2*pi*f*t); y(k) = goert(x,fdet,N,Fs); F(k) = f; k = k + 1; end y = y(:); F = F(:); Maurice Givens
Atri Mandal <mandala@ics.uci.edu> wrote in message news:<40359D7A.12B15FED@ics.uci.edu>...
> Hi, > I am writing an application to detect a particular frequency (viz. > 4.5 Khz) in an input audio signal. I have written the code to > capture the sound using a microphone; but I have to properly design > a digital filter which will return TRUE as soon as it detects sound > of this frequency in the input signal. Also my project demands > that I have to detect the signal by looking at a very small number > of samples e.g. 44. So I will apply the digital filter on a window > of 44 samples of the input signal repeatedly till I get a high > response indicating that I have got the reqd. frequency. > > I know DFT/FFT is an option but are there any better algorithms > I can use for my project. Please suggest the best algorithm that > will do my work. I am writing the code in C++ so if anyone can > point me to existing code on the net that will be really helpful. > > Thanks, > Atri.
I would be inclined to use regression to fit the sampled signal to: c1 + c2 cos(t 2Pi 4500Hz) + c3 sin(t 2Pi 4500Hz) The amplitude of the 4.5 KHz is then sqrt(c2^2 + c3^2). For best results, the model should also include background signals of known specific frequencies. The inverse matrix used in the regression can be calculated ahead of time since it doesn't change. / \ . | n Sum(cos) Sum(sin) | . A = | Sum(cos) Sum(cos^2) Sum(cos sin) | . | Sum(sin) Sum(cos sin) Sum(sin^2) | . \ / / \ / \ . | c1 | | Sum(y) | . C = | c2 | Y = | Sum(y cos) | . | c3 | | Sum(y sin) | . \ / \ / A C = Y C = A^-1 Y
44 samples at 44.1K samples/sec.
Thanks for all your help. I'll try out the Goertzel algorithm.
-Atri.

Jerry Avins wrote:

> One Usenet Poster wrote: > > > Atri Mandal wrote: > > > >> Hi, > >> I am writing an application to detect a particular frequency(viz. 4.5 > >> Khz) in an input audio signal. I have written the code to capture the > >> sound using a microphone; but I have to properly design a digital filter > >> which will return TRUE as soon as it detects sound of this frequency in > >> the input signal. Also my project demands that I have to detect the > >> signal by looking at a very small number of samples e.g. 44. So I will > >> apply the digital filter on a window of 44 samples of the input signal > >> repeatedly till I get a high response indicating that I have got the > >> reqd. frequency. > >> > >> I know DFT/FFT is an option but are there any better algorithms I can > >> use for my project. Please suggest the best algorithm that will do my > >> work. I am writing the code in C++ so if anyone can point me to existing > >> code on the net that will be really helpful. > >> > >> Thanks, > >> Atri. > >> > > > > Atri: > > > > Try the Goertzel algorithm. > > > > Good luck, > > OUP > > "44 samples" at what sample rate? > > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
There is no detection bandwith specified.  1Khz, 100 Hz, 1 Hz, 0.1 Hz ?
If you get a 4.8 KHz signal, should it be detected or not?    The detection
bandwidth is directly related to the amount of time you have to observe the
signal.    You are really trying to detect is about a 0.001 second burst of
a 4.500 KHz tone. That is 4.5 cycles of wanted tone, which indicates that
the detection bandwidth is very large.  Noise and "selectivity" can be
problems with wide bandwidth. Checkout the signal spectrum or FT of a tone
burst.


"Atri Mandal" <mandala@ics.uci.edu> wrote in message
news:40366E19.D39D464C@ics.uci.edu...
> 44 samples at 44.1K samples/sec. > Thanks for all your help. I'll try out the Goertzel algorithm. > -Atri. > > Jerry Avins wrote: > > > One Usenet Poster wrote: > > > > > Atri Mandal wrote: > > > > > >> Hi, > > >> I am writing an application to detect a particular frequency(viz. 4.5 > > >> Khz) in an input audio signal. I have written the code to capture the > > >> sound using a microphone; but I have to properly design a digital
filter
> > >> which will return TRUE as soon as it detects sound of this frequency
in
> > >> the input signal. Also my project demands that I have to detect the > > >> signal by looking at a very small number of samples e.g. 44. So I
will
> > >> apply the digital filter on a window of 44 samples of the input
signal
> > >> repeatedly till I get a high response indicating that I have got the > > >> reqd. frequency. > > >> > > >> I know DFT/FFT is an option but are there any better algorithms I can > > >> use for my project. Please suggest the best algorithm that will do my > > >> work. I am writing the code in C++ so if anyone can point me to
existing
> > >> code on the net that will be really helpful. > > >> > > >> Thanks, > > >> Atri. > > >> > > > > > > Atri: > > > > > > Try the Goertzel algorithm. > > > > > > Good luck, > > > OUP > > > > "44 samples" at what sample rate? > > > > Jerry > > -- > > Engineering is the art of making what you want from things you can get. > > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; >
Maurice Givens wrote:

> y = y(:); > F = F(:); > >
Thanks, Maurice. What do the above two lines do? Bob -- "Things should be described as simply as possible, but no simpler." A. Einstein
To detect is to decide whether what you have is the desired signal, rather
than the other stuff - noise processes - you would expect to see.

The decision will depend on any consequences of wrong decisions of either
type, and on any prior knowledge of liklihood.  It will also depend
importantly on the null hypothesis - what patterns you expect in the
absence of signal.

Testing for a known frequency sine wave in stationary white noise is prety
simple - just correlate to estimate the amplitude and use the known noise
level.  You can do a Bays decision using the gaussian model or use some
stats cook book confidence rule.

If the noise is more variable you could still model it as gaussian in a
local time-frequency region. You could correlate as before to get the
signal power and filter the residue signal in a time-frequency window to
give the local noise power - the distortion of the pure signal you expect.

There are lots of approaches - I find it best to stick to a simple bays
rule and be honest and careful about the implicit assumptions of the
statistical model.  The problem depends on the noise as well as the
signal.  A tone in speech, a tone in a little or a lot of white noise, a
tone in music, one specific tone amother other tones, a tone with a narrow
range of possible frequencies,... they all demand different tests.

PS

Shouldn't this be in comp.dsp?
Hi Maurice,
Thanks for the code for Goertzel algorithm. It's amazing !!!!
But I could not make out why this algo should work .. if you have time
can you please give me a brief explanation as to how this thing works ?

Thanks,
Atri.



Maurice Givens wrote:

> Atri Mandal <mandala@ics.uci.edu> wrote in message news:<40359D7A.12B15FED@ics.uci.edu>... > > Hi, > > I am writing an application to detect a particular frequency(viz. 4.5 > > Khz) in an input audio signal. I have written the code to capture the > > sound using a microphone; but I have to properly design a digital filter > > which will return TRUE as soon as it detects sound of this frequency in > > the input signal. Also my project demands that I have to detect the > > signal by looking at a very small number of samples e.g. 44. So I will > > apply the digital filter on a window of 44 samples of the input signal > > repeatedly till I get a high response indicating that I have got the > > reqd. frequency. > > > > I know DFT/FFT is an option but are there any better algorithms I can > > use for my project. Please suggest the best algorithm that will do my > > work. I am writing the code in C++ so if anyone can point me to existing > > code on the net that will be really helpful. > > > > Thanks, > > Atri. > > Here is Matlab code for the Goertzel algm if you just need a > TRUE/FLASE indication. Set a threshold, and if y is greater than the > threshold, then the frequency is detected. > > x = input vector > f = frequency desired > N = number of samples to test over > Fs = sample frequency > > function y = goert(x,f,N,Fs) > > k = fix(0.5 + N*f/Fs); > w = (2*pi/N)*k; > coeff = 2 * cos(w); > Q1 = 0; > Q2 = 0; > for j = 1:N > Q0 = coeff * Q1 - Q2 + x(j); > Q2 = Q1; > Q1 = Q0; > end > y = sqrt(Q1^2 + Q2^2 - Q1 * Q2 * coeff); > > Here is code to test the alghm. Plot F aganist y to see the detector > output as a function of frequency. > > fdet is the frequency desired > N = number of samples to test over > Fs = sample frequency > > function [F,y] = testgoert(fdet,N,Fs) > > t = 0:1/Fs:((1/Fs)*N - (1/Fs)); > k = 1; > for f = fix(fdet/2:(fdet/2)/15:fdet*2) > x = sin(2*pi*f*t); > y(k) = goert(x,fdet,N,Fs); > F(k) = f; > k = k + 1; > end > y = y(:); > F = F(:); > > Maurice Givens