Jump to content
43oh

Problem facing while detecting peak


Recommended Posts

Hi everybody

I am doing small task of finding peak value of triangular wave. I am using MSP430G2553 launchpad with Energia software. I am reading value simply using analogRead() function.

ADC is having 200ksps sampling rate. When i applied 3KHz wave then result is ok but above this frequency it is showing wrong result. My code is as follows.

 

I set a threshold as per my application and reading three values using for loop and finding the maximum from that. But for higher frequencies when i read 3 values then it passes the threshold value and my maximum is coming less than threshold value.

So here ADC speed is good but my reading values and finding maximum; takes more time. Any how my max value should be greater than threshold value. So how can we achieve this?

can we use buffer for storing ADC values.

 

void setup()
{
Serial.begin(9600);
pinMode(GREEN_LED, OUTPUT);
}

 

 

void print_peak(int u)
{
  for(p=0; p<3; p++)
  {
    sensorValue=analogRead(A3);
    Serial.print("SensorValue: ");
    Serial.print(sensorValue);
    Serial.print("\n");
    (sensorValue>maxValue)?(maxValue=sensorValue):(maxValue=maxValue);
  }
  Serial.print("\t\tmaxValue: ");
  Serial.print(maxValue);
  maxValue=0.0;
  Serial.print("\n");
}

 

 

void loop()
{
  input=analogRead(A3);
  if(input>threshold)
  {
  print_peak(input);
  digitalWrite(GREEN_LED, HIGH);
  }
  else digitalWrite(GREEN_LED, LOW);
}

 

 

Link to post
Share on other sites

Another way to approach a peak detection task is in the analog domain.  With the proliferation of capable, inexpensive MCUs sometimes the simple tried and true analog solutions are not considered these days. That said, it may or may not make sense for your application but I thought I would throw it out there. Please see the discussion in the link below for some of the pros and cons. 

 

http://www.planetanalog.com/author.asp?section_id=396&doc_id=562072

Link to post
Share on other sites

Actually when value is above threshold, it reads 3 readings but for frequency greater than 3 KHz these readings showing value less than threshold value. So i cant get the peak value. I want maximum value at least equal to threshold but sometimes it is showing less than threshold.
How to store adc readings in buffer??
i think if somehow i able to store these reading then by comparing with threshold i could get the max value.

Link to post
Share on other sites

What is your actual sampling frequency?  With this code speed of the ADC is least of your worries.

Print takes a long time, so would try to optimize that first (i.e., move it out of the loop if you can - just record the values in the loop, then worry about finding the maximum and displaying it).

 

Once you have gotten the prints out of the way, if it still isn't fast enough could look at inlining the reads/writes.  (The arduino/Energia calls are nice for abstraction and portability, but they are slow.)

 

If you wanted to get fancier about it (and your threshold was amenable) you might be able to use the comparator. 

It would give you an interrupt when threshold reached, then start looking for the peak (so you could sleep most of the time).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...