@@ -8,7 +8,7 @@ const defaultOptions = {
88 polynomial : 3
99 } ,
1010 minMaxRatio : 0.00025 ,
11- broadRatio : 0.00 ,
11+ broadRatio : 0.0 ,
1212 maxCriteria : true ,
1313 smoothY : true ,
1414 realTopDetection : false ,
@@ -47,10 +47,8 @@ function gsd(x, yIn, options) {
4747 // We have to know if x is equally spaced
4848 var maxDx = 0 ;
4949
50-
5150 var minDx = Number . MAX_VALUE ;
5251
53-
5452 var tmp ;
5553 for ( let i = 0 ; i < x . length - 1 ; ++ i ) {
5654 tmp = Math . abs ( x [ i + 1 ] - x [ i ] ) ;
@@ -88,16 +86,40 @@ function gsd(x, yIn, options) {
8886 let dY , ddY ;
8987 if ( ( maxDx - minDx ) / maxDx < 0.05 ) {
9088 if ( options . smoothY ) {
91- Y = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 0 } ) ;
89+ Y = SG ( y , x [ 1 ] - x [ 0 ] , {
90+ windowSize : sgOptions . windowSize ,
91+ polynomial : sgOptions . polynomial ,
92+ derivative : 0
93+ } ) ;
9294 }
93- dY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 1 } ) ;
94- ddY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 2 } ) ;
95+ dY = SG ( y , x [ 1 ] - x [ 0 ] , {
96+ windowSize : sgOptions . windowSize ,
97+ polynomial : sgOptions . polynomial ,
98+ derivative : 1
99+ } ) ;
100+ ddY = SG ( y , x [ 1 ] - x [ 0 ] , {
101+ windowSize : sgOptions . windowSize ,
102+ polynomial : sgOptions . polynomial ,
103+ derivative : 2
104+ } ) ;
95105 } else {
96106 if ( options . smoothY ) {
97- Y = SG ( y , x , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 0 } ) ;
107+ Y = SG ( y , x , {
108+ windowSize : sgOptions . windowSize ,
109+ polynomial : sgOptions . polynomial ,
110+ derivative : 0
111+ } ) ;
98112 }
99- dY = SG ( y , x , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 1 } ) ;
100- ddY = SG ( y , x , { windowSize : sgOptions . windowSize , polynomial : sgOptions . polynomial , derivative : 2 } ) ;
113+ dY = SG ( y , x , {
114+ windowSize : sgOptions . windowSize ,
115+ polynomial : sgOptions . polynomial ,
116+ derivative : 1
117+ } ) ;
118+ ddY = SG ( y , x , {
119+ windowSize : sgOptions . windowSize ,
120+ polynomial : sgOptions . polynomial ,
121+ derivative : 2
122+ } ) ;
101123 }
102124
103125 const X = x ;
@@ -128,8 +150,10 @@ function gsd(x, yIn, options) {
128150 // filter based on derivativeThreshold
129151 if ( Math . abs ( dY [ i ] ) > options . derivativeThreshold ) {
130152 // Minimum in first derivative
131- if ( ( dY [ i ] < dY [ i - 1 ] ) && ( dY [ i ] <= dY [ i + 1 ] ) ||
132- ( dY [ i ] <= dY [ i - 1 ] ) && ( dY [ i ] < dY [ i + 1 ] ) ) {
153+ if (
154+ ( dY [ i ] < dY [ i - 1 ] && dY [ i ] <= dY [ i + 1 ] ) ||
155+ ( dY [ i ] <= dY [ i - 1 ] && dY [ i ] < dY [ i + 1 ] )
156+ ) {
133157 lastMin = {
134158 x : X [ i ] ,
135159 index : i
@@ -141,8 +165,10 @@ function gsd(x, yIn, options) {
141165 }
142166
143167 // Maximum in first derivative
144- if ( ( dY [ i ] >= dY [ i - 1 ] ) && ( dY [ i ] > dY [ i + 1 ] ) ||
145- ( dY [ i ] > dY [ i - 1 ] ) && ( dY [ i ] >= dY [ i + 1 ] ) ) {
168+ if (
169+ ( dY [ i ] >= dY [ i - 1 ] && dY [ i ] > dY [ i + 1 ] ) ||
170+ ( dY [ i ] > dY [ i - 1 ] && dY [ i ] >= dY [ i + 1 ] )
171+ ) {
146172 lastMax = {
147173 x : X [ i ] ,
148174 index : i
@@ -155,10 +181,11 @@ function gsd(x, yIn, options) {
155181 }
156182
157183 // Minimum in second derivative
158- if ( ( ddY [ i ] < ddY [ i - 1 ] ) && ( ddY [ i ] < ddY [ i + 1 ] ) ) {
184+ if ( ddY [ i ] < ddY [ i - 1 ] && ddY [ i ] < ddY [ i + 1 ] ) {
159185 // TODO should we change this to have 3 arrays ? Huge overhead creating arrays
160186 minddY [ minddYLen ++ ] = i ; // ( [X[i], Y[i], i] );
161- broadMask [ broadMaskLen ++ ] = Math . abs ( ddY [ i ] ) <= options . broadRatio * maxDdy ;
187+ broadMask [ broadMaskLen ++ ] =
188+ Math . abs ( ddY [ i ] ) <= options . broadRatio * maxDdy ;
162189 }
163190 }
164191 minddY . length = minddYLen ;
@@ -177,7 +204,7 @@ function gsd(x, yIn, options) {
177204 minDistance = Number . MAX_VALUE ;
178205 distanceJ = 0 ;
179206 gettingCloser = true ;
180- while ( possible === - 1 && ( k < intervalL . length ) && gettingCloser ) {
207+ while ( possible === - 1 && k < intervalL . length && gettingCloser ) {
181208 distanceJ = Math . abs ( frequency - ( intervalL [ k ] . x + intervalR [ k ] . x ) / 2 ) ;
182209
183210 // Still getting closer?
@@ -209,7 +236,9 @@ function gsd(x, yIn, options) {
209236 if ( options . heightFactor ) {
210237 let yLeft = Y [ intervalL [ possible ] . index ] ;
211238 let yRight = Y [ intervalR [ possible ] . index ] ;
212- signals [ signalsLen - 1 ] . height = options . heightFactor * ( signals [ signalsLen - 1 ] . y - ( ( yLeft + yRight ) / 2 ) ) ;
239+ signals [ signalsLen - 1 ] . height =
240+ options . heightFactor *
241+ ( signals [ signalsLen - 1 ] . y - ( yLeft + yRight ) / 2 ) ;
213242 }
214243 }
215244 }
@@ -235,7 +264,6 @@ function gsd(x, yIn, options) {
235264function getNoiseLevel ( y ) {
236265 var mean = 0 ;
237266
238-
239267 var stddev = 0 ;
240268 var length = y . length ;
241269 for ( let i = 0 ; i < length ; ++ i ) {
@@ -250,7 +278,10 @@ function getNoiseLevel(y) {
250278 if ( length % 2 === 1 ) {
251279 stddev = averageDeviations [ ( length - 1 ) / 2 ] / 0.6745 ;
252280 } else {
253- stddev = 0.5 * ( averageDeviations [ length / 2 ] + averageDeviations [ length / 2 - 1 ] ) / 0.6745 ;
281+ stddev =
282+ ( 0.5 *
283+ ( averageDeviations [ length / 2 ] + averageDeviations [ length / 2 - 1 ] ) ) /
284+ 0.6745 ;
254285 }
255286
256287 return stddev ;
@@ -259,39 +290,53 @@ function getNoiseLevel(y) {
259290function realTopDetection ( peakList , x , y ) {
260291 var alpha , beta , gamma , p , currentPoint ;
261292 for ( var j = 0 ; j < peakList . length ; j ++ ) {
262- currentPoint = peakList [ j ] . i ; // peakList[j][2];
293+ currentPoint = peakList [ j ] . index ; // peakList[j][2];
263294 // The detected peak could be moved 1 or 2 unit to left or right.
264- if ( y [ currentPoint - 1 ] >= y [ currentPoint - 2 ]
265- && y [ currentPoint - 1 ] >= y [ currentPoint ] ) {
295+ if (
296+ y [ currentPoint - 1 ] >= y [ currentPoint - 2 ] &&
297+ y [ currentPoint - 1 ] >= y [ currentPoint ]
298+ ) {
266299 currentPoint -- ;
267300 } else {
268- if ( y [ currentPoint + 1 ] >= y [ currentPoint ]
269- && y [ currentPoint + 1 ] >= y [ currentPoint + 2 ] ) {
301+ if (
302+ y [ currentPoint + 1 ] >= y [ currentPoint ] &&
303+ y [ currentPoint + 1 ] >= y [ currentPoint + 2 ]
304+ ) {
270305 currentPoint ++ ;
271306 } else {
272- if ( y [ currentPoint - 2 ] >= y [ currentPoint - 3 ]
273- && y [ currentPoint - 2 ] >= y [ currentPoint - 1 ] ) {
307+ if (
308+ y [ currentPoint - 2 ] >= y [ currentPoint - 3 ] &&
309+ y [ currentPoint - 2 ] >= y [ currentPoint - 1 ]
310+ ) {
274311 currentPoint -= 2 ;
275312 } else {
276- if ( y [ currentPoint + 2 ] >= y [ currentPoint + 1 ]
277- && y [ currentPoint + 2 ] >= y [ currentPoint + 3 ] ) {
313+ if (
314+ y [ currentPoint + 2 ] >= y [ currentPoint + 1 ] &&
315+ y [ currentPoint + 2 ] >= y [ currentPoint + 3 ]
316+ ) {
278317 currentPoint += 2 ;
279318 }
280319 }
281320 }
282321 }
283322 // interpolation to a sin() function
284- if ( y [ currentPoint - 1 ] > 0 && y [ currentPoint + 1 ] > 0
285- && y [ currentPoint ] >= y [ currentPoint - 1 ]
286- && y [ currentPoint ] >= y [ currentPoint + 1 ] ) {
323+ if (
324+ y [ currentPoint - 1 ] > 0 &&
325+ y [ currentPoint + 1 ] > 0 &&
326+ y [ currentPoint ] >= y [ currentPoint - 1 ] &&
327+ y [ currentPoint ] >= y [ currentPoint + 1 ]
328+ ) {
287329 alpha = 20 * Math . log10 ( y [ currentPoint - 1 ] ) ;
288330 beta = 20 * Math . log10 ( y [ currentPoint ] ) ;
289331 gamma = 20 * Math . log10 ( y [ currentPoint + 1 ] ) ;
290- p = 0.5 * ( alpha - gamma ) / ( alpha - 2 * beta + gamma ) ;
291- // console.log(" p: "+p );
332+ p = ( 0.5 * ( alpha - gamma ) ) / ( alpha - 2 * beta + gamma ) ;
333+ // console.log(alpha, beta, gamma, ` p: ${p}` );
292334 // console.log(x[currentPoint]+" "+tmp+" "+currentPoint);
293- peakList [ j ] . x = x [ currentPoint ] + ( x [ currentPoint ] - x [ currentPoint - 1 ] ) * p ;
294- peakList [ j ] . y = y [ currentPoint ] - 0.25 * ( y [ currentPoint - 1 ] - y [ currentPoint + 1 ] ) * p ;
335+ peakList [ j ] . x =
336+ x [ currentPoint ] + ( x [ currentPoint ] - x [ currentPoint - 1 ] ) * p ;
337+ peakList [ j ] . y =
338+ y [ currentPoint ] -
339+ 0.25 * ( y [ currentPoint - 1 ] - y [ currentPoint + 1 ] ) * p ;
295340 }
296341 }
297342}
0 commit comments