[python]正弦波绘制

参考文章:
音频文件基本处理流程
Python中的音频和数字信号处理(DSP)

代码
最终生成test.wav文件
在这里插入图片描述

import numpy as np
 
import wave
 
import struct
 
import matplotlib.pyplot as plt
 
# frequency is the number of times a wave repeats a second

# 频率
frequency = 1000

# 音频长度
num_samples = 480000
 
# The sampling rate of the analog to digital convert

# 采样率
sampling_rate = 48000.0

# 振幅 32000对应1,即0db
amplitude = 24000
 
file = "test.wav"

sine_wave = [np.sin(2 * np.pi * frequency * x/sampling_rate) for x in range(num_samples)]

nframes=num_samples
 
comptype="NONE"
 
compname="not compressed"
 
# 通道数
nchannels=1

# 采样位宽
sampwidth=2

wav_file=wave.open(file, 'w')
 
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))


for s in sine_wave:
   wav_file.writeframes(struct.pack('h', int(s*amplitude)))

注意amplitude 参数,当它为32000时,显示的波形的纵坐标峰值为1,对应的是1khz0db
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值