Skip to content

Commit c8236c5

Browse files
committed
GSD test for IR
1 parent f6a52d9 commit c8236c5

File tree

6 files changed

+52
-36
lines changed

6 files changed

+52
-36
lines changed

src/gsd.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
var Opt = require("ml-optimize-lorentzian");
2-
2+
var stats = require("ml-stat");
33
function gsd(x, y, options){
44
var options=Object.create(options || {});
55
if (options.minMaxRatio===undefined) options.minMaxRatio=0.00025;
66
if (options.broadRatio===undefined) options.broadRatio=0.00;
77
if (options.noiseLevel===undefined) options.noiseLevel=0;
88
if (options.maxCriteria===undefined) options.maxCriteria=true;
99
if (options.smoothY===undefined) options.smoothY=true;
10+
if (options.realTopDetection===undefined) options.realTopDetection=false;
1011

1112

12-
if (options.noiseLevel>0) {
13+
//Transform y to use the standard algorithm.
14+
var yCorrection = {m:1, b:0};
15+
if(!options.maxCriteria||options.noiseLevel>0){
1316
y=[].concat(y);
14-
for (var i=0; i<y.length; i++){
15-
if(Math.abs(y[i])<options.noiseLevel) {
16-
y[i]=0;
17+
var yCorrection = {m:-1, b:stats.array.max(y)};
18+
if(!options.maxCriteria){
19+
for (var i=0; i<y.length; i++){
20+
y[i]=-y[i]+yCorrection.b;
21+
}
22+
options.noiseLevel=-options.noiseLevel+yCorrection.b;
23+
}
24+
if (options.noiseLevel>0) {
25+
for (var i=0; i<y.length; i++){
26+
if(Math.abs(y[i])<options.noiseLevel) {
27+
y[i]=0;
28+
}
1729
}
1830
}
1931
}
20-
2132
// fill convolution frequency axis
2233
var X = [];//x[2:(x.length-2)];
2334

@@ -78,34 +89,21 @@ function gsd(x, y, options){
7889
intervals.push( [lastMax , lastMin] );
7990
}
8091
}
81-
82-
if(options.maxCriteria){
83-
if ((ddY[i] < ddY[i-1]) && (ddY[i] < ddY[i+1])) {
84-
minddY.push( [X[i], Y[i], i] ); // TODO should we change this to have 3 arrays ? Huge overhead creating arrays
85-
if(Math.abs(ddY[i])>options.broadRatio*maxDdy){ // TODO should this be a parameter =
86-
broadMask.push(false);
87-
}
88-
else{
89-
broadMask.push(true);
90-
}
92+
if ((ddY[i] < ddY[i-1]) && (ddY[i] < ddY[i+1])) {
93+
minddY.push( [X[i], Y[i], i] ); // TODO should we change this to have 3 arrays ? Huge overhead creating arrays
94+
if(Math.abs(ddY[i])>options.broadRatio*maxDdy){ // TODO should this be a parameter =
95+
broadMask.push(false);
9196
}
92-
}
93-
else{
94-
if ((ddY[i] > ddY[i-1]) && (ddY[i] > ddY[i+1])) {
95-
minddY.push( [X[i], Y[i], i] ); // TODO should we change this to have 3 arrays ? Huge overhead creating arrays
96-
if(Math.abs(ddY[i])>options.broadRatio*maxDdy){ // TODO should this be a parameter =
97-
broadMask.push(false);
98-
}
99-
else{
100-
broadMask.push(true);
101-
}
97+
else{
98+
broadMask.push(true);
10299
}
103100
}
104-
105101
}
106-
realTopDetection(minddY,X,Y);
102+
if(options.realTopDetection){
103+
realTopDetection(minddY,X,Y);
104+
}
105+
//
107106
//console.log(intervals);
108-
//console.log(minddY);
109107
var signals = [];
110108

111109
for (var j = 0; j < minddY.length; j++){
@@ -128,8 +126,9 @@ function gsd(x, y, options){
128126
if (Math.abs(height) > options.minMaxRatio*maxY) {
129127
signals.push({
130128
x: frequency,
131-
y: height,
132-
width: linewidth//*widthCorrection
129+
y: (height-yCorrection.b)/yCorrection.m,
130+
width: linewidth,//*widthCorrection
131+
soft:broadMask[j]
133132
})
134133
}
135134
}
@@ -139,7 +138,7 @@ function gsd(x, y, options){
139138
// console.log("Nested "+possible);
140139
}
141140
}
142-
if(options.broadRatio>0){
141+
/*if(options.broadRatio>0){
143142
var broadLines=[[Number.MAX_VALUE,0,0]];
144143
//Optimize the possible broad lines
145144
var max=0, maxI=0,count=0;
@@ -185,7 +184,7 @@ function gsd(x, y, options){
185184
count = 0;
186185
}
187186
}
188-
}
187+
}*/
189188

190189
signals.sort(function (a, b) {
191190
return a.x - b.x;

test/infraRed.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var peakPicking = require("../src/index");
5+
6+
describe('Global spectra deconvolution Infrared spectra', function () {
7+
8+
// Test case obtained from Pag 443, Chap 8.
9+
it('Should get the correct result', function () {
10+
var spectrum=JSON.parse(fs.readFileSync('./test/infraRed.json', 'utf-8'));
11+
var result = peakPicking.gsd(spectrum.x,spectrum.y,{noiseLevel: 32, minMaxRatio:0.03,maxCriteria:false});
12+
//console.log(result);
13+
//console.log(result.length);
14+
});
15+
});

0 commit comments

Comments
 (0)