second try cal


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 + "+" + 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": 1506974703526,
  "device_name": "MultispeQ",
  "device_version": "1",
  "device_id": "03:22:01:96",
  "device_battery": 14,
  "device_firmware": 1.17,
  "sample": [
    {
      "time": 1506974703548,
      "protocol_id": 1,
      "averages": 5,
      "data_raw": [
        41319,
        41302,
        41243,
        41187,
        41152,
        41112,
        41078,
        41039,
        41014,
        40994,
        40970,
        40945,
        40920,
        40904,
        40882,
        40868,
        40853,
        40840,
        40820,
        40814,
        40795,
        40786,
        40775,
        40770,
        40760,
        40735,
        40737,
        40726,
        40706,
        40707,
        40701,
        40695,
        40677,
        40674,
        40672,
        40668,
        40670,
        40657,
        40657,
        40657,
        40645,
        40636,
        40633,
        40621,
        40622,
        40623,
        40601,
        40599,
        40595,
        40591,
        40597,
        40590,
        40588,
        40578,
        40578,
        40581,
        40578,
        40568,
        40567,
        40556,
        40553,
        40556,
        40546,
        40547,
        40535,
        40536,
        40534,
        40530,
        40526,
        40530,
        40521,
        40517,
        40514,
        40507,
        40503,
        40500,
        40486,
        40497,
        40495,
        40490,
        40489,
        40490,
        40484,
        40478,
        40476,
        40467,
        40471,
        40460,
        40468,
        40463,
        40463,
        40459,
        40458,
        40462,
        40455,
        40456,
        40455,
        40438,
        40444,
        40439,
        40438,
        40443,
        40438,
        40439,
        40436,
        40435,
        40432,
        40442,
        40429,
        40436,
        40431,
        40434,
        40428,
        40427,
        40419,
        40425,
        40430,
        40426,
        40428,
        40421,
        40437,
        40434,
        40433,
        40428,
        40436,
        40430,
        40425,
        40425,
        40430,
        40430,
        40425,
        40420,
        40414,
        40420,
        40419,
        40411,
        40410,
        40413,
        40415,
        40420,
        40412,
        40404,
        40407,
        40406,
        40408,
        40409,
        40403,
        40411,
        40401,
        40394,
        40399,
        40391,
        40387,
        40391,
        40380,
        40384,
        40384,
        40382,
        40377,
        40382,
        40381,
        40381,
        40372,
        40370,
        40374,
        40374,
        40377,
        40372,
        40374,
        40367,
        40373,
        40375,
        40369,
        40373,
        40366,
        40360,
        40366,
        40366,
        40360,
        40360,
        34655,
        34570,
        34574,
        34592,
        34600,
        34626,
        34614,
        34630,
        34638,
        34639,
        34640,
        34648,
        34653,
        34648,
        34665,
        34657,
        34667,
        34675,
        34680,
        34671,
        34690,
        34676,
        34680,
        34688,
        34689,
        34701,
        34688,
        34703,
        34694,
        34716,
        34708,
        34698,
        34692,
        34706,
        34706,
        34707,
        34712,
        34710,
        34718,
        34735,
        34733,
        34729,
        34735,
        34731,
        34740,
        34740,
        34745,
        34739,
        34738,
        34742,
        34736,
        34742,
        34746,
        34746,
        34746,
        34745,
        34744,
        34748,
        34735,
        34738,
        30679,
        30675,
        30678,
        30684,
        30686,
        30690,
        30684,
        30686,
        30691,
        30690,
        30696,
        30694,
        30694,
        30684,
        30698,
        30696,
        30694,
        30696,
        30706,
        30697,
        30698,
        30706,
        30706,
        30700,
        30705,
        30704,
        30703,
        30707,
        30711,
        30710,
        30707,
        30710,
        30709,
        30708,
        30711,
        30706,
        30709,
        30709,
        30709,
        30712,
        30712,
        30709,
        30710,
        30713,
        30716,
        30707,
        30710,
        30716,
        30716,
        30728,
        30714,
        30718,
        30715,
        30714,
        30715,
        30722,
        30714,
        30715,
        30716,
        30725,
        30945,
        30774,
        30794,
        30812,
        30815,
        30826,
        30830,
        30840,
        30835,
        30852,
        30850,
        30852,
        30870,
        30875,
        30878,
        30882,
        30889,
        30890,
        30900,
        30896,
        30897,
        30900,
        30899,
        30898,
        30892,
        30894,
        30918,
        30909,
        30902,
        30898,
        30900,
        30906,
        30900,
        30903,
        30910,
        30913,
        30914,
        30921,
        30921,
        30927,
        30924,
        30924,
        30938,
        30928,
        30924,
        30931,
        30920,
        30932,
        30930,
        30930,
        30930,
        30939,
        30940,
        30943,
        30942,
        30940,
        30946,
        30938,
        30943,
        30946,
        39204,
        39180,
        39180,
        39178,
        39180,
        39190,
        39189,
        39198,
        39226,
        39213,
        39208,
        39199,
        39202,
        39208,
        39204,
        39216,
        39207,
        39222,
        39219,
        39236,
        39235,
        39244,
        39235,
        39262,
        39246,
        39250,
        39250,
        39256,
        39251,
        39253,
        39257,
        39252,
        39252,
        39244,
        39253,
        39253,
        39276,
        39263,
        39262,
        39269,
        39263,
        39268,
        39267,
        39267,
        39272,
        39262,
        39275,
        39280,
        39271,
        39286,
        39278,
        39270,
        39273,
        39277,
        39277,
        39291,
        39278,
        39288,
        39278,
        39279,
        31901,
        29246,
        29174,
        29146,
        29142,
        29132,
        29135,
        29140,
        29142,
        29155,
        29136,
        29155,
        29143,
        29161,
        29165,
        29156,
        29173,
        29168,
        29177,
        29179,
        29173,
        29184,
        29189,
        29178,
        29186,
        29191,
        29185,
        29185,
        29195,
        29201,
        29201,
        29210,
        29196,
        29202,
        29208,
        29210,
        29211,
        29214,
        29214,
        29224,
        29216,
        29218,
        29216,
        29230,
        29230,
        29235,
        29244,
        29237,
        29237,
        29240,
        29239,
        29241,
        29233,
        29234,
        29244,
        29246,
        29234,
        29251,
        29252,
        29241,
        39060,
        39114,
        39130,
        39114,
        39127,
        39128,
        39134,
        39122,
        39125,
        39125,
        39122,
        39118,
        39130,
        39120,
        39134,
        39127,
        39139,
        39128,
        39130,
        39138,
        39137,
        39126,
        39110,
        39118,
        39132,
        39122,
        39124,
        39122,
        39116,
        39136,
        39142,
        39133,
        39123,
        39145,
        39120,
        39136,
        39136,
        39139,
        39136,
        39144,
        39136,
        39126,
        39152,
        39143,
        39153,
        39141,
        39154,
        39136,
        39144,
        39141,
        39138,
        39145,
        39151,
        39152,
        39148,
        39134,
        39159,
        39148,
        39146,
        39139,
        40562,
        40584,
        40571,
        40593,
        40574,
        40588,
        40584,
        40591,
        40595,
        40598,
        40595,
        40598,
        40598,
        40574,
        40596,
        40609,
        40592,
        40604,
        40582,
        40582,
        40591,
        40588,
        40589,
        40580,
        40588,
        40590,
        40601,
        40585,
        40595,
        40595,
        40589,
        40587,
        40592,
        40592,
        40598,
        40593,
        40604,
        40604,
        40604,
        40604,
        40625,
        40587,
        40603,
        40587,
        40591,
        40590,
        40588,
        40592,
        40584,
        40593,
        40590,
        40584,
        40592,
        40590,
        40599,
        40591,
        40599,
        40594,
        40594,
        40609
      ]
    }
  ],
  "app_os": "linux",
  "app_name": "PhotosynQ",
  "app_version": "0.3.9",
  "app_device": "x86-64",
  "location": [
    "42.2310767",
    "-83.7479249"
  ],
  "ConsoleMacro": "382"
}
Default avatar
Created by

Jon S


Protocol connections:
0
Latest Update:
Oct 2017