【发布时间】:2018-09-05 23:54:02
【问题描述】:
我对 python 完全陌生,所以我尝试阅读和学习我能做的,但我似乎无法做我想做的事,而且我还没有在 Stack Overflow 或其他来源找到解决方案。我的目标是创建一个brown noise 的波形文件,并在给定频率下进行幅度调制。我想生成棕色噪声并对其进行调制。
我打算使用python acoustics package,不幸的是我不明白如何使用这些函数来创建彩色噪声。我查看了示例,但没有看到有关使用彩色噪声函数的示例。
谁能帮我解决这个问题?谢谢。
这是我的代码:
""" This file proposes to modulate a given wav file"""
import wave
import struct
import time
import math
import random
import acoustics
###########################################
# General variables
outputName = "waveXP.wav"
frequencyModulation = 40
period = 1/frequencyModulation
duration = 1
maxVolume = 23000.0
framerate = 44100
###########################################
# Ask the user about the output file name
temp = ""
temp = input("Name of the output wave file to import (with extension):")
if temp != "":
outputName = str(temp)
# Ask the user about the modulation frequency wanted
temp = ""
temp = input("Modulation frequency wanted (in hertz):")
if temp != "":
frequencyModulation = int(temp)
period = 1/frequencyModulation
# Ask the user about the duration wanted
temp = ""
temp = input("Duration wanted (in seconds):")
if temp != "":
duration = int(temp)
print("------------------------")
###########################################
# Create the output wave file
newWaveFile = wave.open(outputName, "w")
# Define parameters of the wave file
# nchannels = 1 for mono; sampwidth = 2 for 2 bytes per sample; framerate = 44100 for wave file;
# comptype = "NONE" for no compression support; compname = 'not compressed' for no compression support
newWaveFile.setparams([1, 2, framerate, duration, 'NONE', 'not compressed'])
# Generate noise
newWaveFile.writeframes(struct.pack('h'*framerate*duration, *int(maxVolume*0.7*acoustics.generator.brown(framerate*duration))))
# Close wave files
originalWaveFile.close()
newWaveFile.close()
【问题讨论】:
-
为“棕色噪音”点赞
-
我想你可能会从code review得到更好的回答。
-
@user3351605 谢谢你的提示,我会尝试代码审查
-
@user3351605 我不太确定;由于第(1)项。代码审查仅适用于功能完整的代码,如果出现错误或意外行为,即处于调试状态且不在主题范围内。
-
关于这个问题本身,您可以通过将其缩减为一次一个特定问题来使其变得更好。现在它太宽泛了。
标签: python wave modulation noise-generator acoustics