You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyAudio实现主动降噪时遇RuntimeWarning错误(Python3.6)

Fixing the RuntimeWarning in Your Active Noise Cancellation Code

Hey there, let's work through that RuntimeWarning you're seeing when running your Python 3.6 active noise cancellation script. That warning about "invalid value encountered in true_divide" almost always means you're trying to divide by zero (or a NaN/inf value) in line 75 of your code.

First, let's restate the error you got for clarity:

runfile('C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py', wdir='C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT')

  • recording
    C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/SIGNLABPROJ.py:75: RuntimeWarning: invalid value encountered in true_divide
    obj = np.real(np.dot(avg_freq_buffer[1:51], np.abs(freq_synth[1:100:2])) / np.dot(f...

Here are the steps to diagnose and fix this:

  • Check the denominator value first
    The warning comes from the division operation in that line. The denominator (the second np.dot(...) call) is likely evaluating to 0 or NaN. Add a print statement right before line 75 to see what's going on:

    # Insert this above line 75
    denominator = np.dot(...)  # Replace ... with the exact expression from your denominator
    print(f"Denominator value at line 75: {denominator}")
    

    If you see 0 or NaN here, that's the root cause.

  • Avoid division by zero with a small epsilon
    If the denominator is 0, you can add a tiny constant (epsilon) to it to prevent the warning without messing up your calculation:

    epsilon = 1e-8  # A very small number that won't affect valid results
    obj = np.real(np.dot(avg_freq_buffer[1:51], np.abs(freq_synth[1:100:2])) / (np.dot(...) + epsilon))
    
  • Check for NaN/inf in your input arrays
    Sometimes the issue isn't the denominator itself, but invalid values in avg_freq_buffer or freq_synth. Run these checks to verify:

    # Check the slices you're using in the dot product
    print("NaN in avg_freq_buffer slice:", np.any(np.isnan(avg_freq_buffer[1:51])))
    print("NaN in freq_synth slice:", np.any(np.isnan(freq_synth[1:100:2])))
    print("Inf in avg_freq_buffer slice:", np.any(np.isinf(avg_freq_buffer[1:51])))
    print("Inf in freq_synth slice:", np.any(np.isinf(freq_synth[1:100:2])))
    

    If any of these return True, you'll need to trace back how these arrays are generated—maybe your recording or signal processing step is producing invalid values.

  • Update your numpy version (if possible)
    Python 3.6 is pretty old, and the numpy version you're using might have bugs with certain array operations. The latest numpy version compatible with 3.6 is 1.19.5; you can upgrade it with:

    pip install numpy==1.19.5
    

内容的提问来源于stack exchange,提问作者Markus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:28:52