Author Topic: Comparing two pieces of audio on an MCU  (Read 411 times)

0 Members and 1 Guest are viewing this topic.

Offline simonlasnier

  • Frequent Contributor
  • **
  • Posts: 283
  • Country: dk
Comparing two pieces of audio on an MCU
« on: May 25, 2021, 01:17:50 pm »
Hi :)

I am making a test board which has to test the audio output (very short audio) of a device.
The audio does not have to be checked very precisely, just that the audio somehow ressemble the output.

For example - I attached a picture of:
* Top = samples read from the ADC on the test board (plotted on plotvar.com)
* Bottom = audio played from the device (from the oscilloscope)

If a human had to check, I would simply look at the two and say "yea they look similar" - that is good enough for me.
My question: how can I do that check with an MCU (without using an external computer)?

There are about 700 8-bit samples to process. I have:
* In RAM the 700 fresh samples just recorded
* In Flash memory I can have 700 pre-recorded or FFT values, etc. - anything to compare it to

Thank you! :)

Simon

PS: I previously asked about the setup (see here: https://www.eevblog.com/forum/beginners/basic-adc-setup-with-mcu/ ), and thought I would be able to simply compare sample by sample (with a threshold) between the values from the ADC and pre-recorded samples. I am not actually too far but I think it would be better with a "proper" comparison using something like FFT.
 

Offline simonlasnier

  • Frequent Contributor
  • **
  • Posts: 283
  • Country: dk
Re: Comparing two pieces of audio on an MCU
« Reply #1 on: May 25, 2021, 01:22:39 pm »
I am using an STM8 MCU, and I found this code doing FFT and audio processing directly on the MCU.
https://www.st.com/en/embedded-software/stsw-stm8076.html
Thinking it might be a good start?
 

Offline sigma_xi

  • Contributor
  • Posts: 17
  • Country: at
  • Student.
Re: Comparing two pieces of audio on an MCU
« Reply #2 on: May 25, 2021, 02:15:33 pm »
I don't think that transforming your input to the frequency domain via FFT is going to help, since you need to compare recorded values with stored values in either case.

Given that the recorded values are aligned with the stored values, i.e. you have to detect the start of the recorded audio, I suggest to take the difference and apply some kind of norm (https://en.wikipedia.org/wiki/Norm_(mathematics)). The norm can easily be compared to a threshold that you define depending on how large the error between recorded and stored audio may be.

I assume it is sufficient to use a Manhattan norm, so you just compute the absolute value of each difference and sum them up. You can even do this directly within the ADC interrupt routine, so you don't have to store the recorded audio. This saves memory if you don't need the recording afterwards. A draft/pseudo code for the ADC interrupt is

Code: [Select]
error=0; // accumulated error
i=0;  // sample counter

ADC_interrupt(){
  sample=getADCvalue();
  diff=sample-original[i];
  error+= abs(diff);
  i++;
}

And, after all samples have been recorded you just do the comparison
Code: [Select]
if(error>threshold){...}
else{...}
 

Offline sigma_xi

  • Contributor
  • Posts: 17
  • Country: at
  • Student.
Re: Comparing two pieces of audio on an MCU
« Reply #3 on: May 25, 2021, 02:21:55 pm »
As for the threshold, you can derive it from the average tolerance of each sample.

For example, if each recorded sample may differ by +- 4 from the original value, then the threshold for the error is 700*4 = 2800. However, this does not guarantee that each sample is just +-4 off. It only ensures an average of +-4.
 

Offline Marco

  • Super Contributor
  • ***
  • Posts: 5160
  • Country: nl
Re: Comparing two pieces of audio on an MCU
« Reply #4 on: May 25, 2021, 02:29:47 pm »
I don't think that transforming your input to the frequency domain via FFT is going to help, since you need to compare recorded values with stored values in either case.

If the sampling is not completely synchronous with the test signal generation, but relies on a trigger instead, and the test signal has content near 1/2 sampling rate then the FFT is an alternative to resampling. Without resampling a fractional sample misalignment from a digital trigger could make sample by sample comparison go woefully wrong.
 

Offline fcb

  • Super Contributor
  • ***
  • Posts: 1991
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: Comparing two pieces of audio on an MCU
« Reply #5 on: May 25, 2021, 02:36:24 pm »
Autocorrelation, probably of an FFT.
https://en.wikipedia.org/wiki/Autocorrelation
**New USB Picoammeter available now**
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters.
 

Offline sigma_xi

  • Contributor
  • Posts: 17
  • Country: at
  • Student.
Re: Comparing two pieces of audio on an MCU
« Reply #6 on: May 25, 2021, 02:41:19 pm »
I don't think that transforming your input to the frequency domain via FFT is going to help, since you need to compare recorded values with stored values in either case.

If the sampling is not completely synchronous with the test signal generation, but relies on a trigger instead, and the test signal has content near 1/2 sampling rate then the FFT is an alternative to resampling. Without resampling a fractional sample misalignment from a digital trigger could make sample by sample comparison go woefully wrong.

True, perhaps I am underestimating the intricacies of synchronization.  ;D
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 6857
  • Country: fr
Re: Comparing two pieces of audio on an MCU
« Reply #7 on: May 25, 2021, 05:24:38 pm »
Knowing what kind of features in the signals you want to compare should be "similar" would help. What are those signals?

For a "generic" approach (not assuming anything about the signals to compare), one approach is to use normalized cross-correlation: https://web.psi.edu/spc_wiki/Normalized%20Cross-Correlation

But there may be other, possibly less CPU intensive approaches, depending on the signals you have to compare.


 

Offline simonlasnier

  • Frequent Contributor
  • **
  • Posts: 283
  • Country: dk
Re: Comparing two pieces of audio on an MCU
« Reply #8 on: May 25, 2021, 07:42:18 pm »
Hi all

Thank you very much for the replies!
I thought I needed stronger tools like FFT because the signal was not as precise time-wise, it could vary from 1 to 3 samples delay.

But I actually ended up using a solution similar to sigma_xi's, but comparing 5 samples (from index-2 to index+2) on the original/reference samples to take into account that the signal could be slightly moved.

I had to raise the threshold for error to +/- 7 (on 8-bit values) otherwise I would get a failed result once in a while. I am checking the error for each sample though and not summing them up, so when it is valid I know my samples are on the +/- 7 range :)

Thank you all again - it's not the prettiest solution but it works (it also fails when it should I tested it), for a testing board i do not want to spending ages on the code (otherwise I might as well just test manually  ::) )
 

Offline ucanel

  • Regular Contributor
  • *
  • Posts: 133
  • Country: tr
Re: Comparing two pieces of audio on an MCU
« Reply #9 on: May 25, 2021, 07:54:48 pm »
This may give some idea:
"how shazam works computerphile"
https://youtu.be/RRsq9apr5QY
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 1591
  • Country: us
Re: Comparing two pieces of audio on an MCU
« Reply #10 on: May 25, 2021, 08:10:10 pm »
I would use test samples that consist of a single frequency. Testing that you are getting the expected frequency and amplitude is a lot simpler than comparing complex wave forms. It is also time-invariant -- i.e. it doesn't matter when you start sampling as long as you collect enough complete cycles.

Have multiple test samples that span the range of frequencies you'll be working with.
 

Offline simonlasnier

  • Frequent Contributor
  • **
  • Posts: 283
  • Country: dk
Re: Comparing two pieces of audio on an MCU
« Reply #11 on: May 26, 2021, 08:26:00 am »
@ledtester: yes that would be ideal but that would require changing the device under test :)
 

Offline JohnnyMalaria

  • Super Contributor
  • ***
  • Posts: 1147
  • Country: us
    • Enlighten Scientific LLC
Re: Comparing two pieces of audio on an MCU
« Reply #12 on: May 26, 2021, 04:07:25 pm »
Cross correlation (not autocorrelation since that is on a single signal).
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 3073
  • Country: ro
Re: Comparing two pieces of audio on an MCU
« Reply #13 on: May 26, 2021, 04:22:56 pm »
If a human had to check, I would simply look at the two and say "yea they look similar" - that is good enough for me.
My question: how can I do that check with an MCU (without using an external computer)?

Train a NN on a computer, to get a model that recognizes the expected type of signals.  Then, once you have a trained model, it can be run on the MCU for inference only, to give a pass/fail check with the MCU alone.




LATER EDIT:
Stumbled today upon an article with a project doing exactly that:
https://hackaday.com/2021/05/26/speech-recognition-on-an-arduino-nano/
https://www.instructables.com/Speech-Recognition-With-an-Arduino-Nano/
« Last Edit: May 27, 2021, 09:45:43 am by RoGeorge »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf