Build a cheap/small sound spectrum analyser in MicorPython project

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
kynixsemic
Posts: 1
Joined: Mon Jan 15, 2018 8:19 am

Build a cheap/small sound spectrum analyser in MicorPython project

Post by kynixsemic » Mon Jan 15, 2018 8:30 am

Explanation
In this Micropython project I wanted to build a cheap/small sound spectrum analyser to identify specific tones during testing. Initially I tried to connect the Adafruit sound sensor with an ADC and RPI but then faced a lot of timing problems and therefore went to the ARM-M4 micro-controller which is a pretty good straight forward solution. On git-hub I found an FFT code written in ARM assembler by Peter Hinch, which allowed me to obtain on-board frequency spectrum calculation of an array, based on the Cooley-Tukey algorithm.

https://i.stack.imgur.com/Bxllg.png
micropython+adafruit

The picture below illustrates the spectrum(top) in a silent room, the middle plot shows the calculated gradients of the domain and the last the reactivity of the specific tones. The system is continuously captures 2**10 samples in 0.1s >10Hz-5kHz, calculates the FFT and forwards output array to PC in 15ms.

Sound spectrum 10Hz-5kHz

The problem is found in the harmonic tones that appear at exactly 1,2,3,4,5,.. kHz (graph was taken in silent room). Second problem shows up when there is a little more noise; spikes(bottom graph) appear around the harmonic tones+/-240Hz, aliased?. The bottom plot shows the reactivity of the tones.
https://i.stack.imgur.com/KKYHR.png
Tried:
No power; all frequency zero except a few low freq show minor amplitude.
Settings of the FFT for 0.5s/0.1s/0.05s/0.01s and 512/1024/2048 samples; but show the same peaks at same freq.
Three different ADC channels on the Pyboard.
Both 3V3 and GND connections.
External regulated 2-5V with shared ground (laptop no charge). (makes it worse; more noise)
What is most likely the cause of this behaviour? Board circuit / Electret-Amplifier / FFT-code / USB-power?

Alternatives?
Does someone have experience with, or has a better alternative for a cheap on-board spectrum analysis:

Me Sound sensor (LM386) amplifier datasheet: http://www.ti.com/lit/ds/symlink/lm386.pdf
Grove sound sensor (LM386) amplifier.
Adafruit sound sensor (MAX4466) amplifier datasheet: http://www.kynix.com/uploadfiles/pdf967 ... 847424.pdf

User avatar
pythoncoder
Posts: 3450
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Build a cheap/small sound spectrum analyser in MicorPython project

Post by pythoncoder » Tue Jan 16, 2018 10:32 am

Have you got an anti-aliasing filter? If not it's very likely that the 1KHz comb spectrum is an artefact resulting from high frequency digital noise being aliased down into the audio band.

It's worth noting that microphone preamps are sensitive things prone to noise pickup - you really need to give attention to screening. Put the preamp and the anti-aliasing filter in a metal enclosure with the enclosure connected to gnd and use shielded cables for the mic and between the preamp and the Pyboard.

I and others have observed that the amount of digital noise around the Pyboard is reduced if not using the USB connection.
Peter Hinch

triturus
Posts: 2
Joined: Fri Mar 22, 2019 11:45 pm

Re: Build a cheap/small sound spectrum analyser in MicorPython project

Post by triturus » Mon Mar 25, 2019 2:44 am

Hi kynixsemic,

well I´m quite new in pyboard and micropython, but I´m very interested in your theme. I worked a lot with audio signals (lf and ultrasound)
and everytime the mature of analog signals and digital produce problems.
I can confirm Peter´s advise regarding the metal enclosure. Often exist also problems with the power line of the preamp and we use 1000µf
elko between + and GND. In addition to this sometimes we use an additional groundplane in the housing connected to analog ground.

Do you or Peter know where I can find sample code for becoming familiar with this field in micropython. I want make a small recorder for sounds
and up to now I didn´t find much informations. Also it is difficult to find informations about the speed limitations.

The minimum sample rate is 11.025 kHz, 22.050 is good, 44.1 is better and 441 kHz would be great.

Benedikt

User avatar
pythoncoder
Posts: 3450
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Build a cheap/small sound spectrum analyser in MicorPython project

Post by pythoncoder » Mon Mar 25, 2019 11:28 am

44.1KHz should be easy on a Pyboard. But you have to consider data volume: the higher the sample rate the shorter is the duration because data is stored in RAM which is relatively scarce.

With a DFT the correct way to view this is to consider the maximum frequency you want to plot. If you want to plot up to 16KHz then the Nyqist theorem dictates that you must sample at >=32Ksps. In practice you need to sample at a higher rate in order to be able to design a practical anti-aliasing filter. For example you might sample at 64Ksps and want +-1dB of accuracy with 40dB of dynamic range. In this case your AA filter needs to pass 16KHz (+-1dB) but reject 32KHz with 40dB of rejection (because 32.1KHz will be aliased down to 0.1KHz).

It's worth reading up on sampling theory if this stuff is unfamiliar.
Peter Hinch

triturus
Posts: 2
Joined: Fri Mar 22, 2019 11:45 pm

Re: Build a cheap/small sound spectrum analyser in MicorPython project

Post by triturus » Mon Mar 25, 2019 4:57 pm

well, we calculate ever the factor of 2 to 2.5 of the maximum frequency as sampling rate. The main problem I see in the buffer and how to
get all out onto a SD card just in time. In case of the results I´m flexible because in the first step the question is, what is possible and how I
can work around upcoming problems.

Actual I´m designing a mainboard which can pickup the pyboard with some addon´s like FRAM, port expander backup battery and the
analog preamp section.

Do you know whether it is possible to clock the internal ADC with an external clock signal?

Benedikt

User avatar
Mike Teachman
Posts: 69
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Build a cheap/small sound spectrum analyser in MicorPython project

Post by Mike Teachman » Sat Mar 30, 2019 4:51 am

An alternative design approach is to use an I2S microphone with the ESP32. Here is a low-cost I2S microphone that has a built-in digital low pass filter. Using this microphone you will get a stream of 24-bit resolution audio samples.
https://www.invensense.com/wp-content/u ... NMP441.pdf
Modules available on ebay and aliexpress

An I2S microphone digitizes the audio right on the chip which might offer a lower noise solution compared to sampling an analog audio signal with the ADC on a microcontroller. To be determined.

The ESP32 port of uPy currently does not offer I2S support. To use this microphone with the ESP32 requires a custom build, integrating my C-based I2S Module. This module uses the DMA-based implementation in Espressif's ESP-IDF. This means that you can perform sample analysis while the next batch of audio samples is being collected in the background. It's possible that a future uPy release pulls in this work. But, for the moment, you need to make a custom build. Here's a link to the PR:
https://github.com/micropython/micropython/pull/4471

You can configure the sample rate. 44.1kHz is possible, but you will need a compiled FFT in C or ASM to do any onboard analysis of the sample stream.

A usage guide to the PR
https://github.com/miketeachman/micropy ... s-examples

Post Reply