element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
My Project Is Trash
  • Challenges & Projects
  • Design Challenges
  • My Project Is Trash
  • More
  • Cancel
My Project Is Trash
Forum Knock-Knock-Sortin' #7 - EdgeImpulse
  • News
  • Forum
  • Projects
  • Enroll
  • Leaderboard
  • Files
  • Members
  • More
  • Cancel
  • New
Join My Project Is Trash to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 1 reply
  • Subscribers 2 subscribers
  • Views 41 views
  • Users 0 members are here
  • edgeimpulse
  • KnockKnockSort
  • my-project-is-trash
Related

Knock-Knock-Sortin' #7 - EdgeImpulse

amgalbu
amgalbu 17 hours ago

In post #6, I collected the first knock dataset: 80 recordings of an item hitting the plate, 20 each for paper, plastic, metal and noise. Each recording holds what the Nicla Vision under the plate heard with its microphone and felt with its accelerometer. In this post, I will build a small neural network to identify the four classes using Edge Impulse.

Introducing Edge Impulse

Edge Impulse is a web tool for building machine learning models for small boards. You upload data, tell it what each one is, and it trains a model that you can download and run on the board. The chain from the raw signal to the answer is called an impulse, and it has three parts:

  1. A window. The piece of signal that the model looks at in one go. Each of my recordings is 500 ms long, starting about 50 ms before the knock, and I used the whole recording as one window.
  2. A processing block. Half a second of sound is 8,000 numbers, far too many to learn from with only 80 examples. A processing block turns each window into a shorter list of numbers that describe it, called features. An example of a feature could be "how much energy is there around 2 kHz in this slice of time".
  3. A classifier. A small neural network that reads the features and gives a score to each class. The class with the highest score is the answer.

Iteration #1

For the first try, I took what Studio suggests:

image

image

  • For the microphone, the MFE block, which splits the sound into short slices of 20 ms and measures the energy in 40 frequency bands, spaced the way the human ear hears them. That gives 1,960 features (49 slices × 40 bands, since 20 ms slices overlap every 10 ms).
  • For the accelerometer, the Spectral Analysis block on all three axes, which gives a few statistics (how strong the vibration is, how spiky it is) and the strength of each frequency. That gives 168 more features.
  • A simple network with two layers of 20 and 10 neurons.

To check a model, you need recordings it has never seen. So the data is split in two: the training set, about 80% of the recordings, which the model learns from, and the test set, the other 20%, which is kept apart and used only to check it. Edge Impulse makes this split at random when you upload the data.

The first training was right on all 16 test recordings, three times out of three.

image

If this sounds weird, it's because it is weird. The problem is clearly in the recordings included in the test set.
In this dataset the recordings of each class come in groups that are very similar to each other. Data has been collected with the same three items, for this reason samples are very similar (just a few milliseconds and a few dB apart). With a random split, almost every test recording has a close resemble of a recording in the training set. The model doesn't need to learn what paper sounds like: it only needs to remember a knock it has already heard. This is called overfitting, a model that memorizes its examples instead of learning the general rule, and a random split can't see it.
So, before making any change to the impulse, I changed the test in this way

  1. I kept each group of similar recordings together, either all in training or all in test.
  2. I split the data in five different ways, called folds. Each fold keeps apart one group of paper, plus four plastic, four metal and four noise recordings. Over the five folds, every recording is tested once by a model that never saw it.
  3. I trained each fold three times and took the average, because with 16 test recordings, one mistake changes the score by 6%.

Studio has one test set per project, so I ran these folds on my PC, with the same processing code and the same network as in Studio.

The same model, tested with folds, has an accuracy of 93.8%. Accuracy is the percentage of test recordings that has been classified correctly (i.e. plastic bottle identified as plastic bottle). In Studio, on the one fold I set up there, the drop is even bigger: from 100% on the validation set to 81.25% on the test set.

image

Accuracy says how often the model is wrong, but not how. For that there is the confusion matrix, a table with the true class on the rows and the answer of the model on the columns. The diagonal holds the right answers, and every number outside it is a specific mistake.

On the left, 13 of the 60 paper answers are "metal". When the model hasn't heard a specific paper knock before, it takes it for a can. That's not a crazy mistake: in post #6, paper was the loudest class on this plate and the can the brightest, and both stand out against plastic and noise in the same way.

Another way to read this is the recall of each class: the percentage of recordings of that class that got the right answer. Here paper recall is 78%: 78 paper knocks out of 100 are recognized, and the rest are called something else.

Iteration #2

Before proceeding, I wanted to know which information were more relevant to the model, so I trained it on each sensor alone:

Model input Accuracy Noise sorted as an item
Microphone only 93.8 % 0 %
Accelerometer only, 3 axes 69.2 % 11.7 %
Accelerometer only, vertical axis 46.7 % 65.0 %
Both 93.8 % 1.7 %

The microphone alone does as well as both together, and the accelerometer alone is poor: it mixes up metal and paper. That matches post #6, where the plate rang at about 700 Hz whatever landed on it. The accelerometer mostly hears the plate vibrations, not the item.

Still, the microphone-only model classifies 8 plastic and 6 paper as "noise", and the accelerometer is the sensor that knows something really hit the plate. Room noise in a kitchen doesn't reach it either. So I kept both, and started working on the microphone.

Iteration #3

With 80 recordings, the model currently uses 2,128 features, namely

  • 1,960 microphone features (49 slices x 40 bands)
  • 168 accelerometer features (the Spectral Analysis block gives 56 numbers per axis: 5 statistics (RMS, skewness, kurtosis, and the skewness and kurtosis of the spectrum) plus 51 frequency bins of about 31 Hz up to the 1600 Hz cut-off. For three axes, this gives 3 x 56 = 168)

The network has far more numbers to look at than examples to learn from, which is exactly the situation where it starts to memorize. My idea was that fewer, more meaningful features would help. I made the MFE slices longer, 32 ms instead of 20 ms, so each slice includes a whole bounce of the item. The microphone now gives 1,200 features instead of 1,960. I also told the MFE block to ignore everything below 80 Hz, where the microphone has a slow drift.

The accuracy went up to 96.2%, and paper recall from 78% to 88%.

Iteration #4

Following the same idea, I cut the accelerometer down to 11 features and from the vertical axis only (the one perpendicular to the plate, which should collect to most important information about the impact).

The result was not what I expected: accuract fell to 92.5%, and 10% of the noise sorted as an item. I think the problem is that the accelerometer features measure how "spiky" the hit is. A few big acceleration values provided a strong bias to the classifier, so the model started to ask "was there a sharp hit?" more than "what did it sound like?". A knock on the frame is a sharp hit too, so this also explain the noise classified as an item.

Iteration #5

Edge Impulse has an expert mode, where you can write the network yourself in Python with Keras, so I switched to it.

First I tried the network that Edge Impulse suggests for audio, a convolutional network (CNN): instead of looking at every feature separately, it slides small filters along the sound to find patterns, a bit like looking for shapes in a picture. On the microphone alone it scored 85.8%, and it called 29 of 60 noises "plastic". A plastic bottle gives a short, soft thud, and so does a knock on the frame. Without the accelerometer, the microphone can't tell them apart.

Then I built a network with two branches. The sound features are arranged as a small image (time on one side, frequency on the other) and go through a CNN that looks at both directions together. The accelerometer features go through a separate, tiny branch. The two meet only at the end. This way the microphone does most of the work, and the accelerometer can add its opinion without drowning the sound.

Network Accelerometer Accuracy Noise sorted as an item
Two branches, CNN along time only 3 axes 97.1 % 8.3 %
Two branches, CNN along time only  vertical axis 77.9 % 45.0 %
Two branches, CNN on time and frequency  vertical axis 98.8 % 0 %
Two branches, CNN on time and frequency  3 axes 100.0 % 0 %

The CNN that looks at time and frequency together is clearly the better one, and with it the small accelerometer block finally works.

The last two models are the better ones. Since the Nicla board has limited hardware resources, I chose the model that uses only the accelerometer's vertical axis: this reduces the model size and, consequently, the processing time.

Iteration #6

With the hardware constraints in mind, I wondered whether the whole 500 ms window is important for classification. A shorter window means less work for the Nicla, so I tried 300 ms windows starting at 0, 100 and 200 ms of each recording:

Accuracy of 300 ms windows that start at 0, 100 and 200 ms in the 500 ms recording

image

The window that contains the knock (which is at about 50 ms) scores 97.9%, almost as good as 500 ms. The windows that start after the knock get worse and worse: with only the tail left, 17% of the noise is sorted as an item. On the bin the knock always lands at the same point of the recording, so I kept one window of 500 ms. A can rings for up to 290 ms, and I'd rather not cut the tail that is so useful to correctly classify metal. The 300 ms window stays as a plan B if the Nicla turns out to be slow.

Iteration #7

At this point I had a 98.8% model and I was ready to put it on the board. Because microcontrollers have limited memory, Edge Impulse converts the model to int8: every number is stored in 8 bits instead of a 32-bits floating point. It's a simple and effective way to get a model which is smaller and faster. I tested the converted model on the same folds: 88.8%, with 25% of the noise sorted as an item. Two of my four criteria gone at once.

Left: the range of the numbers each processing block produces. Right: accuracy of the model before and after conversion

An 8-bit number has only 256 possible values, and the conversion has to spread them over the whole range of the input, from the smallest feature to the largest.

There are three possible solutions:

  1. Keep the model in 32 bits. It works (98.8%), but it grows from 50 kB to 172 kB and runs slower.
  2. Scale down signals (especially the accelerometer signal: the spikes don't change with scale).
  3. Describe the accelerometer with a block whose numbers have the same range as the sound features.

Iteration #8

I applied solution 2, so I replaced the accelerometer block with the Spectrogram block on the vertical axis. It measures the energy at each frequency in slices of 64 ms, on the same 0 to 1 scale as the MFE block, so the 8-bit conversion treats both sensors the same way. The network is the same two-branch CNN.

This is how the final impulse looks in Studio, with the two processing blocks feeding one classifier:

image

Windows settings: 500 ms, window increase 500 ms, 16000 Hz.

The MFE block turns the sound into a small picture of energy over time and frequency. The hit is the bright column at the start.

MFE settings: frame length 0.032 s, stride 0.016 s, 40 filters, FFT 512, low frequency 80 Hz, noise floor −52 dB. 1,200 features.

image

The Spectrogram block does the same for the vertical axis of the accelerometer, with fewer and longer slices.

Spectogram settings: frame length 0.064 s, stride 0.064 s, FFT 64, noise floor −52 dB. 231 features.

image

The two-branch network is written in expert mode. The code is at the end of the post:

image

On the Studio test set, the 8-bit version of the final model got all 16 recordings right, including the paper group that the first model left uncertain:

image

Here are the results for the final model

Accuracy on the fair test: 99.2%
Noise sorted as an item: 0% (0 of 60)
Worst result of the stress test: 96.7% (knock 20 ms earlier)
Size on the board: 51 kB, same accuracy as the 32-bit version

Two more numbers I looked at:

  • No item was ever called "noise", so the bin never ignores a real item.
  • Only 0.8% of the answers came with a top score below 60%. The app can send those uncertain items to the landfill bin instead of guessing.

The only mistakes left are 2 out of 60 paper knocks called plastic, the two classes with the most similar soft thud on the plate.

Studio also estimates how the model runs on the board. For the 8-bit network, with the EON Compiler, it gives 17 ms per knock, 16.5 KB of RAM and 79.7 KB of flash. These numbers are for the network only, without the processing blocks, and Studio computes them for the Cortex-M4 target of the project:

image

On the Nicla side, the firmware already knows how to compute the Spectrogram features, so the change is only in the settings (see the end of the post).

What's next?

  • Check that the Nicla computes the same features as Edge Impulse
  • Measure how long the Nicla takes to process a knock, and see if the 300 ms window is needed.
  • Record more drops per class, with more items of each material and retrain
  • Sign in to reply
  • Cancel
  • BigG
    BigG 15 hours ago

    This is great blog describing the data classification process for neural networks using Edge Impulse. It's easy to forget that without this vital step it's simply rubbish in equating to rubbish out.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube