I have connected a sine wave of amplitude 3V with 1.5V shifted above the zero to 12-bit ADC channel by using dsPIC33FJ32MC204 controller and stored in an array. I want to detect the peak for each interval, so please give me any suggestion on it. I have just posted my logic for max value detection out of five samples. I am getting output as a zero.
void read_adc_Voltage()
{
int arr[100];
int arr1[100];
int max = arr[0];
arr[0]=0;
int i,j=0;
int count = 6;
for (i=1;i<count;i++)
{
var=(ain1Buff[sampleCounter]);
voltage=var*((float)3.3/(float)4095);
arr[j] = voltage;
if(arr[j] > max)
{
max = arr[j];
}
j++;
}
sprintf(data1,"%.2f",max);
LCD_String_xy(1,1,data1);
sampleCounter++;
if(sampleCounter==6)
{
sampleCounter=0;
}
}
Define what you mean by various terms like peak and interval, and ask or decide, and specify, your criteria for other conditions.
You can track the maximum value by updating MaxValue whenever SampleValue is greater than the previous MaxValue in that interval. Similarly with MinValue.
For example is your criterion for peak that SampleValue should Rise by a certain amount, followed by a Fall of a certain amount? Or is it that SampleValue should exceed a certain fixed Threshold? Or something more complicated? They will have different consequences when the level of the signal fluctuates with noise; you may need to set up initial conditions; and perhaps to consider whether the system can lock into an undesirable condition like Threshold too high or too low. The more complicated the solution, the more the opportunity for undesirable conditions so KISS.
Same sort of considerations for when there is a new interval. Is it a fixed time? - use a timer. Is it related to the sample frequency? - count the number of samples. Is it to begin after each peak, perhaps with a minimum interval? - reset your timing or counting and MaxValue and MinValue after each peak, and prohibit determination of a peak until your MinTime or MinCount is passed.
Use the simplest solution that will deal with your signal, taking noise etc into account. Might get quite complicated if you let it.