PDA

View Full Version : Frecuency Detection



Josuetas
- 9th August 2007, 21:51
I just dont get that Goertzel algorithm or the application from Microchip.

What i intend is this, i have 4 frequencies 350,420, 480 and 620 Hz. I need to find if a certain frecuency is happening.

These are by the way typical Multifrequency Signaling frequencies so detection has to be fast, like 40mS to determine wheter the frequency is present or not in a given signal.

Microhip has an app about DTMF detection that seems to have a lead, i just need to find different Frequencies than those in DTMF. The Microchip app is made to 18F devices, i intend to use a cheaper 16F628A. Would this be impossible?.

I tried using external components like a LM567 but i would need 4, since each lm567 can only get one of the signals, due to bandwidth restrains.

Ok has anyone done something similar?, can anyone explain the algorithm?

Thanks for any help!!:D

mackrackit
- 10th August 2007, 15:02
Have you tried PBP "count"? http://www.picbasic.co.uk/forum/showthread.php?t=6865

Bruce
- 10th August 2007, 18:21
Measuring a frequency is pretty straight forward, and can be done on just about
any PIC with a spare input pin.

Here are a few ideas.

1. Use the COUNT command.
2. Use TIMER1.
3. Use the hardware capture module.

And here's a simple test program using all 3 options. Tested on a 16F627A.


@ DEVICE MCLR_OFF, INTRC_OSC, WDT_OFF, LVP_OFF, BOD_ON, PWRT_ON, PROTECT_OFF
DEFINE OSC 4

T1 VAR WORD
T2 VAR WORD
TRISB = %00001000 ' RB3 = input

T1CON = 0
TMR1L=0
TMR1H=0

' GOTO Test3 ' using COUNT
' GOTO Test2 ' using capture
' GOTO Test1 ' using TMR1 with pin state watch

Test1:
WHILE PORTB.3=1 ' noise filter
WEND
WHILE PORTB.3=0 ' wait for low to high edge
WEND
T1CON=1 ' start TMR1 on rising edge
WHILE PORTB.3=1 ' wait for high to low edge
WEND
WHILE PORTB.3=0 ' wait for 2nd rising edge (end of period)
WEND
T1CON=0 ' stop TMR1

' Record the period as T1.
T1.HighByte = TMR1H
T1.LowByte = TMR1L
HSEROUT ["Period = ",DEC T1,13,10]
PAUSE 10000
TMR1L=0
TMR1H=0
GOTO Test1
' Serial from PIC ' Shown on O-Scope
' Period = 3143 ' 315 Hz
' Period = 2403 ' 412 Hz
' Period = 2113 ' 468 Hz
' Period = 1643 ' 603 Hz

Test2:
'Configure capture for rising edge.
CCP1CON = %00000101 ' Capture mode, capture on rising edge
'Setup & start Timer1.
T1CON = 1 ' TMR1 prescale=1, clock=Fosc/4, TMR1=on.

Loop:
'Wait for the capture flag bit to be set.
WHILE PIR1.2 = 0
WEND

'Record the first capture value as T1.
T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

'Clear the capture flag.
PIR1.2 = 0

'Wait for the capture flag to be set a 2nd time.
WHILE PIR1.2 = 0
WEND

'Record the 2nd capture value as T2.
T2.HighByte = CCPR1H
T2.LowByte = CCPR1L

'Clear the capture flag
PIR1.2 = 0

'T2-T1 = the period in uS.
HSEROUT ["Period = ",DEC T2-T1,13,10]
PAUSE 1000
GOTO Loop
' Serial from PIC ' Shown on O-Scope
' Period = 3149 ' Freq = 315 Hz
' Period = 2410 ' Freq = 412 Hz
' Period = 2120 ' Freq = 468 Hz
' Period = 1648 ' Freq = 603 Hz

Test3:
COUNT PORTB.3,20,T1 'Count low to high transitions for 20mS
HSEROUT ["Count = ",DEC T1,13,10]
PAUSE 10000
GOTO Test3
' Serial from PIC ' Shown on O-Scope
' Count = 6 ' 315 Hz
' Count = 8 ' 412 Hz
' Count = 9 ' 468 Hz
' Count = 12 ' 603 Hz
' Resolution with COUNT isn't that great, but it works.
' 20mS/count value = the approximate period.

END
An 8-pin PIC was used for the frequency generator.

Capture could be used with the capture flag generating an interrupt if
you prefer not to wait for state changes.

I'm sure there are several more ways to do this, but this should get you
started.

Josuetas
- 10th August 2007, 21:02
Your ideas are pretty good Bruce, but maybe i wasnt clear enough, this frequencies can apper at the same time. And there can even be more components in the signal, like voice.

Consider DTMF detection with uC but with these other frequencies, in fact these are the frequencies of multifrequency signaling. My guess is that this conventional procedures wouldnīt be as close to real as in your experiment. or Maybe not? i will anyway try.

Thanks

ronsimpson
- 12th August 2007, 00:35
Josuetas,

I think a hardware frequency detector like the LM567 or a digital switched capacitor filter is the way to go.

To ask a PIC16F to do a DSP function is pushing it. Last time I did something like this I had to move up to the PIC18F family because it handles RAM better and it multiplies much faster. If you only wanted to detect 350hz out of much noise and voice then I would try it. To detect 4 frequencies at the same time is no going to happen totally in software. I think you should move up to a DSP chip if you need to do it in software. Even then 4 frequencies at one time is a project!

Josuetas
- 13th August 2007, 16:43
i would need 4 LM567, i already have this solution made and it works fine, but
the size and cost are excesive.

I am working on it right now, i have read AN257 from microchip wich has a Discrete Fourier Transform Implementation form uC (18F) with 1 Bit ADC that seems simple.

I will post my results anyway, thanks for your advice.

mikem
- 15th August 2007, 02:35
DTMF detection can be done with a PIC, just Check out the web page below it will give you some more insight as to how it's done.



Mike


http://www.geocities.com/constantinescuradu/content/dtmf.htm

Josuetas
- 15th August 2007, 05:30
Amazingly: there is no math at all in the final algorithm. I am having real fun doing this stuff by the way.

In short words the math formula of DFT is a simple comparision of two bits in N samples, when the sample is passed through a 1bit A/D converter (Zero cross detector for example).

What the algorithm does is:


if RealSample = StoredExpected then
Energy = Energy + 1
else
Energy = Energy - 1
endif


The stored expected is a representation of 1`s and 0īs of the signal, considering the samplig frecuency.

i.e. Stored Expected
11111000001111100000111110000011111000001111100000 111110000011111


This must be done with each sample and one can choose like 114 samples (any number, the bigger the better).

The final Energy its a great representation of the existence of the Detected Frequency in the signal. a simple excel worksheet amazed me, this is really a great filter!!

You could even try to trick the algorithm by adding two or three more frequencies at the same time and it will GET the result!

Result so far:
I am getting the greatest energy around 300 with a F applied of 350. Possible problem with optimization of code or large interrupt routine.

I am attaching the excel sheet that amazed me in case someone is interested, you can move frequency and phase of two frequencies and watch the resultant Wave, and also watch the resultant energy, wich would show that the frequency is detected.

Will post code so far tomorrow

Joe Rocci
- 4th February 2010, 02:01
This intrigues me. Did you ever get any further with it?