0

I am trying to read MCP3201 with 44100 samples per second. The chip has top clock frequency of 1Mhz, which should be sufficient for 62500 two-byte samples per second.

sampnumb = 44100

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 125000000

for i in range(sampnumb):
    dat = spi.xfer2([0x00, 0x00])

It actually takes almost 2s to read as many samples. Using 976000 for max_speed_hz makes it even slower.

Are there any ways to make sampling faster in Python? If not, is this possible to achieve fast sampling any other way?

1 Answer 1

2

You need to write your software in another language to achieve the speed you are after.

The obvious choice is C.

7
  • integrate top level python calls into low level c calls with cython. don't do a big for loop in python- instead loop in c but tell c the number to loop via python.
    – Abel
    Jan 31 at 12:39
  • @Abel I intend to use ctypes since I already have experience with that.
    – Pygmalion
    Jan 31 at 13:34
  • 1
    With a simple C script (ioctl(fd_spi, SPI_IOC_MESSAGE(1), &spi); spi.rx_buf = spi.rx_buf + 2;) I could get about 15k sampling. Unfortunately sampling is not very consistent, which means that FFT of the sound is rather noisy. Any ideas for improvement?
    – Pygmalion
    Jan 31 at 16:15
  • Use pigpio. Use a faster Pi (you appear to be using an old one).
    – joan
    Jan 31 at 16:33
  • You are doing something wrong if you are getting slower rates in C compared to Python. Have you set the SPI bit rate in your C code?
    – joan
    Jan 31 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.