<p>We use a data acquisition card to take readings from a device that increases its signal to a peak and then falls back to near the original value. To find the peak value we currently search the array for the highest reading and use the index to determine the timing of the peak value which is used in our calculations.</p>

<p>This works well if the highest value is the peak we are looking for but if the device is not working correctly we can see a second peak which can be higher than the initial peak. We take 10 readings a second from 16 devices over a 90 second period.</p>

<p>My initial thoughts are to cycle through the readings checking to see if the previous and next points are less than the current to find a peak and construct an array of peaks. Maybe we should be looking at a average of a number of points either side of the current position to allow for noise in the system. Is this the best way to proceed or are there better techniques?</p>

<hr>

<p>We do use LabVIEW and I have checked the <a href="http://forums.lavag.org/Peak-Detection-t8499.html" rel="noreferrer">LAVA forums</a> and there are a number of interesting examples. This is part of our test software and we are trying to avoid using too many non-standard VI libraries so I was hoping for feedback on the process/algorithms involved rather than specific code. </p>

[{'answer_id': 3288, 'author': 'Brendan', 'author_id': 199, 'author_profile': 'https://Stackoverflow.com/users/199', 'pm_score': 4, 'selected': True, 'text': '<p>You could try signal averaging, i.e. for each point, average the value with the surrounding 3 or more points. If the noise blips are huge, then even this may not help.</p>\n\n<p>I realise that this was language agnostic, but guessing that you are using LabView, there are lots of pre-packaged signal processing VIs that come with LabView that you can use to do smoothing and noise reduction. The <a href="http://forums.ni.com/" rel="noreferrer">NI forums</a> are a great place to get more specialised help on this sort of thing.</p>\n'}, {'answer_id': 3289, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>You could apply some <a href="http://en.wikipedia.org/wiki/Standard_deviations" rel="nofollow noreferrer">Standard Deviation</a> to your logic and take notice of peaks over x%.</p>\n'}, {'answer_id': 3299, 'author': 'Anthony Cramp', 'author_id': 488, 'author_profile': 'https://Stackoverflow.com/users/488', 'pm_score': 2, 'selected': False, 'text': '<p>I think you want to <a href="http://en.wikipedia.org/wiki/Cross-correlation" rel="nofollow noreferrer">cross-correlate</a> your signal with an expected, exemplar signal. But, it has been such a long time since I studied signal processing and even then I didn\'t take much notice.</p>\n'}, {'answer_id': 3300, 'author': 'John with waffle', 'author_id': 279, 'author_profile': 'https://Stackoverflow.com/users/279', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know very much about instrumentation, so this might be totally impractical, but then again it might be a helpful different direction.  If you know how the readings can fail, and there is a certain interval between peaks given such failures, why not do gradient descent at each interval.  If the descent brings you back to an area you've searched before, you can abandon it.  Depending upon the shape of the sampled surface, this also might help you find peaks faster than search.</p>\n"}, {'answer_id': 24259, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 3, 'selected': False, 'text': '<p>This problem has been studied in some detail. </p>\n\n<p>There are a set of very up-to-date implementations in the <code>TSpectrum*</code> classes of <A href="http://root.cern.ch/" rel="noreferrer">ROOT</A> (a nuclear/particle physics analysis tool). The code works in one- to three-dimensional data.</p>\n\n<p>The ROOT source code is available, so you can grab this implementation if you want.</p>\n\n<p>From the <A href="http://root.cern.ch/root/html/TSpectrum.html" rel="noreferrer">TSpectrum</A> class documentation:</p>\n\n<p>The algorithms used in this class have been published in the following references:</p>\n\n<blockquote>\n  <p>[1] M.Morhac et al.: Background\n  elimination methods for\n  multidimensional coincidence gamma-ray\n  spectra. Nuclear Instruments and\n  Methods in Physics Research A 401\n  (1997) 113-\n  132.</p>\n  \n  <p>[2]  M.Morhac et al.: Efficient one- and two-dimensional Gold\n  deconvolution and its application to\n  gamma-ray spectra decomposition.\n  Nuclear Instruments and Methods in\n  Physics Research A 401 (1997) 385-408.</p>\n  \n  <p>[3]  M.Morhac et al.: Identification of peaks in\n  multidimensional coincidence gamma-ray\n  spectra. Nuclear Instruments and\n  Methods in Research Physics A \n  443(2000), 108-125.</p>\n</blockquote>\n\n<p>The papers are linked from the class documentation for those of you who don\'t have a NIM online subscription.</p>\n\n<hr>\n\n<p>The short version of what is done is that the histogram flattened to eliminate noise, and then local maxima are detected by brute force in the flattened histogram. </p>\n'}, {'answer_id': 38663, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 2, 'selected': False, 'text': '<p>This method is basically from David Marr\'s  book "Vision"</p>\n\n<p>Gaussian blur your signal with the expected width of your peaks.\nthis gets rid of noise spikes and your phase data is undamaged.</p>\n\n<p>Then edge detect (LOG will do)</p>\n\n<p>Then your edges were the edges of features (like peaks).\nlook between edges  for peaks, sort peaks by size, and you\'re done.</p>\n\n<p>I have used variations on this and they work very well.</p>\n'}, {'answer_id': 39381, 'author': 'Adam Hollidge', 'author_id': 4069, 'author_profile': 'https://Stackoverflow.com/users/4069', 'pm_score': 0, 'selected': False, 'text': '<p>Is there a qualitative difference between the desired peak and the unwanted second peak? If both peaks are "sharp" -- i.e. short in time duration -- when looking at the signal in the frequency domain (by doing FFT) you\'ll get energy at most bands. But if the "good" peak reliably has energy present at frequencies not existing in the "bad" peak, or vice versa, you may be able to automatically differentiate them that way.</p>\n'}, {'answer_id': 44357, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 6, 'selected': False, 'text': '<p>There are lots and lots of classic peak detection methods, any of which might work.  You\'ll have to see what, in particular, bounds the quality of your data.  Here are basic descriptions:</p>\n\n<ol>\n<li><p>Between any two points in your data, <code>(x(0), y(0))</code> and <code>(x(n), y(n))</code>, add up <code>y(i + 1) - y(i)</code> for <code>0 &lt;= i &lt; n</code> and call this <code>T</code> ("travel") and set <code>R</code> ("rise") to <code>y(n) - y(0) + k</code> for suitably small <code>k</code>.  <code>T/R &gt; 1</code> indicates a peak.  This works OK if large travel due to noise is unlikely or if noise distributes symmetrically around a base curve shape.  For your application, accept the earliest peak with a score above a given threshold, or analyze the curve of travel per rise values for more interesting properties.</p></li>\n<li><p>Use matched filters to score similarity to a standard peak shape (essentially, use a normalized dot-product against some shape to get a cosine-metric of similarity)</p></li>\n<li><p>Deconvolve against a standard peak shape and check for high values (though I often find 2 to be less sensitive to noise for simple instrumentation output).</p></li>\n<li><p>Smooth the data and check for triplets of equally spaced points where, if <code>x0 &lt; x1 &lt; x2, y1 &gt; 0.5 * (y0 + y2)</code>, or check Euclidean distances like this:  <code>D((x0, y0), (x1, y1)) + D((x1, y1), (x2, y2)) &gt; D((x0, y0),(x2, y2))</code>, which relies on the triangle inequality.  Using simple ratios will again provide you a scoring mechanism.</p></li>\n<li><p>Fit a very simple 2-gaussian mixture model to your data (for example, Numerical Recipes has a nice ready-made chunk of code).  Take the earlier peak.  This will deal correctly with overlapping peaks.</p></li>\n<li><p>Find the best match in the data to a simple Gaussian, Cauchy, Poisson, or what-have-you curve.  Evaluate this curve over a broad range and subtract it from a copy of the data after noting it\'s peak location.  Repeat.  Take the earliest peak whose model parameters (standard deviation probably, but some applications might care about kurtosis or other features) meet some criterion.  Watch out for artifacts left behind when peaks are subtracted from the data.\nBest match might be determined by the kind of match scoring suggested in #2 above.</p></li>\n</ol>\n\n<p>I\'ve done what you\'re doing before:  finding peaks in DNA sequence data, finding peaks in derivatives estimated from measured curves, and finding peaks in histograms.</p>\n\n<p>I encourage you to attend carefully to proper baselining.  Wiener filtering or other filtering or simple histogram analysis is often an easy way to baseline in the presence of noise.</p>\n\n<p>Finally, if your data is typically noisy and you\'re getting data off the card as unreferenced single-ended output (or even referenced, just not differential), and if you\'re averaging lots of observations into each data point, try sorting those observations and throwing away the first and last quartile and averaging what remains.  There are a host of such outlier elimination tactics that can be really useful.</p>\n'}, {'answer_id': 33509689, 'author': 'Jean-Paul', 'author_id': 2431885, 'author_profile': 'https://Stackoverflow.com/users/2431885', 'pm_score': 3, 'selected': False, 'text': '<p>I would like to contribute to this thread an algorithm that <a href="https://stackoverflow.com/questions/22583391/peak-detection-in-realtime-timeseries-data">I have developed myself</a>:</p>\n<p>It is based on the principle of <a href="https://en.wikipedia.org/wiki/Statistical_dispersion" rel="nofollow noreferrer">dispersion</a>: if a new datapoint is a given x number of standard deviations away from some moving mean, the algorithm signals (also called <a href="https://en.wikipedia.org/wiki/Standard_score" rel="nofollow noreferrer">z-score</a>). The algorithm is very robust because it constructs a <em>separate</em> moving mean and deviation, such that signals do not corrupt the threshold. Future signals are therefore identified with approximately the same accuracy, regardless of the amount of previous signals. The algorithm takes 3 inputs: <code>lag = the lag of the moving window</code>, <code>threshold = the z-score at which the algorithm signals</code> and <code>influence = the influence (between 0 and 1) of new signals on the mean and standard deviation</code>. For example, a <code>lag</code> of 5 will use the last 5 observations to smooth the data. A <code>threshold</code> of 3.5 will signal if a datapoint is 3.5 standard deviations away from the moving mean. And an <code>influence</code> of 0.5 gives signals <em>half</em> of the influence that normal datapoints have. Likewise, an <code>influence</code> of 0 ignores signals completely for recalculating the new threshold: an influence of 0 is therefore the most robust option.</p>\n<p>It works as follows:</p>\n<p><em><strong>Pseudocode</strong></em></p>\n<pre class="lang-r prettyprint-override"><code># Let y be a vector of timeseries data of at least length lag+2\n# Let mean() be a function that calculates the mean\n# Let std() be a function that calculates the standard deviaton\n# Let absolute() be the absolute value function\n\n# Settings (the ones below are examples: choose what is best for your data)\nset lag to 5;          # lag 5 for the smoothing functions\nset threshold to 3.5;  # 3.5 standard deviations for signal\nset influence to 0.5;  # between 0 and 1, where 1 is normal influence, 0.5 is half\n\n# Initialise variables\nset signals to vector 0,...,0 of length of y;   # Initialise signal results\nset filteredY to y(1,...,lag)                   # Initialise filtered series\nset avgFilter to null;                          # Initialise average filter\nset stdFilter to null;                          # Initialise std. filter\nset avgFilter(lag) to mean(y(1,...,lag));       # Initialise first value\nset stdFilter(lag) to std(y(1,...,lag));        # Initialise first value\n\nfor i=lag+1,...,t do\n  if absolute(y(i) - avgFilter(i-1)) &gt; threshold*stdFilter(i-1) then\n    if y(i) &gt; avgFilter(i-1)\n      set signals(i) to +1;                     # Positive signal\n    else\n      set signals(i) to -1;                     # Negative signal\n    end\n    # Adjust the filters\n    set filteredY(i) to influence*y(i) + (1-influence)*filteredY(i-1);\n    set avgFilter(i) to mean(filteredY(i-lag,i),lag);\n    set stdFilter(i) to std(filteredY(i-lag,i),lag);\n  else\n    set signals(i) to 0;                        # No signal\n    # Adjust the filters\n    set filteredY(i) to y(i);\n    set avgFilter(i) to mean(filteredY(i-lag,i),lag);\n    set stdFilter(i) to std(filteredY(i-lag,i),lag);\n  end\nend\n</code></pre>\n<hr />\n<h1>Demo</h1>\n<p><a href="https://i.imgur.com/LFvEM2Y.gif" rel="nofollow noreferrer"><img src="https://i.imgur.com/LFvEM2Y.gif" alt="Demonstration of robust thresholding algorithm" /></a></p>\n<h1><a href="https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data/22640362">&gt; For more information, see original answer</a></h1>\n'}]
['https://Stackoverflow.com/questions/3260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/186/']