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