@@ -9,7 +9,7 @@ var sgDefOptions = {
99} ;
1010
1111
12- function gsd ( xIn , yIn , options ) {
12+ function gsd ( x , yIn , options ) {
1313 //options = extend({}, defaultOptions, options);
1414 var options = Object . create ( options || { } ) ;
1515 if ( options . minMaxRatio === undefined ) options . minMaxRatio = 0.00025 ;
@@ -21,147 +21,138 @@ function gsd(xIn, yIn, options){
2121 if ( options . realTopDetection === undefined ) options . realTopDetection = false ;
2222
2323 var sgOptions = extend ( { } , sgDefOptions , options . sgOptions ) ;
24+ const y = [ ] . concat ( yIn ) ;
2425
25- //console.log(JSON.stringify(stats.array.minMax(y)));
26- if ( options . noiseLevel === undefined ) {
26+ var i , j , k ;
27+
28+ if ( options . noiseLevel === undefined ) {
2729 //We have to know if x is equally spaced
28- var maxDx = 0 , minDx = Number . MAX_VALUE , tmp ;
29- for ( var i = 0 ; i < x . length - 1 ; i ++ ) {
30- var tmp = Math . abs ( x [ i + 1 ] - x [ i ] ) ;
31- if ( tmp < minDx ) {
30+ var maxDx = 0 , minDx = Number . MAX_VALUE , tmp ;
31+ for ( i = 0 ; i < x . length - 1 ; i ++ ) {
32+ tmp = Math . abs ( x [ i + 1 ] - x [ i ] ) ;
33+ if ( tmp < minDx ) {
3234 minDx = tmp ;
3335 }
34- if ( tmp > maxDx ) {
36+ if ( tmp > maxDx ) {
3537 maxDx = tmp ;
3638 }
3739 }
3840
39- if ( ( maxDx - minDx ) / maxDx < 0.05 ) {
40-
41+ if ( ( maxDx - minDx ) / maxDx < 0.05 ) {
4142 options . noiseLevel = getNoiseLevel ( y ) ;
42- //console.log(options.noiseLevel+" "+stats.array.median(y));
4343 }
44- else {
44+ else {
4545 options . noiseLevel = 0 ;
4646 }
4747 }
48- //console.log("options.noiseLevel "+options.noiseLevel);
49- var y = [ ] . concat ( yIn ) ;
50- var yCorrection = { m :1 , b :options . noiseLevel } ;
51- if ( ! options . maxCriteria ) {
48+ const yCorrection = { m :1 , b :options . noiseLevel } ;
49+ if ( ! options . maxCriteria ) {
5250 yCorrection . m = - 1 ;
5351 yCorrection . b *= - 1 ;
5452 }
5553
56- for ( var i = 0 ; i < y . length ; i ++ ) {
57- y [ i ] = yCorrection . m * y [ i ] - yCorrection . b ;
54+ for ( i = 0 ; i < y . length ; i ++ ) {
55+ y [ i ] = yCorrection . m * y [ i ] - yCorrection . b ;
5856 }
5957
60- for ( var i = 0 ; i < y . length ; i ++ ) {
58+ for ( i = 0 ; i < y . length ; i ++ ) {
6159 if ( y [ i ] < 0 ) {
6260 y [ i ] = 0 ;
6361 }
6462 }
65-
6663 //If the max difference between delta x is less than 5%, then, we can assume it to be equally spaced variable
6764 var Y = y ;
68- if ( ( maxDx - minDx ) / maxDx < 0.05 ) {
65+ var dY , ddY ;
66+ if ( ( maxDx - minDx ) / maxDx < 0.05 ) {
6967 if ( options . smoothY )
7068 Y = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :0 } ) ;
71- var dY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :1 } ) ;
72- var ddY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :2 } ) ;
69+ dY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :1 } ) ;
70+ ddY = SG ( y , x [ 1 ] - x [ 0 ] , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :2 } ) ;
7371 }
74- else {
72+ else {
7573 if ( options . smoothY )
7674 Y = SG ( y , x , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :0 } ) ;
77- var dY = SG ( y , x , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :1 } ) ;
78- var ddY = SG ( y , x , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :2 } ) ;
75+ dY = SG ( y , x , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :1 } ) ;
76+ ddY = SG ( y , x , { windowSize :sgOptions . windowSize , polynomial :sgOptions . polynomial , derivative :2 } ) ;
7977 }
8078
81- var X = x ;
82- var dx = x [ 1 ] - x [ 0 ] ;
83- var maxDdy = 0 ;
79+ const X = x ;
80+ const dx = x [ 1 ] - x [ 0 ] ;
81+ var maxDdy = 0 ;
8482 var maxY = 0 ;
85- //console.log(Y.length);
86- for ( var i = 0 ; i < Y . length ; i ++ ) {
87- if ( Math . abs ( ddY [ i ] ) > maxDdy ) {
83+ for ( i = 0 ; i < Y . length ; i ++ ) {
84+ if ( Math . abs ( ddY [ i ] ) > maxDdy ) {
8885 maxDdy = Math . abs ( ddY [ i ] ) ;
8986 }
90- if ( Math . abs ( Y [ i ] ) > maxY ) {
87+ if ( Math . abs ( Y [ i ] ) > maxY ) {
9188 maxY = Math . abs ( Y [ i ] ) ;
9289 }
9390 }
94- //console.log(maxY+"x"+maxDy+"x"+maxDdy);
9591
9692 var minddY = [ ] ;
9793 var intervalL = [ ] ;
9894 var intervalR = [ ] ;
9995 var lastMax = null ;
10096 var lastMin = null ;
10197 var broadMask = new Array ( ) ;
102- //console.log(dx);
10398 //By the intermediate value theorem We cannot find 2 consecutive maxima or minima
104- for ( var i = 1 ; i < Y . length - 1 ; i ++ ) {
99+ for ( i = 1 ; i < Y . length - 1 ; i ++ ) {
105100 //console.log(dY[i]);
106- if ( ( dY [ i ] < dY [ i - 1 ] ) && ( dY [ i ] <= dY [ i + 1 ] ) ||
101+ if ( ( dY [ i ] < dY [ i - 1 ] ) && ( dY [ i ] <= dY [ i + 1 ] ) ||
107102 ( dY [ i ] <= dY [ i - 1 ] ) && ( dY [ i ] < dY [ i + 1 ] ) ) {
108103 lastMin = X [ i ] ;
109- //console.log("min "+lastMin);
110- if ( dx > 0 && lastMax != null ) {
104+ if ( dx > 0 && lastMax != null ) {
111105 intervalL . push ( lastMax ) ;
112106 intervalR . push ( lastMin ) ;
113107
114108 }
115109 }
116110
117- if ( ( dY [ i ] >= dY [ i - 1 ] ) && ( dY [ i ] > dY [ i + 1 ] ) ||
111+ if ( ( dY [ i ] >= dY [ i - 1 ] ) && ( dY [ i ] > dY [ i + 1 ] ) ||
118112 ( dY [ i ] > dY [ i - 1 ] ) && ( dY [ i ] >= dY [ i + 1 ] ) ) {
119113 lastMax = X [ i ] ;
120- //console.log("max "+lastMax);
121- if ( dx < 0 && lastMin != null ) {
114+ if ( dx < 0 && lastMin != null ) {
122115 intervalL . push ( lastMax ) ;
123116 intervalR . push ( lastMin ) ;
124117 }
125118 }
126119 if ( ( ddY [ i ] < ddY [ i - 1 ] ) && ( ddY [ i ] < ddY [ i + 1 ] ) ) {
127120 minddY . push ( i ) ; //( [X[i], Y[i], i] ); // TODO should we change this to have 3 arrays ? Huge overhead creating arrays
128- if ( Math . abs ( ddY [ i ] ) > options . broadRatio * maxDdy ) { // TODO should this be a parameter =
121+ if ( Math . abs ( ddY [ i ] ) > options . broadRatio * maxDdy ) { // TODO should this be a parameter =
129122 broadMask . push ( false ) ;
130123 }
131- else {
124+ else {
132125 broadMask . push ( true ) ;
133126 }
134127 }
135128 }
136- //
137- //console.log(intervalL.length+" "+minddY.length+" "+broadMask.length);
129+
138130 var signals = [ ] ;
139- var lastK = 0 , possible , k , f , frequency , distanceJ , minDistance , gettingCloser ;
140- for ( var j = 0 ; j < minddY . length ; j ++ ) {
141- frequency = X [ minddY [ j ] ] ; //minddY[j][0];
131+ var lastK = 0 ;
132+ var possible , frequency , distanceJ , minDistance , gettingCloser ;
133+ for ( j = 0 ; j < minddY . length ; j ++ ) {
134+ frequency = X [ minddY [ j ] ] ;
142135 possible = - 1 ;
143- k = lastK + 1 ;
136+ k = lastK + 1 ;
144137 minDistance = Number . MAX_VALUE ;
145138 distanceJ = 0 ;
146139 gettingCloser = true ;
147- while ( possible == - 1 && k < intervalL . length && gettingCloser ) {
140+ while ( possible == - 1 && k < intervalL . length && gettingCloser ) {
148141 distanceJ = Math . abs ( frequency - ( intervalL [ k ] + intervalR [ k ] ) / 2 ) ;
149142 //Still getting closer?
150- if ( distanceJ < minDistance ) {
143+ if ( distanceJ < minDistance ) {
151144 minDistance = distanceJ ;
152145 }
153146 else {
154147 gettingCloser = false ;
155148 }
156- if ( distanceJ < Math . abs ( intervalL [ k ] - intervalR [ k ] ) / 2 ) {
157- possible = k ;
149+ if ( distanceJ < Math . abs ( intervalL [ k ] - intervalR [ k ] ) / 2 ) {
150+ possible = k ;
158151 lastK = k ;
159152 }
160153 k ++ ;
161154 }
162- //console.log(lastK+" "+intervalL.length+" possible "+k);
163- if ( possible != - 1 ) {
164- //console.log(height);
155+ if ( possible != - 1 ) {
165156 if ( Math . abs ( Y [ minddY [ j ] ] ) > options . minMaxRatio * maxY ) {
166157 signals . push ( {
167158 i :minddY [ j ] ,
@@ -175,13 +166,13 @@ function gsd(xIn, yIn, options){
175166 }
176167
177168
178- if ( options . realTopDetection ) {
169+ if ( options . realTopDetection ) {
179170 realTopDetection ( signals , X , Y ) ;
180171 }
181172
182173 //Correct the values to fit the original spectra data
183- for ( var j = 0 ; j < signals . length ; j ++ ) {
184- signals [ j ] . base = options . noiseLevel ;
174+ for ( j = 0 ; j < signals . length ; j ++ ) {
175+ signals [ j ] . base = options . noiseLevel ;
185176 }
186177
187178 signals . sort ( function ( a , b ) {
@@ -192,11 +183,11 @@ function gsd(xIn, yIn, options){
192183
193184}
194185
195- function getNoiseLevel ( y ) {
196- var mean = 0 , stddev = 0 ;
197- var length = y . length , i = 0 ;
198- for ( i = 0 ; i < length ; i ++ ) {
199- mean += y [ i ] ;
186+ function getNoiseLevel ( y ) {
187+ var mean = 0 , stddev = 0 ;
188+ var length = y . length , i = 0 ;
189+ for ( i = 0 ; i < length ; i ++ ) {
190+ mean += y [ i ] ;
200191 }
201192 mean /= length ;
202193 var averageDeviations = new Array ( length ) ;
@@ -212,41 +203,36 @@ function getNoiseLevel(y){
212203 return stddev ;
213204}
214205
215- function realTopDetection ( peakList , x , y ) {
216- //console.log(peakList);
217- //console.log(x);
218- //console.log(y);
219- var listP = [ ] ;
206+ function realTopDetection ( peakList , x , y ) {
220207 var alpha , beta , gamma , p , currentPoint ;
221- for ( var j = 0 ; j < peakList . length ; j ++ ) {
208+ for ( var j = 0 ; j < peakList . length ; j ++ ) {
222209 currentPoint = peakList [ j ] . i ; //peakList[j][2];
223- var tmp = currentPoint ;
224210 //The detected peak could be moved 1 or 2 unit to left or right.
225- if ( y [ currentPoint - 1 ] >= y [ currentPoint - 2 ]
226- && y [ currentPoint - 1 ] >= y [ currentPoint ] ) {
211+ if ( y [ currentPoint - 1 ] >= y [ currentPoint - 2 ]
212+ && y [ currentPoint - 1 ] >= y [ currentPoint ] ) {
227213 currentPoint -- ;
228214 }
229- else {
230- if ( y [ currentPoint + 1 ] >= y [ currentPoint ]
231- && y [ currentPoint + 1 ] >= y [ currentPoint + 2 ] ) {
215+ else {
216+ if ( y [ currentPoint + 1 ] >= y [ currentPoint ]
217+ && y [ currentPoint + 1 ] >= y [ currentPoint + 2 ] ) {
232218 currentPoint ++ ;
233219 }
234220 else {
235- if ( y [ currentPoint - 2 ] >= y [ currentPoint - 3 ]
236- && y [ currentPoint - 2 ] >= y [ currentPoint - 1 ] ) {
221+ if ( y [ currentPoint - 2 ] >= y [ currentPoint - 3 ]
222+ && y [ currentPoint - 2 ] >= y [ currentPoint - 1 ] ) {
237223 currentPoint -= 2 ;
238224 }
239- else {
240- if ( y [ currentPoint + 2 ] >= y [ currentPoint + 1 ]
241- && y [ currentPoint + 2 ] >= y [ currentPoint + 3 ] ) {
225+ else {
226+ if ( y [ currentPoint + 2 ] >= y [ currentPoint + 1 ]
227+ && y [ currentPoint + 2 ] >= y [ currentPoint + 3 ] ) {
242228 currentPoint += 2 ;
243229 }
244230 }
245231 }
246232 }
247- if ( y [ currentPoint - 1 ] > 0 && y [ currentPoint + 1 ] > 0
248- && y [ currentPoint ] >= y [ currentPoint - 1 ]
249- && y [ currentPoint ] >= y [ currentPoint + 1 ] ) {
233+ if ( y [ currentPoint - 1 ] > 0 && y [ currentPoint + 1 ] > 0
234+ && y [ currentPoint ] >= y [ currentPoint - 1 ]
235+ && y [ currentPoint ] >= y [ currentPoint + 1 ] ) {
250236 alpha = 20 * Math . log10 ( y [ currentPoint - 1 ] ) ;
251237 beta = 20 * Math . log10 ( y [ currentPoint ] ) ;
252238 gamma = 20 * Math . log10 ( y [ currentPoint + 1 ] ) ;
@@ -256,7 +242,6 @@ function realTopDetection(peakList, x, y){
256242 peakList [ j ] . x = x [ currentPoint ] + ( x [ currentPoint ] - x [ currentPoint - 1 ] ) * p ;
257243 peakList [ j ] . y = y [ currentPoint ] - 0.25 * ( y [ currentPoint - 1 ]
258244 - y [ currentPoint + 1 ] ) * p ; //signal.peaks[j].intensity);
259- //console.log(y[tmp]+" "+peakList[j].y);
260245 }
261246 }
262247}
0 commit comments