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

如何去除实时可调频正弦波音频信号中的无用谐波噪声?

实时可调频正弦波的谐波噪声去除问题

我制作了一个可通过键盘输入实时调整频率的正弦波信号,但音频输出中存在大量我认为是谐波的噪声,希望将其去除。本项目需要生成初始频率为400Hz、步长为0.1Hz的可调频正弦波信号,以下是我的音频生成代码:

import numpy as np
import sounddevice as sd
import time
import msvcrt
from scipy import signal

# Define the initial parameters
duration = 0.026  # Duration of the sine wave audio block
freq = 400        # Starting frequency in Hz
sample_rate = 48000  # Sample rate in Hz
step = 0.1        # Frequency adjustment step in Hz

# Generate the initial time array for one audio block
block_size = int(duration * sample_rate)
t_block = np.linspace(0, duration, block_size, endpoint=False)

# Kaiser window
window = np.kaiser(block_size, beta=5)  # This is for window width

# Design the band-pass filter
nyquist_freq = 0.5 * sample_rate
cutoff_freq = [390/nyquist_freq, 424/nyquist_freq]  # Cutoff frequency for the band-pass filter in Hz
b, a = signal.butter(4, cutoff_freq, 'bandpass')

# Define the audio callback function for each block
def audio_callback(outdata, frames, time, status):
    global freq
    wave = np.sin(2 * np.pi * freq * t_block)
    outdata[:, 0] = signal.lfilter(b, a, wave * window)  # Filtered signal

# Create an audio stream
stream = sd.OutputStream(callback=audio_callback, channels=1, samplerate=sample_rate)  # Change to 2 channels for both ears

# Start the audio stream
stream.start()

# Main loop for real-time frequency adjustment
while True:
    if msvcrt.kbhit():
        key = msvcrt.getch()
        if key == b'\x1b':  # 'esc' key to exit the program
            break
        elif key == b'H':  # Up arrow key to increase frequency
            freq += step
            print(f"Frequency: {freq:.1f}")
        elif key == b'P':  # Down arrow key to decrease frequency
            freq -= step
            print(f"Frequency: {freq:.1f}")

    time.sleep(duration)  # Wait for the duration of each audio block

# Stop and close the audio stream
stream.stop()
stream.close()

我通过外部麦克风录制扬声器输出的音频,生成了三张对比图:

  • 第一张为期望效果:单一清晰的单频信号
  • 第二张为同时应用带通滤波器与Kaiser窗后的效果
  • 第三张为仅应用Kaiser窗的效果

作为音频工程领域的新手,我可能遗漏了某些关键技术,若有需要补充的信息请告知。

我已尝试过以下方法,但效果未达预期:

  • 应用带通滤波器、陷波滤波器等多种滤波器
  • 调整Kaiser窗的beta值(该操作显著改善了信号质量,但仍未达到成品要求)
  • 尝试调频合成、线性回归、调整陷波滤波器等技术,这些操作要么导致音频质量下降,要么未带来明显改善

我的核心需求是生成单一清晰的单频信号,提升音频清晰度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 03:28:25