Scott Dattalo wrote: > > Paul Mathews wrote: > > ... The correct way to detect a 2kHz square wave. However, Paul > don't forget the quadrature waveform! It's possible that the incoming > signal and the square wave you're beating it against (i.e. multiplying) > are 90 degrees out of phase. In which case the dot product is zero. > > I've been looking at this fairly closely in the last couple of weeks > and I've come up with a VERY SIMPLE algorithm that will perform the > filtering Paul describes. It also takes care of the quadrature waveform > too. I originally was going to post the theory too, but it's too long > for the current PIC List temperament. Those of you who enjoy the > theoretical enlightenment then drop me a line. > > If you have seen the theory then..... > Finally, all of the above can be combined to create the following > (UNTESTED) code. It takes 15 cycles/sample regardless of the path > that the code takes (recall the recent "Isochronous Code" thread). > > TONE_FREQ EQU 2000 ;Hz > SAMPLING_TIME EQU 15 ;uS > RESOLUTION EQU 2<<13 ;8192 > DPH EQU SAMPLING_TIME*TONE_FREQ*RESOLUTION/1000000 > Qbit_c EQU 13-8 ;Bit 5 > Qbit_s EQU 12-8 ;Bit 4 > > start: > MOVLW NUM_OF_SAMPLES > MOVWF samples > samp_loop: > MOVLW DPH > ADDWF ph_lo,F ;Low byte of phase > SKPNC > INCF ph_hi,F > > BTFSS INPUT_PORT,INPUT_BIT > goto low > > ;Input is high > BTFSC ref_wave,Qbit_c ; > INCF DFT,F ;c=1,s=x,in=1 > nop > goto next > > low: > ;Input is low > BTFSS ref_wave,Qbit_c ; > goto $-2 > BTFSS ref_wave,Qbit_s > INCF DFT_s,F ;c=1,s=0,in=0 > nop > next > ;Put a delay loop here if your crystal frequency>4Mhz > > DECFSZ samples,F > goto samp_loop > > ;At this point the variable "DFT" has the sum of both > ;quadratures. We can decide/experiment with thresholds > ;to see how well they correlate to actual signal. > ; > ; Do some more system stuff... > > goto start > > Scott > > PS The theory behind this is almost identical to the > theory behind the DTMF decoding. I'm glad that I started things off....however, note that the algorithm I sketched out (admittedly sketchy and non-optimal) has starting edge detection, so I don't understand your point about the quadrature wave. Upon calculating a dot product of zero, the process restarts. I've implemented more sophisticated versions of this algorithm in many ways: beginning in 1978 using 9601 monostable multivibrators, in 1980 using custom ASICs, in 1982 using microcontrollers, in 1992 using PGA, etc., so I'm quite confident that it works. -- Paul Mathews, consulting engineer AEngineering Co. optoeng@whidbey.com non-contact sensing and optoelectronics specialists