PDA

View Full Version : Count, measure peak values and average them for finite duration ~ sinusoidal pulse?



jellis00
- 11th June 2011, 01:24
I have an application in which I have to use an ADC for input of an analog signal on which an almost sinusoidal pulse of finite duration needs to be measured. Typical pulsed signal is 7-8 cycles (up to 500 Hz) where the peak values are not always equal, so it is close to a sinusoid, but not exactly sinusoidal. I need to use PICBASIC code to sample the analog input at Nyquist (1200 Hz?) with an ADC input to my PIC 12F676 and then count the number of peaks in the waveform, measure each of the peaks, and average the counted peak values. Can anyone give me tips on how to do this...or better yet some example PICBASIC PRO code?

HenrikOlsson
- 11th June 2011, 08:22
Hi,
I'd probably sample at atleast 10 times the input frequency dependning on what else the PIC has to do while doing this thing.

Here's one idea, from the top of my head, untested....

Sample VAR WORD
LastSample VAR WORD
Average VAR WORD
Peaks VAR WORD [8]
i VAR BYTE
j VAR BYTE

' You might want to insert some form of "trigger" mechanism here.
' Perhaps sample the input in a loop and wait until there's a
' positive going trend or something - then move on the following:

For i = 0 to 7 ' Get 8 peaks

LastSample = 0 ' Set up
Average = 0 ' Set up
Sample = 0 ' Set up

DO
LastSample = Average ' Save previous sample
Average = 0 ' Reset accumulator

For j = 0 to 3 ' Take 4 samples quickly
ADCIN 1, Sample ' Get current input
Average = Average + Sample ' Add to accumulator
Next ' Next sample

Average = Average >> 2 ' Divide by 4
UNTIL Average < LastSample ' Stop if this sample is lower than the last one.

Peaks[i] = LastSample ' Current sample is lower than last one, we've found the peak.

NEXT

For i = 0 to 7
HSEROUT ["Peak number", DEC (i+1), ": ", DEC Peaks[i], " ADC counts", 13]
Next

/Henrik.