Sine wave amplitude computing question(Searching for min and max)
Posted: Wed Oct 16, 2019 11:57 am
Hello guys!
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:
Okay this is cool, i get the amplitude of my sine wave. But the problem is that this function is going trought in all the periods on the buffer and it is saving just the last min and max from the sine wave.
Here is some picture from the sine wave in this buffer plotted to my web:
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: