I have an array with length of 1024.
There are some sine wave data in it.
I want to find the minimum and maximum value of that sine wave and after that i want to compute its amplitude.
The facts that this sine wave is not constant. It is always changing its maximum and minimum values and in beetween.
Every time i get a new measurement and read it from that array buffer, i want to search its min and max value.
(The value ranges are from 0 to 4095. My signal is pulled high because i dont want to get signed measurements so it is usually starting from 500 all the way to 2800-3000.)
I tried the followings:
Code: Select all
int Min_Counter = 0; // THESE ARE NOT NECCESSARY FOR NOW
int Max_Counter = 0;
#define NUM_SAMPLES = 1024; // SIZE OF I2S_ADC_BUFFER
static const inline int Min_Max_Amp(uint16_t* Buffer){ //BUFFER IS i2s_read_buff[NUM_SAMPLES]
int max = 0;
int min = 4095;
for(int i = 0;i<NUM_SAMPLES;i++){ // START TO SEARCH IN THE BUFFER UNTIL ITS END
if((Buffer[i]<Buffer[i+1])&&Buffer[i]<min){ // IF THE CURRENT SAMPLE IS LOWER THEN THE CURRENT+1 AND ITS LOWER THEN THE PREVIOUS MIN, THEN SAVE IT TO min VARIABLE FOR FURTHER COMPUTING!
min = Buffer[i];
Min_Counter++;
}else if((Buffer[i]>Buffer[i+1])&&Buffer[i]>max){ // SAME AS IN MIN JUST REVERSE
max = Buffer[i];
Max_Counter++;
}
} // IF THE LOOP ENDED SUBSTRACT THE MIN FROM THE MAX
int Vamp = max - min;
return Vamp; // RETURN IT FOR PLOTTING
}
Here is some picture from the sine wave in this buffer plotted to my web: