← Back to Blog

Embedded Knock Detection: Offline Knock Recognition SDK for Doorbells and CareNEW

The Value of Knock Detection: Scenarios and Workflow

Not every visitor rings the bell: couriers knock twice and leave, neighbors tap the window, kids can't reach the button. Pure button-triggered doorbells miss these visitors; motion detection can't tell "someone walked by" from "someone knocked". Knock detection closes the gap — and the same engine, reconfigured, becomes a call-for-help channel in elderly care.

Doorway scene and event chain
Knock → detect → wake → notify, end to end
Business flow across visitor / device / user lanes
The knock event business flow (swimlane view)

The Signature: Knock vs Footsteps vs Door Slam

Knock signature comparison
Pulse groups vs low-frequency footsteps vs slow-decay slams

Below is a real knocking recording — waveform (top) and Mel spectrogram (bottom). The distinct short pulses stand out against a clean background:

Real knocking sample: waveform and spectrum
Real knocking sample: waveform (top) + Mel spectrogram (bottom)

Audio samples (real recordings — press play):

🔊 Knocking (real sample)

🔊 Another knock recording

Knock — — 2–4 pulse events in a group, with a stable human rhythm; pulse grouping is the core evidence

Footsteps — — low-frequency, wide-envelope, continuous repetition; no sharp impacts

Door slam — — low-frequency large single burst with slower decay; spectrum centroid far lower

Event-based detection is naturally insensitive to wind noise: continuous sounds carry no pulse structure.

How Recognition Works: Preprocessing → Features → Algorithms

End-to-end pipeline
Capture, detection, interference filtering, wake-up, push

The pipeline: capture → knock event detection (transient + feature judgment, with an interference-filter branch rejecting footsteps, door slams and wind) → video wake-up → notification. Fast response matters: waking a video doorbell the moment a knock lands.

On-device architecture
Capture, processing, engine, output, application

Four Application Forms

Four application forms
Doorbell, lock, unattended store, elderly care

Smart doorbells — — wake the video the instant a knock occurs; push a "someone is knocking" alert

Smart locks — — add visitor awareness with zero extra hardware

Unattended stores — — after-hours knock alerts with remote verification

Elderly care — — knocking as a call for help (below)

Elderly Care: Knocking as a Call for Help

For seniors with limited mobility, the smartphone is far away and the pendant is easy to forget — but the bed rail is right there. Knocking on a bed rail or table is a natural, low-effort call for help.

Elderly bedroom scene
Bed rail knocking, bedside device, child-side notification
Graded response flow
Single tap vs continuous urgent knocks, and escalation rules

Single light knock — — push notification only (could be turning in sleep)

3+ continuous knocks or high-strength pattern — — emergency tier: phone call plus strong push with audio

Night hours (22:00–07:00) — — more sensitive thresholds

No response within 2 minutes — — escalate to the next contact (neighbor, relative, caregiver)

Design considerations
Sensitivity tiers, interference handling, low power, privacy

Sensitivity tiers between day and night; pulse-rhythm discrimination between intentional knocking and accidental bumps; long standby on battery; and privacy-friendliness — audio never leaves the device, and pure-acoustic setups need no camera. The common principle of elder-care products: better a few extra notifications than a missed call for help.

Timing and response data
Hourly distribution (58% at night) and response metrics (illustrative)

Knock events cluster at night — precisely when response is hardest. The response chain targets: notification within 5 seconds, two-way talk within a minute, escalation within two minutes.

Positioning statement: a care-assistance tool for faster human response — for true emergencies, call emergency services directly.

Accuracy and Performance

Detection data
Detection rates by door type and distance (illustrative)

Detection stays above 94% from 0.5 m to 2 m — heavier doors attenuate knocks, so near-field mic placement matters. Deployment points: mic near the door's inner surface; avoid motors and fans; calibrate by knocking on the actual door at install time.

Item
Spec
Accuracy
95.0%
False alarm rate
<2%
Model size
0.2–1 MB (INT8)
Inference latency
Configurable to platform resources
Sample rate
16 kHz
Platforms
ARM Linux / MIPS / x86_64; SVP / Magik pre-adapted

Note: performance figures are based on internal test environments; actual results depend on hardware and deployment scenarios.

Platform and Hardware Requirements

Doorbell and lock-class SoCs; CPU under 50 MHz; shares the SoC with video and networking; battery-friendly event architecture for lock and care devices.

C API and Embedded Integration

The knock detection library exposes a concise streaming C API: the caller just keeps feeding 16 kHz mono PCM; framing, Mel preprocessing and model inference run internally, and frame-level probabilities are aggregated by the alarm strategy into event callbacks.

Full interface declaration (knock_detect.h):

knock_detect.hc
/**
 * knock_detect.h — 敲击声识别统一接口
 *
 * 封装 Mel 预处理 + 推理引擎 + 报警策略, 内部模型消费线程处理音频。
 * 与录音模块 (audio_capture.h) 相互独立: 调用者自行决定音频来源
 * (录音回调 / wav 文件 / 网络流), 通过 knock_detect_feed 送入, 数据任意大小。
 *
 * 用法 (实时录音模式):
 *   knock_detect_t *d = knock_detect_create(mgk_path, NULL, NULL);
 *   knock_detect_set_listener(d, on_frame, on_onset, on_offset, NULL);
 *   knock_detect_start(d);                          // 启动内部模型消费线程
 *   audio_capture_start(rec, capture_cb, d);        // 录音回调里调 knock_detect_feed
 *   ...
 *   knock_detect_stop(d);                           // 排空缓冲, 停止线程
 *   knock_detect_destroy(d);
 *
 * 用法 (wav 文件模式):
 *   knock_detect_t *d = knock_detect_create(mgk_path, NULL, NULL);
 *   knock_detect_set_listener(d, on_frame, on_onset, on_offset, NULL);
 *   knock_detect_start(d);
 *   循环读文件: knock_detect_feed(d, pcm, n);       // 任意数据大小
 *   knock_detect_stop(d);
 *   knock_detect_destroy(d);
 */

#ifndef KNOCK_DETECT_H
#define KNOCK_DETECT_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 识别事件 (报警策略输出, 用于事件结束回调) */
typedef struct {
    float start_time;       /* 事件开始时间 (秒) */
    float end_time;         /* 事件结束时间 (秒) */
    float confidence;       /* 事件置信度 */
    float max_confidence;   /* 事件内最大帧置信度 */
    int   frame_count;      /* 事件持续帧数 */
} knock_detect_event_t;

/* 帧级回调: 每帧识别结果 (模型线程内执行) */
typedef void (*knock_detect_frame_cb_t)(float knock_prob, float timestamp,
                                        void *user_data);

/* 事件开始回调: 策略判定敲击事件开始, 只有开始时间 */
typedef void (*knock_detect_onset_cb_t)(float start_time, void *user_data);

/* 事件结束回调: 策略判定敲击事件结束 (或停止识别时未结束的事件), 完整事件信息 */
typedef void (*knock_detect_offset_cb_t)(const knock_detect_event_t *event,
                                         void *user_data);

typedef struct knock_detect_s knock_detect_t;

/* 创建/销毁; alarm_name/alarm_params 可传 NULL (用默认策略及参数) */
knock_detect_t *knock_detect_create(const char *mgk_path,      /* 模型文件路径 (必填) */
                                    const char *alarm_name,    /* 报警策略名, NULL=默认 */
                                    const char *alarm_params); /* 策略参数 key=val,key=val, NULL=默认 */
void knock_detect_destroy(knock_detect_t *det);

/* 设置事件回调 (create 后调用, 也可在运行中调整); 不需要的回调传 NULL */
void knock_detect_set_listener(knock_detect_t *det,
                               knock_detect_frame_cb_t  on_frame,
                               knock_detect_onset_cb_t  on_onset,
                               knock_detect_offset_cb_t on_offset,
                               void *user_data);

/* 启动/停止识别: 启动内部模型消费线程 / 排空缓冲后停止线程 */
int  knock_detect_start(knock_detect_t *det);
void knock_detect_stop(knock_detect_t *det);
int  knock_detect_is_running(knock_detect_t *det);

/* 设置事件识别策略 (可在运行中调整) */
int knock_detect_set_alarm(knock_detect_t *det, const char *alarm_name,
                           const char *alarm_params);

/* 送入 PCM 数据 (16bit 单声道 16kHz), 线程安全, 任意数据大小 */
int knock_detect_feed(knock_detect_t *det, const int16_t *pcm, int num_samples);

#ifdef __cplusplus
}
#endif

#endif /* KNOCK_DETECT_H */

A minimal WAV-inference demo (excerpt; the full file ships at src/knockDetect/c/knock_demo.c):

knock_demo.cc
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "knock_detect.h"

#define DEFAULT_MGK  "knock_detect_v7.mgk"  /* 模型文件 */
#define DEFAULT_WAV  "test_knock.wav"       /* 16kHz 单声道 16bit PCM */
#define FEED_CHUNK   16000                  /* 每次送入 1 秒音频 */

/* 帧级回调: 每帧输出敲击声概率 (约 1 秒一帧) */
static void on_frame(float knock_prob, float timestamp, void *user_data)
{
    (void)user_data;
    printf("%8.2fs  knock=%.4f\n", timestamp, knock_prob);
}

/* 事件开始回调: 策略判定敲击事件开始 */
static void on_onset(float start_time, void *user_data)
{
    (void)user_data;
    printf("[EVENT] knock start at %.2fs\n", start_time);
}

/* 事件结束回调: 应用层可据此做后续统计或分级响应 */
static void on_offset(const knock_detect_event_t *ev, void *user_data)
{
    (void)user_data;
    printf("[EVENT] knock end at %.2fs (dur=%.2fs, conf=%.3f, frames=%d)\n",
           ev->end_time, ev->end_time - ev->start_time,
           ev->confidence, ev->frame_count);
}

static int read_wav_pcm(const char *path, int16_t **pcm, int *n, int *sr);  /* 完整实现见源文件 */

int main(void)
{
    knock_detect_t *det;
    int16_t *pcm = NULL;
    int num_samples = 0, sample_rate = 0;
    int pos;

    if (read_wav_pcm(DEFAULT_WAV, &pcm, &num_samples, &sample_rate) != 0)
        return 1;

    /* 1. 创建识别器: 模型文件 + 默认报警策略 (NULL) */
    det = knock_detect_create(DEFAULT_MGK, NULL, NULL);
    if (!det) return 1;

    /* 2. 注册回调 (均为可选) */
    knock_detect_set_listener(det, on_frame, on_onset, on_offset, NULL);

    /* 3. 启动内部模型消费线程 */
    knock_detect_start(det);

    /* 4. 分块送入 PCM; 实时录音时改在录音回调里 feed */
    for (pos = 0; pos < num_samples; pos += FEED_CHUNK) {
        int n = num_samples - pos;
        if (n > FEED_CHUNK) n = FEED_CHUNK;
        knock_detect_feed(det, pcm + pos, n);
    }

    /* 5. 停止并销毁 */
    knock_detect_stop(det);
    knock_detect_destroy(det);
    free(pcm);
    return 0;
}

Build and run:

buildbash
$(CC) knock_demo.c -I. -L. -lknockdetect -lpthread -lm -o knock_demo
./knock_demo

Conclusion

Knocking is the simplest interface a device can listen for — from visitor awareness at the door to a call for help at the bedside. An online trial with care-scenario support is available.