python - 从音调中去除不需要的频率
问题描述
我正在尝试以 2350 Hz 的恒定音调产生“哔”声。我正在使用下面的代码(我在这里得到)来生成一个持续时间为 0.5 秒的具有此音调的 WAV 文件。
import math
import wave
import struct
# Audio will contain a long list of samples (i.e. floating point numbers describing the
# waveform). If you were working with a very long sound you'd want to stream this to
# disk instead of buffering it all in memory list this. But most sounds will fit in
# memory.
audio = []
sample_rate = 44100.0
def append_silence(duration_milliseconds=500):
"""
Adding silence is easy - we add zeros to the end of our array
"""
num_samples = duration_milliseconds * (sample_rate / 1000.0)
for x in range(int(num_samples)):
audio.append(0.0)
return
def append_sinewave(
freq=440.0,
duration_milliseconds=500,
volume=1.0):
"""
The sine wave generated here is the standard beep. If you want something
more aggresive you could try a square or saw tooth waveform. Though there
are some rather complicated issues with making high quality square and
sawtooth waves... which we won't address here :)
"""
global audio # using global variables isn't cool.
num_samples = duration_milliseconds * (sample_rate / 1000.0)
for x in range(int(num_samples)):
audio.append(volume * math.sin(2 * math.pi * freq * ( x / sample_rate )))
return
def save_wav(file_name):
# Open up a wav file
wav_file=wave.open(file_name,"w")
# wav params
nchannels = 1
sampwidth = 2
# 44100 is the industry standard sample rate - CD quality. If you need to
# save on file size you can adjust it downwards. The stanard for low quality
# is 8000 or 8kHz.
nframes = len(audio)
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
# WAV files here are using short, 16 bit, signed integers for the
# sample size. So we multiply the floating point data we have by 32767, the
# maximum value for a short integer. NOTE: It is theortically possible to
# use the floating point -1.0 to 1.0 data directly in a WAV file but not
# obvious how to do that using the wave module in python.
for sample in audio:
wav_file.writeframes(struct.pack('h', int( sample * 32767.0 )))
wav_file.close()
return
append_sinewave(volume=1, freq=2350)
save_wav("output.wav")
当运行下面的代码(使用 Librosa)生成 WAV 文件的频谱图时,我看到:
频谱图:
代码:
beepData,beep_sample_rate = librosa.load(beepSoundPath, sr=44100)
D = librosa.stft(beepData)
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
librosa.display.specshow(S_db)
问题是频谱图开头和结尾的额外频率。我怎样才能摆脱这些不需要的频率?
解决方案
这些是 STFT / FFT 过程的伪影,因为在窗口的开始/结束处存在不连续性。您可以尝试使用librosa.stft(..., center=False)
,它应该消除一开始的那个。然后您可能还需要修剪/忽略最后的输出段。至少一半的n_fft
参数。
推荐阅读
- python-3.x - 子类和父类的比较
- scala - 在 Scala 中展平 + (~self-join) 带有结构数组的 spark 数据帧
- typescript - 根据键列表从对象中选择属性
- java - 为二进制值设置 VALUE_SERIALIZER_CLASS_CONFIG
- angular - 如何在角度 8 中从 http 响应将数据分配给数组
- swift - 您如何在 Swift 中对数组中的元素进行洗牌时找到这种逻辑?
- android - 如何使水平回收器视图中的水平滚动视图工作?
- sql-server - SQL Server:使用自连接输出具有不同名称的同一列会导致性能不佳 - 需要改进
- linux - Windows 作业可以确定 Linux 系统上的文件版本吗?
- python - python浮点不精度