numpy 当从python中的系统声音中听到特定频率(组合)时,有什么方法可以做出响应吗?

wa7juj8i  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(68)

我目前正在尝试用python创建一个类似调制解调器的脚本,它使用声音来响应其他带有sounddevice的自身示例,有点像以前使用的真实的调制解调器。
我已经开发了一些发送和回复功能,如DSPG生成器和二进制转换器,但我有一个问题,检测某些频率(440赫兹+350赫兹又名。拨号音),这使我无法继续收听其他声音(DVDs,数据等)和真实的回复。
我对sounddevice和numpy也很陌生,只使用了其他用户为opencv提供的numpy代码。我只想出了如何创建和播放选定的正弦波为选定的时间。对于接收部分,我主要使用ChatGPT,但它的代码要么没有回复,要么总是返回一个错误,所以我决定尝试自己做一个,但(至少对我来说)文档对我来说没有意义,希望还没有。
如果你能以任何方式帮助我使用ChatGPT给我的脚本,这里是:

import sounddevice as sd
import numpy as np

# Parameters
target_frequencies = [440, 350]  # Frequencies to detect (440Hz and 350Hz)
duration = 15  # Duration in seconds
sample_rate = 44100  # Sample rate

# Callback function for audio input
def audio_callback(indata, frames, time, status):
    # Convert audio data to mono
    mono_data = np.mean(indata, axis=1)
    
    # Compute the Fast Fourier Transform (FFT)
    fft_data = np.fft.fft(mono_data)
    freqs = np.fft.fftfreq(len(fft_data), 1 / sample_rate)
    
    # Find the indices of the target frequencies
    target_indices = [np.argmin(np.abs(freqs - freq)) for freq in target_frequencies]
    
    # Check if the target frequencies are present
    if all(abs(fft_data[index]) > 10000 for index in target_indices):
        print("yo yo yo")

# Start recording
with sd.InputStream(callback=audio_callback, channels=2, samplerate=sample_rate):
    print("Listening for tones...")
    sd.sleep(int(duration * 1000))  # Record for the desired duration
    print("Recording finished")

否则,请至少向我解释一下,例如InputStream是如何工作的,以及我如何从它中检测声音。
谢谢你,谢谢!

nwo49xxi

nwo49xxi1#

我希望从ChatGPT所产生的内容中,你已经学会了不要相信它来编写应用程序。
对于您的应用程序来说,检测最大光谱分量是不够的,并且 * 肯定 * 不足以检测任何高于10,000的任意值的此类分量。相反,您需要某种启发式方法来比较您关心的带内频谱能量与总能量,如果超过阈值,则认为您的音调存在。(此外,您需要检查总能量,以区分“一些声音”和“背景噪音”;我没有展示这一点)。
FFT基于样本大小和采样频率有许多折衷。你不希望分辨率太低,否则你将无法区分好的频率和坏的频率。你不希望它太高,否则每个块将花费比它需要的更长的时间来捕获(也会占用比它需要的更多的内存)。你不希望样本大小太低,否则你会错过你的最低频率。您不希望样本大小太大,否则捕获样本所需的时间太长,并且无法尽可能快地响应。
在这种情况下,频率桶大小的合理值是10 Hz,因为您感兴趣的两个频率的最大公因数是10,这足以将这些音调与DVB-POTS系统中的其他音调区分开来。
在你的麦克风上尝试之前,先在维基百科的一个固定文件上尝试一下:

import librosa
import numpy as np

print('Loading canned tone...')
canned, rate = librosa.load('US_dial_tone.mp3', mono=True)

# Ignore next-highest DTMF tone of 697 Hz and up
# From the Precise Tone Plan (https://en.wikipedia.org/wiki/Precise_tone_plan),
# ignore 480 and 620 Hz
freq_bucket_size = 10  # greatest common factor of 350 and 440
n = rate//freq_bucket_size
target_frequencies = 350, 440
target_idx = [f//freq_bucket_size for f in target_frequencies]

print('Processing...')
canned = canned[:len(canned) - len(canned)%n]
for chunk in canned.reshape((-1, n)):
    ampl = np.abs(np.fft.rfft(chunk))
    total_energy = ampl[1:].sum()
    tone_energy = ampl[target_idx].sum()
    match = tone_energy / total_energy
    if match > 0.5:
        print(f'Tone matched at {match:.1%} energy')
Loading canned tone...
Processing...
Tone matched at 90.4% energy
Tone matched at 91.3% energy
Tone matched at 89.7% energy
Tone matched at 90.5% energy
Tone matched at 91.1% energy
Tone matched at 91.0% energy
Tone matched at 89.7% energy
Tone matched at 90.8% energy
Tone matched at 92.1% energy
Tone matched at 90.8% energy
Tone matched at 89.7% energy
Tone matched at 90.1% energy
Tone matched at 92.5% energy
Tone matched at 91.7% energy
Tone matched at 89.8% energy
Tone matched at 91.3% energy
Tone matched at 92.1% energy
Tone matched at 90.8% energy
Tone matched at 91.2% energy
Tone matched at 91.3% energy
Tone matched at 92.5% energy
Tone matched at 91.8% energy
Tone matched at 90.3% energy
Tone matched at 93.1% energy
Tone matched at 93.2% energy
Tone matched at 91.3% energy
Tone matched at 91.8% energy
Tone matched at 93.2% energy
Tone matched at 92.6% energy
Tone matched at 92.9% energy
Tone matched at 93.3% energy
Tone matched at 93.8% energy
Tone matched at 94.1% energy
Tone matched at 92.2% energy
Tone matched at 92.9% energy
Tone matched at 93.3% energy
Tone matched at 93.8% energy
Tone matched at 93.6% energy
Tone matched at 93.0% energy
Tone matched at 94.0% energy
Tone matched at 92.1% energy
Tone matched at 91.9% energy
Tone matched at 93.4% energy
Tone matched at 93.2% energy
Tone matched at 91.3% energy
Tone matched at 92.9% energy
Tone matched at 92.8% energy
Tone matched at 91.1% energy
Tone matched at 91.4% energy
Tone matched at 90.1% energy

相关问题