second try cal 2


Saves the 'blackbody' reflection at each wavelength
/*

This macro does a few things:
1) it separates out each wavelength based on the # of pulses per wavelength
2) it then subtracts the high 
2) it addresses LED heating by ignoring the first 2/3 of the pulses to reduce standard deviation caused by heating
3) it then tries to flatten the remining 1/3 of pulses
4) then it chooses the median, calculates standard deviation, # of bits for those pulses. 
5) then saves the raw median to the device for the shiney (0 - 9) or black (10 - 19) values

*/


var data = json.data_raw;
var output = {};

var wavelengths = [370, 395, 420, 530, 605, 650, 730, 850, 880, 940];
var pulses = 60;
var partPulses = (2/3);
var timeArray = [];
for (var z = 0; z < pulses - pulses*partPulses; z++) {
  timeArray[z] = z;
}
var raw_array = [];
var raw_array_flat = [];
var trans_array = [];
var spad_array = [];
/*                             // uncomment for main measurement (not calibration)
var max_reflectance_High = [json.recall["userdef[0]"],json.recall["userdef[1]"],json.recall["userdef[2]"],json.recall["userdef[3]"],json.recall["userdef[4]"],json.recall["userdef[5]"],json.recall["userdef[6]"],json.recall["userdef[7]"],json.recall["userdef[8]"],json.recall["userdef[9]"]];
var max_reflectance_Low = [json.recall["userdef[10]"],json.recall["userdef[11]"],json.recall["userdef[12]"],json.recall["userdef[13]"],json.recall["userdef[14]"],json.recall["userdef[15]"],json.recall["userdef[16]"],json.recall["userdef[17]"],json.recall["userdef[18]"],json.recall["userdef[19]"]];
var max_reflectance = [];
 for (j = 0; j < 10; j++) {
   max_reflectance[i] = max_reflectance_High - max_reflectance_Low;
}
*/
var average_bits = 0;
var average_bits_flat = 0;
//var max_reflectance = [json.recall["userdef[0]"],json.recall["userdef[1]"],json.recall["userdef[2]"],json.recall["userdef[3]"],json.recall["userdef[4]"],json.recall["userdef[5]"],json.recall["userdef[6]"],json.recall["userdef[7]"],json.recall["userdef[8]"],json.recall["userdef[9]"]];
//var max_rawMedian_940 = max_reflectance[9];
var rawMedian_940 = MathROUND(MathMEDIAN(json.data_raw.slice(pulses*9 + pulses*partPulses,pulses*(9+1))),0);
var tester = wavelengths.length;

output["toDevice"] = "set_user_defined+";

for (var i = 0; i < wavelengths.length; i++) {
  // first pull out the values of interest
  var rawValuesAll = json.data_raw.slice(pulses*i,pulses*(i+1));
  // select only the last bit, so if there is heating we are avoiding most of the effect
  var rawValues = json.data_raw.slice(pulses*i + pulses*partPulses,pulses*(i+1));
  // now we're going to address any additional slope by creating a linear regression and normalizing so that it's flat (slope == 0)
  var reg = MathLINREG(timeArray,rawValues);
//  output["reg.m"+wavelengths[i]] = reg.m; // uncomment to see slope and y-intercept
//  output["reg.b"+wavelengths[i]] = reg.b;
  // what is the center point of rotation for the line (halfway through the array) - that's the value from which we will adjust other values
  var centerPoint = reg.m*(pulses - pulses*partPulses)/2 + reg.b;
//  output["centerPoint"+wavelengths[i]] = centerPoint;

  var rawValues_flat = [];
  for (var j = 0; j < rawValues.length; j++ ) { 
    // now calculate difference between centerpoint and actual regression line
//    var regloc = (reg.m*timeArray[j] + reg.b)
//    output["regloc"+wavelengths[i]+j] = regloc;
    var adjustment = centerPoint - (reg.m*timeArray[j] + reg.b);
//    output["adjustment"+wavelengths[i]+j] = adjustment;
    rawValues_flat[j] = rawValues[j] + adjustment;
//    output["rawValues_flat"+wavelengths[i]] += MathROUND(rawValues_flat[j],2) + ",";     // uncomment to see the flattened data + compare to actual
//    output["rawValues"+wavelengths[i]] += MathROUND(rawValues[j],2) + ",";               // uncomment to see the actual data
//    output["final value_rom_array"+wavelengths[i]+j] = rawValues_flat[j];
//    output["final value"+wavelengths[i]+j] = rawValues[j];
  }
  
  var rawMedian = MathROUND(MathMEDIAN(json.data_raw.slice(pulses*i + pulses*partPulses,pulses*(i+1))),0);
  var rawMedian_flat = MathROUND(MathMEDIAN(rawValues_flat),0);
  output["toDevice"] += i+10 + "+" + rawMedian_flat+"+";
  var rawStd = MathSTDEV(rawValues);
  var rawStd_flat = MathSTDEV(rawValues_flat);
  var bits = ( 15 - MathLOG(rawStd*2) / MathLOG(2) );
  var bits_flat = ( 15 - MathLOG(rawStd_flat*2) / MathLOG(2) );
  average_bits = average_bits + bits;
  average_bits_flat = average_bits_flat + bits_flat;
//  var spad = 100 * Math.log ( ( rawMedian_940 / max_rawMedian_940 ) / (rawMedian / max_reflectance[i] ) );
//  output["raw_trans_"+wavelengths[i]] = rawTrans;
//  output["raw_trans_flat_"+wavelengths[i]] = rawMedian_flat;
//  output["percent_trans_"+wavelengths[i]] = MathROUND(1 - ( rawTrans / max_reflectance[i]),2);
//  output["spad_"+wavelengths[i]] = MathROUND(spad,2);
//  output["cal_"+wavelengths[i]] = max_reflectance[i]
//  output["rawValuesAll"+wavelengths[i]] = rawValuesAll;                       // uncomment to see the entire 60 pulses
//  output["rawValues_notflat_"+wavelengths[i]] = rawValues;                             // uncomment to see the final 20 pulses
  output["rawMedian"+wavelengths[i]] = MathROUND(rawValues_flat[i]);
//  output["std_notflat_"+wavelengths[i]] = MathROUND(rawStd,1);
//  output["std_flat_"+wavelengths[i]] = MathROUND(rawStd_flat,1);
//  output["bits_notflat_"+wavelengths[i]] = MathROUND(bits,1);                         // uncomment to see non-flattened bits
  output["bits"+wavelengths[i]] = MathROUND(bits_flat,1);
  raw_array[i] = rawMedian;
  raw_array_flat[i] = rawMedian_flat;
  trans_array[i] = rawMedian;
  spad_array[i] = rawMedian;
//  output["toDevice"] += i + "+" + raw_array[i]+"+";
 }

output["average_bits"] = MathROUND(average_bits / wavelengths.length,2);
output["average_bits_flat"] = MathROUND(average_bits_flat / wavelengths.length,2);

output["toDevice"] += "-1+";
//output["max_reflectance_Low"] = max_reflectance_Low;
output["rawMedian"] = raw_array_flat;
//output["rawMedian_notflat"] = raw_array;
//output["max_reflectance"] = max_reflectance;
//output["max_reflectance_High"] = max_reflectance_High;
//output["max_reflectance_Low"] = max_reflectance_Low;
//output["max_rawMedian_940"] = max_rawMedian_940;
//output["rawMedian_940"] = rawMedian_940;

//output["tester"] = tester;

// = "set_user_defined+1+"+shinybody_530+"+2+"+shinybody_650+"+3+"+shinybody_605+"+4+"+shinybody_420+"+5+"+shinybody_940+"+6+"+shinybody_850+"+7+"+shinybody_730+"+8+"+shinybody_880+"+-1+";

return output;
{
  "time_offset": 240,
  "time": 1506970740076,
  "device_name": "MultispeQ",
  "device_version": "1",
  "device_id": "03:22:01:96",
  "device_battery": 14,
  "device_firmware": 1.17,
  "sample": [
    {
      "time": 1506970740101,
      "protocol_id": 1,
      "averages": 5,
      "recall": {
        "time": 1506970744584,
        "userdef[0]": 23090,
        "userdef[1]": 22827,
        "userdef[2]": 22744,
        "userdef[3]": 22698,
        "userdef[4]": 22668,
        "userdef[5]": 22647,
        "userdef[6]": 18722,
        "userdef[7]": 18773,
        "userdef[8]": 17296,
        "userdef[9]": 17320,
        "userdef[10]": 45320,
        "userdef[11]": 45252,
        "userdef[12]": 45221,
        "userdef[13]": 45194,
        "userdef[14]": 45218,
        "userdef[15]": 45198,
        "userdef[16]": 36434,
        "userdef[17]": 36456,
        "userdef[18]": 36431,
        "userdef[19]": 36433
      },
      "data_raw": [
        41502,
        41498,
        41432,
        41378,
        41337,
        41296,
        41267,
        41237,
        41204,
        41182,
        41161,
        41135,
        41118,
        41103,
        41073,
        41059,
        41045,
        41046,
        41024,
        41012,
        41002,
        40992,
        40982,
        40968,
        40961,
        40955,
        40932,
        40924,
        40914,
        40909,
        40900,
        40888,
        40888,
        40885,
        40876,
        40864,
        40862,
        40852,
        40850,
        40848,
        40835,
        40828,
        40828,
        40820,
        40813,
        40800,
        40794,
        40794,
        40792,
        40792,
        40787,
        40780,
        40772,
        40770,
        40772,
        40765,
        40750,
        40747,
        40742,
        40733,
        40741,
        40731,
        40730,
        40734,
        40719,
        40718,
        40716,
        40715,
        40714,
        40708,
        40704,
        40699,
        40696,
        40701,
        40705,
        40686,
        40693,
        40692,
        40681,
        40679,
        40686,
        40680,
        40673,
        40661,
        40667,
        40666,
        40664,
        40660,
        40654,
        40652,
        40655,
        40650,
        40651,
        40654,
        40654,
        40644,
        40642,
        40645,
        40644,
        40644,
        40642,
        40636,
        40629,
        40629,
        40628,
        40626,
        40620,
        40620,
        40626,
        40622,
        40628,
        40628,
        40627,
        40622,
        40620,
        40619,
        40624,
        40620,
        40626,
        40619,
        40629,
        40632,
        40632,
        40629,
        40629,
        40621,
        40620,
        40622,
        40616,
        40624,
        40618,
        40620,
        40617,
        40610,
        40613,
        40610,
        40609,
        40598,
        40598,
        40615,
        40604,
        40591,
        40599,
        40593,
        40604,
        40591,
        40590,
        40588,
        40589,
        40588,
        40584,
        40578,
        40576,
        40582,
        40576,
        40578,
        40584,
        40581,
        40571,
        40578,
        40575,
        40572,
        40569,
        40571,
        40568,
        40571,
        40566,
        40564,
        40567,
        40568,
        40572,
        40568,
        40559,
        40563,
        40568,
        40559,
        40563,
        40565,
        40560,
        40556,
        34770,
        34686,
        34687,
        34708,
        34718,
        34721,
        34730,
        34740,
        34746,
        34743,
        34768,
        34774,
        34788,
        34791,
        34799,
        34798,
        34798,
        34804,
        34799,
        34807,
        34812,
        34812,
        34817,
        34816,
        34819,
        34823,
        34820,
        34819,
        34822,
        34816,
        34820,
        34825,
        34838,
        34830,
        34826,
        34840,
        34844,
        34840,
        34833,
        34831,
        34839,
        34845,
        34844,
        34845,
        34842,
        34852,
        34845,
        34847,
        34848,
        34841,
        34829,
        34830,
        34831,
        34838,
        34842,
        34819,
        34836,
        34833,
        34839,
        34834,
        30815,
        30798,
        30805,
        30807,
        30806,
        30816,
        30812,
        30822,
        30819,
        30821,
        30818,
        30822,
        30825,
        30824,
        30822,
        30830,
        30835,
        30836,
        30831,
        30834,
        30834,
        30840,
        30842,
        30842,
        30848,
        30841,
        30834,
        30837,
        30840,
        30843,
        30851,
        30847,
        30856,
        30848,
        30854,
        30852,
        30852,
        30852,
        30851,
        30846,
        30852,
        30862,
        30849,
        30866,
        30851,
        30862,
        30862,
        30864,
        30862,
        30853,
        30858,
        30868,
        30860,
        30865,
        30861,
        30852,
        30861,
        30861,
        30859,
        30864,
        31158,
        31001,
        31006,
        31020,
        31031,
        31026,
        31037,
        31048,
        31058,
        31062,
        31076,
        31080,
        31090,
        31084,
        31091,
        31105,
        31105,
        31108,
        31102,
        31107,
        31108,
        31116,
        31111,
        31091,
        31090,
        31104,
        31116,
        31103,
        31113,
        31098,
        31118,
        31115,
        31120,
        31124,
        31123,
        31129,
        31128,
        31132,
        31123,
        31141,
        31134,
        31135,
        31131,
        31138,
        31139,
        31139,
        31142,
        31138,
        31138,
        31141,
        31138,
        31152,
        31150,
        31148,
        31144,
        31152,
        31152,
        31146,
        31151,
        31155,
        39286,
        39252,
        39255,
        39266,
        39280,
        39269,
        39260,
        39256,
        39266,
        39262,
        39277,
        39267,
        39276,
        39294,
        39295,
        39304,
        39304,
        39308,
        39320,
        39309,
        39322,
        39315,
        39325,
        39330,
        39330,
        39316,
        39326,
        39326,
        39326,
        39331,
        39326,
        39316,
        39311,
        39331,
        39338,
        39336,
        39335,
        39332,
        39343,
        39330,
        39346,
        39349,
        39340,
        39312,
        39316,
        39313,
        39318,
        39314,
        39321,
        39324,
        39324,
        39321,
        39326,
        39320,
        39325,
        39316,
        39327,
        39330,
        39330,
        39322,
        32108,
        29383,
        29308,
        29284,
        29261,
        29268,
        29268,
        29271,
        29278,
        29271,
        29284,
        29283,
        29286,
        29290,
        29285,
        29296,
        29296,
        29309,
        29297,
        29294,
        29312,
        29304,
        29305,
        29314,
        29322,
        29312,
        29327,
        29328,
        29326,
        29335,
        29334,
        29336,
        29330,
        29342,
        29350,
        29340,
        29364,
        29352,
        29352,
        29354,
        29354,
        29357,
        29349,
        29364,
        29361,
        29364,
        29352,
        29372,
        29365,
        29364,
        29368,
        29370,
        29366,
        29376,
        29380,
        29369,
        29376,
        29365,
        29375,
        29364,
        39271,
        39338,
        39344,
        39331,
        39326,
        39334,
        39337,
        39338,
        39337,
        39348,
        39344,
        39345,
        39351,
        39340,
        39356,
        39333,
        39341,
        39342,
        39346,
        39356,
        39342,
        39349,
        39341,
        39345,
        39344,
        39344,
        39341,
        39345,
        39356,
        39348,
        39351,
        39358,
        39339,
        39369,
        39350,
        39366,
        39353,
        39362,
        39352,
        39372,
        39365,
        39356,
        39365,
        39356,
        39360,
        39372,
        39365,
        39364,
        39376,
        39366,
        39366,
        39367,
        39372,
        39376,
        39372,
        39377,
        39375,
        39388,
        39383,
        39385,
        40742,
        40761,
        40754,
        40758,
        40756,
        40740,
        40758,
        40760,
        40756,
        40753,
        40756,
        40760,
        40750,
        40762,
        40759,
        40763,
        40750,
        40758,
        40753,
        40763,
        40766,
        40757,
        40761,
        40760,
        40770,
        40756,
        40761,
        40756,
        40766,
        40764,
        40754,
        40767,
        40762,
        40762,
        40763,
        40764,
        40751,
        40759,
        40751,
        40754,
        40754,
        40754,
        40763,
        40756,
        40760,
        40769,
        40770,
        40748,
        40753,
        40758,
        40756,
        40767,
        40766,
        40761,
        40756,
        40766,
        40754,
        40756,
        40749,
        40762
      ]
    }
  ],
  "app_os": "linux",
  "app_name": "PhotosynQ",
  "app_version": "0.3.9",
  "app_device": "x86-64",
  "location": [
    "42.2310636",
    "-83.7478788"
  ]
}
Default avatar
Created by

Jon S


Protocol connections:
1
Latest Update:
Oct 2017