Keep It Simple, Stupid PerlMonks

### Re: How do I find peaks in noisy data?

by TedPride (Priest)
 on Apr 27, 2006 at 06:29 UTC ( #545967=note: print w/replies, xml ) Need Help??

in reply to How do I find peaks in noisy data?

Not that hard. If you want an absolute cutoff (500) rather than percentage (6% of max), just change the lines assigning values to \$cutoff.
```use strict;
use warnings;

my @n = (101, 203, 321, 45, 67, 156, 203, 502, 899, 2003, 5007, 8020,
+7301, 5030, 3045, 1243, 567, 321, 234, 45, 123, 453);
my \$cutoff = .06;

my \$max = 0;
for (@n) {
\$max = \$_ if \$_ > \$max;
}
\$cutoff = \$max * \$cutoff;

for (@n) {
\$_ = 0 if \$_ < \$cutoff;
print \$_ ? '-' x (\$_ * 50 / \$max) : '', "\n";
}
Returns:
```

---
-----
------------
-------------------------------
--------------------------------------------------
---------------------------------------------
-------------------------------
------------------
-------
---

EDIT: Hmm, you're right, the background noise could theoretically vary, as well as the height of the peaks. I guess a more complicated algorithm is needed. Perhaps the cutoff level could be set to some percentage of the closest peak, then any islands remaining would be removed if they had a bad width-height aggregate (this would be the complicated part). The idea is to set the cutoff low enough so you don't lose much of the edges of the islands, but also to remove any islands which are just background noise.

Then any parts which have been removed can be analyzed to see if there's a regular pattern. If there is, that pattern is extrapolated and removed from the remaining islands.

Altogether too much work, imho :) I'm sure there is a utility written in C or C++ somewhere that does this already, probably faster and better than anything we can write on the spur of the moment. Unless what you're trying to do is something simple like automatically trimming sound files, I'd personally just get a third-party utility and write a wrapper for it.

Create A New User
Node Status?
node history
Node Type: note [id://545967]
help
Chatterbox?
 [Cosmic37]: now I have tried another blunder - can anyone explain why I am such a dunderhead? [Cosmic37]: if ( \$line =~ /\$mydt/ ) { print \$line; } [Cosmic37]: I try to match successive date times stored in variable \$mydt [Cosmic37]: I guess it is searching for the string "\$mydt" [Corion]: Indeed cool, erix ;) [Cosmic37]: rather than the value of \$mydt which is a date time strong such as 2016-01-01 12:30:56 [Corion]: Cosmic37: No, but maybe \$mydt doesn't contain what you think it does, or it contains characters that are special in a regular expression? Try if( \$line =~ /\Q\$mydt\E/) { ... for a literal match [Cosmic37]: I mean string grrr [Corion]: Maybe add an else branch in which you print what the values of \$line and \$mydt are? [Cosmic37]: ah thank you I will try

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2017-06-29 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (673 votes). Check out past polls.