Skip to content

3. Implementation

SANG WON edited this page Jun 2, 2019 · 9 revisions

Data collection

We collected and analyzed the data through SmartWatch for about a month 
to characterize the smoking behavior.
This graph shows the changes in the x-axis value of the accelerometer sensor 
among sensor values that appear when performing some of the most common actions in everyday life.
The x-axis value of the accelerometer sensor is used 
because the direction of the wrist changes when smoking is performed,
and the value changes most clearly.

Key observation

Through this, we have found two important features. 
The first feature is that the hands are raised and their hands 
are lowered again after about 1 to 1.5 seconds while inhaling the cigarette.
At this time, the sensor data values when the hands were up and down
were measured from -10 to -11 and 7 to 9 on average, respectively.
The second feature is that these patterns repeat on average six or more times.

Algorithm

Based on this, we implemented the algorithm in two steps. 
First, the suction operation is determined. 
Inhalation here refers to the process of raising your hand,
inhaling the cigarette, and bringing it back down. 
If this suction operation is repeated 6 or more times within a certain time,
it is judged that smoking is finally possible. 
In addition, the user can determine whether or not 
he / she wears a smart watch through the heart rate sensor value.

This is our algorithm code.

   @Override
    public void onSensorChanged(SensorEvent event) {
        //Sending data
        //   client.sendSensorData(event.sensor.getType(), event.accuracy, event.timestamp, event.values);
        if(event.sensor.getType()==1) {
            //   sample[count] = event.values[0];
            if(event.values[0]>=-10||event.values[0]<=10) //noise filtering
                sum+=event.values[0];
            count++;
            Log.d("smoking", "" + event.values[0]);
        }
        else if(event.sensor.getType()==Sensor.TYPE_HEART_RATE)
        {
            sum2+=event.values[0];
            count2++;
            Log.d("heart"," "+event.values[0]);
        }

        long now=System.currentTimeMillis();

        if(now-currentTime>=windowSize) //each windowSize
        {
            currentTime=now;
            Log.d("smoking"," a number of counts per second"+count);
            Log.d("smoking"," Average value per second"+sum/count);
            if(sum/count>=Handsup_Threshold)  //status: hands up
            {
                if(!isStart) {
                    Log.d("smoking","hands up / measurement start!");
                    startTime = currentTime;
                    isStart = true;
                }
                else
                {
                    if(currentTime-startTime>=4000) //Reset when hand does not go down for more than 4 seconds
                    {
                        Log.d("smoking","measurement finish!");
                        isStart = false;
                    }
                }
            }

            else if(sum/count<=handsdown_Threshold) //status: hands down
            {
                if (isStart)
                {
                    Long temp=now-startTime;

                    int min=temp.compareTo(1000L);
                    int max=temp.compareTo(5000L);
                    if (min>=0 && max<=0) //
                    {
                        count2=0;
                        temp=now-smokeTime;
                        min=temp.compareTo(3000L);
                        max=temp.compareTo(30000L);
                        if (min<=0 || max>=0)
                            totcount = 1;
                        else {
                            totcount++;
                            Log.d("smoking","detecting smoking action");

                            if (totcount == 5) //detect smoking action more than 5 times
                            {
                                Log.d("smoking","detecting smoking!");
                                client.sendSensorData(event.sensor.getType(), event.accuracy, now, event.values); //Sending data
                                totcount=0;
                            }
                        }
                        smokeTime = now;
                    }
                    else
                        Log.d("smoking","Not smoking action! / measurement finish!");
                    isStart = false;

                }
            }
            count=0;
            sum=0.0;
        }
    }
Clone this wiki locally