Make realtime Discrate fournier transform from i2S mic

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Make realtime Discrate fournier transform from i2S mic

Post by zaord » Tue Dec 07, 2021 6:45 pm

This should works because the 2 fondamentals are far higher than the beating frequency.

1) Do I need to do a hilbert transform before doing what you proposed me ?

2) How can I do low pass filter with micropython ?

3) Thanks a LOT :)

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

Re: Make realtime Discrate fournier transform from i2S mic

Post by pythoncoder » Wed Dec 08, 2021 9:57 am

  1. No
  2. See micropython-filters
The above repo uses STM assembler, but FIR filters are very simple and the code could easily be ported to Python. The nice bit is the website which produces coefficients from passband characteristics.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Make realtime Discrate fournier transform from i2S mic

Post by zaord » Wed Dec 08, 2021 10:11 am

Gosh, I don't know assembler :)

I will have a look. Thanks a lot !

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Envelope extraction

Post by zaord » Wed Dec 08, 2021 11:03 am


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

Re: Make realtime Discrate fournier transform from i2S mic

Post by pythoncoder » Thu Dec 09, 2021 9:52 am

That imports fir.py which uses STM Assembler, so will only run on Pyboards and similar. I suggest you read up on FIR filtering: it really is very simple - just a weighted moving average. Writing a Python version of fir.py would be easy.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Make realtime Discrate fournier transform from i2S mic

Post by zaord » Thu Dec 09, 2021 10:25 am

I never learn assembler at school :)
I will try to make it with your comments !

In all the case I will need the table with the coefficient right ?

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

Re: Make realtime Discrate fournier transform from i2S mic

Post by pythoncoder » Thu Dec 09, 2021 10:39 am

You need two arrays, one containing the data and the other the N coefficients. The result is the sum of the products of matching elements of the two arrays. The one challenge is dealing with moving the data through the array. In a realtime solution such as mine this can be done with a circular buffer. If you're not using realtime data your data array will hold the entire sample set, so you only need to change the array index to point to the next N data values to generate successive filtered results.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Make realtime Discrate fournier transform from i2S mic

Post by zaord » Thu Dec 09, 2021 11:19 am

My application is realtime :)
I will use a buffer :)

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

Re: Make realtime Discrate fournier transform from i2S mic

Post by pythoncoder » Fri Dec 10, 2021 4:50 pm

Curiosity got the better of me so I simulated this:

Code: Select all

from math import sin, cos, pi
from array import array
import matplotlib.pyplot as plt

length = 800
a = array('f', (0 for _ in range(length)))
b = array('f', (0 for _ in range(length)))
f = 0.0  # Filtered result

for n in range(length):
    a[n] = sin(n *2 * pi / 20) + 0.9 * sin(n * 2 * pi * 22 /(20 * 20))
    if a[n] < 0:
        a[n] = -a[n]
    f += a[n]
    f -= f/20  #IIR single pole filter
    b[n] = f

plt.plot(range(length), b)
plt.show()
with this outcome:
Image

Note that the filtering is a crude single pole IIR design: trivial to code but not too effective.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Make realtime Discrate fournier transform from i2S mic

Post by zaord » Mon Dec 13, 2021 9:50 pm

Good !
How to find the fundamental frequency of this output in micropython with a realtime optimization ? i should double it due to absolute value opération right ?
You think I should use a fft ?

Best

Ewen

Post Reply