calculate offset sinusoidal
Jack// ani wrote:
Wrote a little code on CCS PIC, nothing fancy standard deviation with 30 samples at work as discussed by FvM.
Have run out of 16F877A, so couldn't burnt and tested it. Any comments, mistakes, improvements are always appreciated...
#include <16F877A.h>
#device ADC=10
#include <math.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
void main()
{
int16 value, samples[29]; //Value is a 10bit data sample
int i;
float volts;
setup_port_a(ALL_ANALOG); // All the 8 channels are analog
setup_adc(ADC_CLOCK_INTERNAL); // Internal ADC Clock
set_adc_channel(0); // RA0/AN0(Pin2) is analog input channel
while(1)
{
for(i=0;i<=29;i++)
{
samples=read_adc();
delay_us(666);
}
//SQUARE OF SAMPLES
for(i=0;i<=29;i++)
{
samples=samples*samples; // Sqaure
value=samples+value; // Sum total
}
value = value/30; // Mean of 30 samples
value = sqrt(value); // Square root
volts=(float)value*0.0048828125; // 1024 discrete levels, 5/1024 = 0.0048828125
printf("Voltage is : %3.3f\n",volts);// Print upto 3 decimal places
delay_ms(500);
}
}
I would like to add a few things
1- I suggest initialize "vaue" variable to 0
2- Do following to get rid of dc level in the signal(for example if the signal has been biased)
a. DC_Average = sumOfSample/NoOfSamples
b. For each sample samples = PositiveValueOf(samples - DC_Average)
c. Do the rest as in //SQUARE OF SAMPLES
Dear All
The above information is usefull to me . Can i have more details about the point(b)
Please say me in details what i should with DC Average and what would be the value.
Please say me in detail
And if i have a variable Frequency how could i sample the amplitude.
Waiting for u reply
Regards
Prabakaran