0

I have 8 data points that form the peak of a partial sine wave. I am trying to fit these to get an equation so I discover the point of the true maximum position (which most likely lies between the data points). The coding will be in C. Does anyone have any info on algorithms or ideally code samples?

Dirk Bruere
  • 237
  • 2
  • 15

2 Answers2

2

Since the data points are all near a maximum, the wave y = A*sin(B*x + C) + D can be approximated as a parabola much like the first 2 terms of cos(x) = (1.0 - x*x/2! + ...).

So find the best fit parabola for the 8 data points and calculate the maximum. C- Peak detection via quadratic fit

Lots of google examples exist. Example

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Provided your sample-values form a "hump", i.e. increasing followed by decreasing samples, you could try viewing the samplevalues as "weights" and compute the "center of gravity":

float cog = 0f;
for (i=0; i<num_samples; ii+) {
    cog += i * samples[i];
}
cog /= num_samples;

I've used that in similar cases in the past.

NOTE: This scheme only works if the set of samples used contain a single peak, which the question phrasing certainly made me think was the case. Finding locations of interest can easily be done by monitoring, if sample values are increasing or decreasing, selecting an "interesting" range of samples and computing the peak location as described.

Also note, that if the actual goal is to determine the sine wave phase or frequency of an input signal, it would be a lot better to correlate the signal against reference set of sine-waves (in other words, do a Fourier transform).

S.C. Madsen
  • 5,100
  • 5
  • 32
  • 50
  • As coded above, it doesn't work, but I like the idea and will go with it as a test – Dirk Bruere Nov 03 '14 at 15:10
  • Doesn't work if several peak values are close together – Dirk Bruere Nov 03 '14 at 16:20
  • Perhaps my "opening remark" was not clear enough, but the code provided estimates the sub-sample location of a single "hump" or "peak", given a set of samples distributed around that peak. Finding each peak, and selecting a sample-set around it is assumed to be done elsewhere – S.C. Madsen Nov 03 '14 at 19:48