Exemple #1
0
        public int HRProcesssing(byte[] data, Size size)
        {
            if (Interlocked.Exchange(ref processing, 1) == 1)
            {
                return(-1);
            }

            int    width  = (int)size.Width;
            int    height = (int)size.Height;
            double RedAvg;
            double GreenAvg;

            GreenAvg = ImageProcessing.DecodeYUV420SPtoRedBlueGreenAvg(ImageProcessing.Clone(data), height, width, 3);           //1 stands for red intensity, 2 for blue, 3 for green
            RedAvg   = ImageProcessing.DecodeYUV420SPtoRedBlueGreenAvg(ImageProcessing.Clone(data), height, width, 1);           //1 stands for red intensity, 2 for blue, 3 for green
            GreenAvgList.Add(GreenAvg);
            RedAvgList.Add(RedAvg);

            ++counter;             //countes number of frames in 30 seconds
            System.Diagnostics.Debug.WriteLine($"Processing {size.Width} {size.Height} red avg {RedAvg}");
            //To check if we got a good red intensity to process if not return to the condition and set it again until we get a good red intensity
            if (RedAvg < 200)
            {
                counter = 0;
                //inc = 0;
                //ProgP = inc;
                //ProgO2.setProgress(ProgP);
                Interlocked.Exchange(ref processing, 0);
            }


            long   endTime         = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            double totalTimeInSecs = (endTime - startTime) / 1000d;             //to convert time to seconds

            if (totalTimeInSecs >= 30)
            {
                startTime    = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                SamplingFreq = (counter / totalTimeInSecs);
                Double[] Green = GreenAvgList.ToArray();
                Double[] Red   = RedAvgList.ToArray();

                SamplingFreq = (counter / totalTimeInSecs);                 //calculating the sampling frequency

                double HRFreq  = FFT(Green, counter, SamplingFreq);         // send the green array and get its fft then return the amount of heartrate per second
                double bpm     = (int)Math.Ceiling(HRFreq * 60);
                double HR1Freq = FFT(Red, counter, SamplingFreq);           // send the red array and get its fft then return the amount of heartrate per second
                double bpm1    = (int)Math.Ceiling(HR1Freq * 60);

                if ((bpm > 45 || bpm < 200))
                {
                    if ((bpm1 > 45 || bpm1 < 200))
                    {
                        bufferAvgB = (bpm + bpm1) / 2;
                    }
                    else
                    {
                        bufferAvgB = bpm;
                    }
                }
                else if ((bpm1 > 45 || bpm1 < 200))
                {
                    bufferAvgB = bpm1;
                }
                else
                {
                    bufferAvgB = bpm1;
                }

                Beats = (int)bufferAvgB;
            }

            if (Beats != 0)
            {
            }

            if (RedAvg != 0)
            {
                //                    ProgP=inc++/34;;
                //                    ProgO2.setProgress(ProgP);
            }

            Interlocked.Exchange(ref processing, 0);
            return(Beats);
        }
Exemple #2
0
        public int O2Processsing(byte[] data, Size size)
        {
            if (Interlocked.Exchange(ref processing, 1) == 1)
            {
                return(-1);
            }

            int    width  = (int)size.Width;
            int    height = (int)size.Height;
            double RedAvg;
            double BlueAvg;

            RedAvg  = ImageProcessing.DecodeYUV420SPtoRedBlueGreenAvg(ImageProcessing.Clone(data), width, height, 1);            //1 stands for red intensity, 2 for blue, 3 for green
            sumred  = sumred + RedAvg;
            BlueAvg = ImageProcessing.DecodeYUV420SPtoRedBlueGreenAvg(ImageProcessing.Clone(data), width, height, 2);            //1 stands for red intensity, 2 for blue, 3 for green
            sumblue = sumblue + BlueAvg;

            RedAvgList.Add(RedAvg);
            BlueAvgList.Add(BlueAvg);

            ++counter;             //countes number of frames in 30 seconds

            System.Diagnostics.Debug.WriteLine($"Processing {size.Width} {size.Height} red avg {RedAvg}");

            //To check if we got a good red intensity to process if not return to the condition and set it again until we get a good red intensity
            if (RedAvg < 200)
            {
                //inc = 0;
                //ProgP = inc;
                //ProgO2.setProgress(ProgP);
                Interlocked.Exchange(ref processing, 0);
            }

            long   endTime         = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            double totalTimeInSecs = (endTime - startTime) / 1000d;             //to convert time to seconds

            //when 30 seconds of measuring passes do the following " we chose 30 seconds to take half sample since 60 seconds is normally a full sample of the heart beat
            if (totalTimeInSecs >= 30)
            {
                startTime    = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                SamplingFreq = (counter / totalTimeInSecs);
                Double[] Red    = RedAvgList.ToArray();
                Double[] Blue   = BlueAvgList.ToArray();
                double   HRFreq = FFT(Red, counter, SamplingFreq);
                double   bpm    = (int)Math.Ceiling(HRFreq * 60);

                double meanr = sumred / counter;
                double meanb = sumblue / counter;

                for (int i = 0; i < counter - 1; i++)
                {
                    Double bufferb = Blue[i];

                    Stdb = Stdb + ((bufferb - meanb) * (bufferb - meanb));

                    Double bufferr = Red[i];

                    Stdr = Stdr + ((bufferr - meanr) * (bufferr - meanr));
                }

                double varr = Math.Sqrt(Stdr / (counter - 1));
                double varb = Math.Sqrt(Stdb / (counter - 1));

                double R = (varr / meanr) / (varb / meanb);

                double spo2 = 100 - 5 * (R);
                o2 = (int)(spo2);

                if ((o2 < 80 || o2 > 99) || (bpm < 45 || bpm > 200))
                {
                    //inc = 0;
                    //ProgP = inc;
                    //ProgO2.setProgress(ProgP);
                    //mainToast = Toast.makeText(getApplicationContext(), "Measurement Failed", Toast.LENGTH_SHORT);
                    //mainToast.show();
                    startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                    counter   = 0;
                    Interlocked.Exchange(ref processing, 0);
                    return(-2);
                }
            }

            if (o2 != 0)
            {
            }

            if (RedAvg != 0)
            {
                //                    ProgP=inc++/34;;
                //                    ProgO2.setProgress(ProgP);
            }

            Interlocked.Exchange(ref processing, 0);

            return(o2);
        }