At 10:27 AM 3/19/2010, Olin Lathrop wrote: >Dario Greggio wrote: > > Hmmm, I may be missing something but.. I can't get any result out of > > the IIR filtering, nor the raw samples, nor their average... > > > > Well, I simulated 20 samples (one full sine wave) on an Excel > > spreadsheet, and tried shifting the sine with respect to the sampling > > point, and the result does change a lot - not good. > > > > Can you suggest where I'm going wrong? > >It's not clear what exactly you're doing. You have 20 samples of a sine >wave, but what are you trying to get from those and what are you getting? > >If this signal contains harmonics, did you remember to apply a anti-aliasing >filter to get rid of the ones your sample stream can't represent? Hi there, Olin. He's referring to a post I made where I showed my IIR filter (written in assembler). I'll paste a copy below. He's converting it to C - I'm not adequately competent to determine if there is an error in translation or not. Copy starts: This routine runs in my background task which executes every 1.024ms. "rampg1" & "rampg0" are macros that set the appropriate page bits. Those are a single instruction for the 12f675 but would expand to two instructions for PICs with 4 RAM pages. BGTMP1,2 are temporary RAM bytes for use by the background routine and are not persistent. A/D is configured for 10 bit result (right-justified). The a/d start command occurs when the background task is about to finish and is not shown here. ;AD routines: 1 ch with 16 sample IIR filter. Choose 16 samples 'cuz ; 16 * 1.024ms ~~ 16.667ms (period of 60Hz signal). ; Would need to go to more than 20 samples for 50Hz signal (use 32 samples). ;divide sum by 16 AD1:AD1L /16 -> BGTMP2:BGTMP1 swapf AD1L,W ;LO byte andlw b'00001111' movwf BGTMP1 ;hi nib AD1L -> lo nib BGTMP (>>4) swapf AD1,W ;HI byte movwf BGTMP2 ;swap nibs AD1 -> BGTMP2 (>>4) andlw b'11110000' ;clr lo nib in W xorwf BGTMP2,F ;clr hi nib BGTMP2 (XOR value with itself is 0) iorwf BGTMP1,F ;lo nib AD1 -> hi nib BGTMP1 (>>4) ;subtract old sample from sum movfw BGTMP1 ;LS byte subwf AD1L,F ;F-W ->d movfw BGTMP2 skpc incfsz BGTMP2,W subwf AD1,F ;add new sample to sum rampg1 movfw RB1(ADRESL) rampg0 addwf AD1L,F movfw ADRESH skpnc incfsz ADRESH,W addwf AD1,F ;done. Do all line voltage tests on sum (AD1,AD1L) dwayne -- Dwayne Reid Trinity Electronics Systems Ltd Edmonton, AB, CANADA (780) 489-3199 voice (780) 487-6397 fax www.trinity-electronics.com Custom Electronics Design and Manufacturing -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist