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.
    public void onSensorChanged(SensorEvent event) {
        //데이터 전송
        //   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 filter
                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) //windowSize 마다
        {
            currentTime=now;
            Log.d("smoking"," 초당 데이터 개수"+count);
            Log.d("smoking","초당 평균 값"+sum/count);
            if(sum/count>=5)  //손이 올라간 상태
            {
                if(!isStart) {
                    Log.d("smoking","손이 올라감 / 측정 시작!");
                    startTime = currentTime;
                    isStart = true;
                }
                else
                {
                    if(currentTime-startTime>=4000) //손이 4초이상 아래로 내려가지 않을 때 초기화
                    {
                        Log.d("smoking","손이 올라갔는데 안내려감 / 측정 종료!");
                        isStart = false;
                    }
                }
            }

            else if(sum/count<=-7) //손이 내려간 상태
            {
                if (isStart) //올라가 있었다면
                {
                    Long temp=now-startTime;

                    int min=temp.compareTo(1000L);
                    int max=temp.compareTo(3000L);
                    //             Log.d("smoking","비교"+min+" "+max);
                    if (min>=0 && max<=0&&sum2/count2>10) //1~3초동안 지속되었다면
                    {
                        sum2=0;
                        count2=0;
                        temp=now-smokeTime;
                        min=temp.compareTo(3000L);
                        max=temp.compareTo(30000L);
                        if (min<=0 || max>=0) //3~30초 사이 안에 이루어지지 않았다면
                            totcount = 1;
                        else {
                            totcount++;
                            Log.d("smoking","흡입동작 관측!");

                            if (totcount == 5) //5회 이상 smoking action 감지시
                            {
                                Log.d("smoking","흡연 탐지!");
                                client.sendSensorData(event.sensor.getType(), event.accuracy, now, event.values); //데이터 전송
                                totcount=0;
                            }
                        }
                        smokeTime = now;
                    }
                    else
                        Log.d("smoking","흡연동작은 아님! / 측정 종료!");
                    isStart = false;

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