/// SETUP JSON

        public void SetupNewSession(string folder)
        {
            filepath      = folder + "/" + filename;
            sessionValues = new SessionVariables();
            sessionValues.CreateDictionaryOfTimeSeries();
            isConfigured = true;
        }
 public void StopCurrentSession()
 {
     if (isConfigured)
     {
         // Save file
         SaveSettings();
         // Reset values
         sessionValues = null;
         filepath      = null;
         isConfigured  = false;
     }
 }
 /// <summary>
 /// Load json from disk
 /// </summary>
 public SessionVariables LoadSessionValues(string filepath)
 {
     if (File.Exists(filepath))
     {
         sessionValues = JsonUtility.FromJson <SessionVariables>(File.ReadAllText(filepath));
         sessionValues.RemoveEmptyTimeSeries();
         return(sessionValues);
     }
     else
     {
         // If the file does not exist, create one with default values and read from it.
         Debug.Log("The file does not exist in " + filepath);
         return(null);
     }
 }
예제 #4
0
        public static bool Calculate(SessionVariables sessionData)
        {
            // Check that both arrays are the same length
            int N = sessionData.RMSSD.timestamp.Count;

            // Assess length of each timeseries, by default are assummed equal
            bool equalLength = true, useRRi = false, useRMSSD = false;

            if (sessionData.RRi.timestamp.Count != sessionData.RMSSD.timestamp.Count)
            {
                // timeseries are different length
                equalLength = false;
                N           = Math.Min(sessionData.RRi.timestamp.Count, sessionData.RMSSD.timestamp.Count);

                // Which is the shorter signal?
                if (N == sessionData.RRi.timestamp.Count)
                {
                    useRRi = true;
                }
                else if (N == sessionData.RMSSD.timestamp.Count)
                {
                    useRMSSD = true;
                }
            }

            // Mean values
            float meanRRi   = 0f;
            float meanRMSSD = 0f;

            for (int i = 0; i < N; i++)
            {
                // Check timestamps are similar :: +/-10ms offset
                if (Math.Abs(sessionData.RRi.timestamp[i] - sessionData.RMSSD.timestamp[i]) > 0.1f)
                {
                    ExciteOMeterManager.DebugLog("WARNING: Two timestamps differ in more than 0.1s");
                }

                // Cumulative sum
                meanRRi   += sessionData.RRi.value[i];
                meanRMSSD += sessionData.RMSSD.value[i];
            }
            meanRRi   = meanRRi / N;
            meanRMSSD = meanRMSSD / N;

            // Standard deviation
            float stdRRi   = 0f;
            float stdRMSSD = 0f;

            for (int i = 0; i < N; i++)
            {
                stdRRi   += (float)Math.Pow(sessionData.RRi.value[i] - meanRRi, 2);
                stdRMSSD += (float)Math.Pow(sessionData.RMSSD.value[i] - meanRMSSD, 2);
            }
            stdRRi   = (float)Math.Sqrt(stdRRi / N);
            stdRMSSD = (float)Math.Sqrt(stdRMSSD / N);

            // Placeholder for z-score
            double zScoreRRi = 0f, percentileRRi = 0f;
            double zScoreRMSSD = 0f, percentileRMSSD = 0f;

            // Final EoM level
            float timestampEOM = 0.0f;
            float valueEOM     = 0.0f;

            // Calculate final EoM level
            for (int i = 0; i < N; i++)
            {
                zScoreRRi   = (double)((sessionData.RRi.value[i] - meanRRi) / stdRRi);
                zScoreRMSSD = (double)((sessionData.RMSSD.value[i] - meanRMSSD) / stdRMSSD);

                percentileRRi   = Phi(zScoreRRi);
                percentileRMSSD = Phi(zScoreRMSSD);

                // Average of percentile of RRI and percentile of RMSSD
                valueEOM = (1 - (float)(percentileRRi + percentileRMSSD) / 2.0f);

                if (equalLength)
                {
                    timestampEOM = (sessionData.RRi.timestamp[i]);
                }
                else if (useRRi)
                {
                    timestampEOM = (sessionData.RRi.timestamp[i]);
                }
                else if (useRMSSD)
                {
                    timestampEOM = (sessionData.RMSSD.timestamp[i]);
                }

                // Add to class that is exported as JSON
                SessionVariablesController.instance.WritePostProcessedExciteOMeterIndex(timestampEOM, valueEOM);
                // Add to CSV file
                LoggerController.instance.WriteLine(LogName.EOM, timestampEOM.ToString("F6") + "," + valueEOM.ToString("F5"));
            }

            return(true);
        }