Sine wave amplitude computing question(Searching for min and max)

zekageri
Posts: 43
Joined: Mon Sep 03, 2018 11:04 am

Sine wave amplitude computing question(Searching for min and max)

Postby zekageri » 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:

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
}
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:
Attachments
Képernyőfelvétel (31).png
Képernyőfelvétel (31).png (86.46 KiB) Viewed 5299 times
Képernyőfelvétel (30).png
Képernyőfelvétel (30).png (89.49 KiB) Viewed 5299 times

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: Sine wave amplitude computing question(Searching for min and max)

Postby tommeyers » Wed Oct 16, 2019 1:18 pm

Do you want to return a list of highs and lows for a sample where you are now returning one high and one low
IT Professional, Maker
Santiago, Dominican Republic

zekageri
Posts: 43
Joined: Mon Sep 03, 2018 11:04 am

Re: Sine wave amplitude computing question(Searching for min and max)

Postby zekageri » Wed Oct 16, 2019 1:29 pm

tommeyers wrote:
Wed Oct 16, 2019 1:18 pm
Do you want to return a list of highs and lows for a sample where you are now returning one high and one low
Actually i don't know if it is better if i measure all the mins and maxs in the buffer and get the average for both of them and compute with that variable or just let it be and compute with the last min and last max.

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: Sine wave amplitude computing question(Searching for min and max)

Postby tommeyers » Wed Oct 16, 2019 11:47 pm

Average may eliminate noise and give a better point estimate for avg. Maybe standard deviation too.
IT Professional, Maker
Santiago, Dominican Republic

Who is online

Users browsing this forum: No registered users and 47 guests