pythonscipy

resample_poly from scipy.signal gives all-zero result on pi3b


I am building an audio signal detector and have a basic version working on PC, but cannot get it to work on my target Raspberry Pi 3B. I have narrowed the problem down to down-sampling using resample_poly (to reduce CPU load). I have prepared a really simple code sample:

import numpy as np
from scipy.signal import resample_poly

audio_data = np.array([ 43, 58, 67, 88, 89, 99, 121, 113, 88, 69])
sample_rate = 4000
file_rate = 16000
print(f"Before: {audio_data}")
audio_data = resample_poly(audio_data, sample_rate, file_rate)
print(f"After {audio_data}")

On PC, I get: After [ 28.66957128 103.77145182 80.49835032] On Pi, I get: After [0. 0. 0.]

This is running on numpy-2.0.2 and scipy-1.13.1. The only older version I can get to run is numpy-1.23.0 scipy-1.8.1 with the same result. Other versions are not available as wheels and would not build on my pi, or failed on import in Python. Driving me totally mad just now!!!


Solution

  • The problem sounds like this github issue. If you can't upgrade to a newer version of SciPy, try converting audio_data to floating point before calling resample_poly. E.g.

    audio_data = np.array([ 43, 58, 67, 88, 89, 99, 121, 113, 88, 69], dtype=np.float64)
    

    or, if you prefer to keep the data in its original form as long as possible,

    resample_poly(audio_data.astype(np.float64), sample_rate, file_rate)